~flimm/python-snippets/gtkcrashhandler

« back to all changes in this revision

Viewing changes to pyturtle/tdemo_chaos.py

  • Committer: Jono Bacon
  • Date: 2010-04-24 18:14:20 UTC
  • mfrom: (93.3.2 python-snippets-bzr)
  • Revision ID: jono@system76-pc-20100424181420-hup208wuekhvr1qc
A bunch of PyTurtle snippets. Thanks, Grant!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Chaos]
 
4
# [SNIPPET_CATEGORIES: PyTurtle]
 
5
# [SNIPPET_DESCRIPTION: Use turtle to graph chaos.]
 
6
# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html]
 
7
# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>]
 
8
# [SNIPPET_LICENSE: PSF]
 
9
# Code authorship from http://python.org/download/releases/2.6.4/
 
10
 
 
11
 
 
12
# File: tdemo_chaos.py
 
13
# Author: Gregor Lingl
 
14
# Date: 2009-06-24
 
15
 
 
16
# A demonstration of chaos
 
17
 
 
18
from turtle import *
 
19
 
 
20
N = 80
 
21
 
 
22
def f(x):
 
23
    return 3.9*x*(1-x)
 
24
 
 
25
def g(x):
 
26
    return 3.9*(x-x**2)
 
27
 
 
28
def h(x):
 
29
    return 3.9*x-3.9*x*x
 
30
 
 
31
def jumpto(x, y):
 
32
    penup(); goto(x,y)
 
33
 
 
34
def line(x1, y1, x2, y2):
 
35
    jumpto(x1, y1)
 
36
    pendown()
 
37
    goto(x2, y2)
 
38
 
 
39
def coosys():
 
40
    line(-1, 0, N+1, 0)
 
41
    line(0, -0.1, 0, 1.1)
 
42
 
 
43
def plot(fun, start, colour):
 
44
    pencolor(colour)
 
45
    x = start
 
46
    jumpto(0, x)
 
47
    pendown()
 
48
    dot(5)
 
49
    for i in range(N):
 
50
        x=fun(x)
 
51
        goto(i+1,x)
 
52
        dot(5)
 
53
 
 
54
def main():
 
55
    reset()
 
56
    setworldcoordinates(-1.0,-0.1, N+1, 1.1)
 
57
    speed(0)
 
58
    hideturtle()
 
59
    coosys()
 
60
    plot(f, 0.35, "blue")
 
61
    plot(g, 0.35, "green")
 
62
    plot(h, 0.35, "red")
 
63
    # Now zoom in:
 
64
    for s in range(100):
 
65
        setworldcoordinates(0.5*s,-0.1, N+1, 1.1)
 
66
    return "Done!"
 
67
 
 
68
if __name__ == "__main__":
 
69
    main()
 
70
    mainloop()