forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtlefrac.py
executable file
·86 lines (71 loc) · 1.79 KB
/
turtlefrac.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import turtle
import time
import sys
whichcolor = 0
colors = [ "black", "white" ]
screen = turtle.Screen()
def triangle(side):
"""Draw an equilateral triangle with the point on the current point,
the base in the direction of the current heading.
"""
turtle.left(30)
turtle.forward(side)
turtle.right(120)
turtle.forward(side)
turtle.right(120)
turtle.forward(side)
def Sierpinski(side, steps):
"""Draw a Sierpinski triangle fractal, recursing the given number of steps.
Assume we're starting from a black triangle of with sides of size side*2,
point down, with the turtle at the bottom point.
"""
if not steps:
return
# Clear a white triangle in the center.
turtle.setheading(120)
turtle.penup()
turtle.forward(side)
turtle.pendown()
turtle.fill(True)
turtle.fillcolor("white")
turtle.right(120)
turtle.forward(side)
turtle.left(120)
turtle.forward(side)
turtle.left(120)
turtle.forward(side)
turtle.fill(False)
# For each of the black sub-triangles left, run recursively.
Sierpinski(side/2, steps-1)
turtle.setheading(0)
turtle.penup()
turtle.forward(side)
Sierpinski(side/2, steps-1)
turtle.setheading(240)
turtle.penup()
turtle.forward(side)
Sierpinski(side/2, steps-1)
turtle.speed(0)
turtle.setheading(270)
turtle.penup()
turtle.forward(200)
turtle.setheading(90)
turtle.fillcolor("black")
turtle.fill(True)
triangle(500)
turtle.fill(False)
def end_program():
print "Ouch!"
screen.bye()
screen.onkey(end_program, "q")
screen.listen()
if len(sys.argv) <= 1:
steps = 5
else:
steps = int(sys.argv[1])
try:
Sierpinski(250, steps)
turtle.mainloop()
except turtle.Terminator:
print "Turtle Terminator"