-
Notifications
You must be signed in to change notification settings - Fork 3
/
koch.py
37 lines (31 loc) · 888 Bytes
/
koch.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
#-----------------------------------------------------------------------
# koch.py
#-----------------------------------------------------------------------
import stddraw
import turtle
# Plot to stddraw a Koch curve of order n, with given step size,
# using Turtle object tur.
def koch(n, step, tur):
if n == 0:
tur.goForward(step)
return
koch(n-1, step, tur)
tur.turnLeft(60.0)
koch(n-1, step, tur)
tur.turnLeft(-120.0)
koch(n-1, step, tur)
tur.turnLeft(60.0)
koch(n-1, step, tur)
# Accept integer n as a command-line argument. Plot a Koch curve of
# order n to stddraw.
def main(argv):
n = int(argv[1])
stddraw.createWindow()
step = 1.0 / (3.0 ** n)
tur = turtle.Turtle(0.0, 0.0, 0.0)
koch(n, step, tur)
stddraw.show()
stddraw.wait()
if __name__ == '__main__':
import sys
main(sys.argv)