Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96165
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
import turtle
15
16
17
def koch(t, n):
18
"""Draws a koch curve with length n."""
19
if n < 10:
20
t.fd(n)
21
return
22
m = n/3
23
koch(t, m)
24
t.lt(60)
25
koch(t, m)
26
t.rt(120)
27
koch(t, m)
28
t.lt(60)
29
koch(t, m)
30
31
32
def snowflake(t, n):
33
"""Draws a snowflake (a triangle with a Koch curve for each side)."""
34
for i in range(3):
35
koch(t, n)
36
t.rt(120)
37
38
39
bob = turtle.Turtle()
40
41
bob.pu()
42
bob.goto(-150, 90)
43
bob.pd()
44
snowflake(bob, 300)
45
46
turtle.mainloop()
47
48
49