MCPcopy Index your code
hub / github.com/python/cpython / CurvesTurtle

Class CurvesTurtle

Lib/turtledemo/fractalcurves.py:14–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12from time import sleep, perf_counter as clock
13
14class CurvesTurtle(Pen):
15 # example derived from
16 # Turtle Geometry: The Computer as a Medium for Exploring Mathematics
17 # by Harold Abelson and Andrea diSessa
18 # p. 96-98
19 def hilbert(self, size, level, parity):
20 if level == 0:
21 return
22 # rotate and draw first subcurve with opposite parity to big curve
23 self.left(parity * 90)
24 self.hilbert(size, level - 1, -parity)
25 # interface to and draw second subcurve with same parity as big curve
26 self.forward(size)
27 self.right(parity * 90)
28 self.hilbert(size, level - 1, parity)
29 # third subcurve
30 self.forward(size)
31 self.hilbert(size, level - 1, parity)
32 # fourth subcurve
33 self.right(parity * 90)
34 self.forward(size)
35 self.hilbert(size, level - 1, -parity)
36 # a final turn is needed to make the turtle
37 # end up facing outward from the large square
38 self.left(parity * 90)
39
40 # Visual Modeling with Logo: A Structural Approach to Seeing
41 # by James Clayson
42 # Koch curve, after Helge von Koch who introduced this geometric figure in 1904
43 # p. 146
44 def fractalgon(self, n, rad, lev, dir):
45 import math
46
47 # if dir = 1 turn outward
48 # if dir = -1 turn inward
49 edge = 2 * rad * math.sin(math.pi / n)
50 self.pu()
51 self.fd(rad)
52 self.pd()
53 self.rt(180 - (90 * (n - 2) / n))
54 for i in range(n):
55 self.fractal(edge, lev, dir)
56 self.rt(360 / n)
57 self.lt(180 - (90 * (n - 2) / n))
58 self.pu()
59 self.bk(rad)
60 self.pd()
61
62 # p. 146
63 def fractal(self, dist, depth, dir):
64 if depth < 1:
65 self.fd(dist)
66 return
67 self.fractal(dist / 3, depth - 1, dir)
68 self.lt(60 * dir)
69 self.fractal(dist / 3, depth - 1, dir)
70 self.rt(120 * dir)
71 self.fractal(dist / 3, depth - 1, dir)

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…