~flimm/python-snippets/gtkcrashhandler

« back to all changes in this revision

Viewing changes to pyturtle/tdemo_penrose.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: Penrose]
 
4
# [SNIPPET_CATEGORIES: PyTurtle]
 
5
# [SNIPPET_DESCRIPTION: Use turtle to draw two Penrose tilings.]
 
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
"""       xturtle-example-suite:
 
13
 
 
14
          xtx_kites_and_darts.py
 
15
 
 
16
Constructs two aperiodic penrose-tilings,
 
17
consisting of kites and darts, by the method
 
18
of inflation in six steps.
 
19
 
 
20
Starting points are the patterns "sun"
 
21
consisting of five kites and "star"
 
22
consisting of five darts.
 
23
 
 
24
For more information see:
 
25
 http://en.wikipedia.org/wiki/Penrose_tiling
 
26
 -------------------------------------------
 
27
"""
 
28
from turtle import *
 
29
from math import cos, pi
 
30
from time import clock, sleep
 
31
 
 
32
f = (5**0.5-1)/2.0   # (sqrt(5)-1)/2 -- golden ratio
 
33
d = 2 * cos(3*pi/10)
 
34
 
 
35
def kite(l):
 
36
    fl = f * l
 
37
    lt(36)
 
38
    fd(l)
 
39
    rt(108)
 
40
    fd(fl)
 
41
    rt(36)
 
42
    fd(fl)
 
43
    rt(108)
 
44
    fd(l)
 
45
    rt(144)
 
46
 
 
47
def dart(l):
 
48
    fl = f * l
 
49
    lt(36)
 
50
    fd(l)
 
51
    rt(144)
 
52
    fd(fl)
 
53
    lt(36)
 
54
    fd(fl)
 
55
    rt(144)
 
56
    fd(l)
 
57
    rt(144)
 
58
 
 
59
def inflatekite(l, n):
 
60
    if n == 0:
 
61
        px, py = pos()
 
62
        h, x, y = int(heading()), round(px,3), round(py,3)
 
63
        tiledict[(h,x,y)] = True
 
64
        return
 
65
    fl = f * l
 
66
    lt(36)
 
67
    inflatedart(fl, n-1)
 
68
    fd(l)
 
69
    rt(144)
 
70
    inflatekite(fl, n-1)
 
71
    lt(18)
 
72
    fd(l*d)
 
73
    rt(162)
 
74
    inflatekite(fl, n-1)
 
75
    lt(36)
 
76
    fd(l)
 
77
    rt(180)
 
78
    inflatedart(fl, n-1)
 
79
    lt(36)
 
80
 
 
81
def inflatedart(l, n):
 
82
    if n == 0:
 
83
        px, py = pos()
 
84
        h, x, y = int(heading()), round(px,3), round(py,3)
 
85
        tiledict[(h,x,y)] = False
 
86
        return
 
87
    fl = f * l
 
88
    inflatekite(fl, n-1)
 
89
    lt(36)
 
90
    fd(l)
 
91
    rt(180)
 
92
    inflatedart(fl, n-1)
 
93
    lt(54)
 
94
    fd(l*d)
 
95
    rt(126)
 
96
    inflatedart(fl, n-1)
 
97
    fd(l)
 
98
    rt(144)
 
99
 
 
100
def draw(l, n, th=2):
 
101
    clear()
 
102
    l = l * f**n
 
103
    shapesize(l/100.0, l/100.0, th)
 
104
    for k in tiledict:
 
105
        h, x, y = k
 
106
        setpos(x, y)
 
107
        setheading(h)
 
108
        if tiledict[k]:
 
109
            shape("kite")
 
110
            color("black", (0, 0.75, 0))
 
111
        else:
 
112
            shape("dart")
 
113
            color("black", (0.75, 0, 0))
 
114
        stamp()
 
115
 
 
116
def sun(l, n):
 
117
    for i in range(5):
 
118
        inflatekite(l, n)
 
119
        lt(72)
 
120
 
 
121
def star(l,n):
 
122
    for i in range(5):
 
123
        inflatedart(l, n)
 
124
        lt(72)
 
125
 
 
126
def makeshapes():
 
127
    tracer(0)
 
128
    begin_poly()
 
129
    kite(100)
 
130
    end_poly()
 
131
    register_shape("kite", get_poly())
 
132
    begin_poly()
 
133
    dart(100)
 
134
    end_poly()
 
135
    register_shape("dart", get_poly())
 
136
    tracer(1)
 
137
 
 
138
def start():
 
139
    reset()
 
140
    ht()
 
141
    pu()
 
142
    makeshapes()
 
143
    resizemode("user")
 
144
 
 
145
def test(l=200, n=4, fun=sun, startpos=(0,0), th=2):
 
146
    global tiledict
 
147
    goto(startpos)
 
148
    setheading(0)
 
149
    tiledict = {}
 
150
    a = clock()
 
151
    tracer(0)
 
152
    fun(l, n)
 
153
    b = clock()
 
154
    draw(l, n, th)
 
155
    tracer(1)
 
156
    c = clock()
 
157
    print "Calculation:   %7.4f s" % (b - a)
 
158
    print "Drawing:  %7.4f s" % (c - b)
 
159
    print "Together: %7.4f s" % (c - a)
 
160
    nk = len([x for x in tiledict if tiledict[x]])
 
161
    nd = len([x for x in tiledict if not tiledict[x]])
 
162
    print "%d kites and %d darts = %d pieces." % (nk, nd, nk+nd)
 
163
 
 
164
def demo(fun=sun):
 
165
    start()
 
166
    for i in range(8):
 
167
        a = clock()
 
168
        test(300, i, fun)
 
169
        b = clock()
 
170
        t = b - a
 
171
        if t < 2:
 
172
            sleep(2 - t)
 
173
 
 
174
def main():
 
175
    #title("Penrose-tiling with kites and darts.")
 
176
    mode("logo")
 
177
    bgcolor(0.3, 0.3, 0)
 
178
    demo(sun)
 
179
    sleep(2)
 
180
    demo(star)
 
181
    pencolor("black")
 
182
    goto(0,-200)
 
183
    pencolor(0.7,0.7,1)
 
184
    write("Please wait...",
 
185
          align="center", font=('Arial Black', 36, 'bold'))
 
186
    test(600, 8, startpos=(70, 117))
 
187
    return "Done"
 
188
 
 
189
if __name__ == "__main__":
 
190
    msg = main()
 
191
    mainloop()