annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/fractalcurves.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 #!/usr/bin/env python3
jpayne@69 2 """ turtle-example-suite:
jpayne@69 3
jpayne@69 4 tdemo_fractalCurves.py
jpayne@69 5
jpayne@69 6 This program draws two fractal-curve-designs:
jpayne@69 7 (1) A hilbert curve (in a box)
jpayne@69 8 (2) A combination of Koch-curves.
jpayne@69 9
jpayne@69 10 The CurvesTurtle class and the fractal-curve-
jpayne@69 11 methods are taken from the PythonCard example
jpayne@69 12 scripts for turtle-graphics.
jpayne@69 13 """
jpayne@69 14 from turtle import *
jpayne@69 15 from time import sleep, perf_counter as clock
jpayne@69 16
jpayne@69 17 class CurvesTurtle(Pen):
jpayne@69 18 # example derived from
jpayne@69 19 # Turtle Geometry: The Computer as a Medium for Exploring Mathematics
jpayne@69 20 # by Harold Abelson and Andrea diSessa
jpayne@69 21 # p. 96-98
jpayne@69 22 def hilbert(self, size, level, parity):
jpayne@69 23 if level == 0:
jpayne@69 24 return
jpayne@69 25 # rotate and draw first subcurve with opposite parity to big curve
jpayne@69 26 self.left(parity * 90)
jpayne@69 27 self.hilbert(size, level - 1, -parity)
jpayne@69 28 # interface to and draw second subcurve with same parity as big curve
jpayne@69 29 self.forward(size)
jpayne@69 30 self.right(parity * 90)
jpayne@69 31 self.hilbert(size, level - 1, parity)
jpayne@69 32 # third subcurve
jpayne@69 33 self.forward(size)
jpayne@69 34 self.hilbert(size, level - 1, parity)
jpayne@69 35 # fourth subcurve
jpayne@69 36 self.right(parity * 90)
jpayne@69 37 self.forward(size)
jpayne@69 38 self.hilbert(size, level - 1, -parity)
jpayne@69 39 # a final turn is needed to make the turtle
jpayne@69 40 # end up facing outward from the large square
jpayne@69 41 self.left(parity * 90)
jpayne@69 42
jpayne@69 43 # Visual Modeling with Logo: A Structural Approach to Seeing
jpayne@69 44 # by James Clayson
jpayne@69 45 # Koch curve, after Helge von Koch who introduced this geometric figure in 1904
jpayne@69 46 # p. 146
jpayne@69 47 def fractalgon(self, n, rad, lev, dir):
jpayne@69 48 import math
jpayne@69 49
jpayne@69 50 # if dir = 1 turn outward
jpayne@69 51 # if dir = -1 turn inward
jpayne@69 52 edge = 2 * rad * math.sin(math.pi / n)
jpayne@69 53 self.pu()
jpayne@69 54 self.fd(rad)
jpayne@69 55 self.pd()
jpayne@69 56 self.rt(180 - (90 * (n - 2) / n))
jpayne@69 57 for i in range(n):
jpayne@69 58 self.fractal(edge, lev, dir)
jpayne@69 59 self.rt(360 / n)
jpayne@69 60 self.lt(180 - (90 * (n - 2) / n))
jpayne@69 61 self.pu()
jpayne@69 62 self.bk(rad)
jpayne@69 63 self.pd()
jpayne@69 64
jpayne@69 65 # p. 146
jpayne@69 66 def fractal(self, dist, depth, dir):
jpayne@69 67 if depth < 1:
jpayne@69 68 self.fd(dist)
jpayne@69 69 return
jpayne@69 70 self.fractal(dist / 3, depth - 1, dir)
jpayne@69 71 self.lt(60 * dir)
jpayne@69 72 self.fractal(dist / 3, depth - 1, dir)
jpayne@69 73 self.rt(120 * dir)
jpayne@69 74 self.fractal(dist / 3, depth - 1, dir)
jpayne@69 75 self.lt(60 * dir)
jpayne@69 76 self.fractal(dist / 3, depth - 1, dir)
jpayne@69 77
jpayne@69 78 def main():
jpayne@69 79 ft = CurvesTurtle()
jpayne@69 80
jpayne@69 81 ft.reset()
jpayne@69 82 ft.speed(0)
jpayne@69 83 ft.ht()
jpayne@69 84 ft.getscreen().tracer(1,0)
jpayne@69 85 ft.pu()
jpayne@69 86
jpayne@69 87 size = 6
jpayne@69 88 ft.setpos(-33*size, -32*size)
jpayne@69 89 ft.pd()
jpayne@69 90
jpayne@69 91 ta=clock()
jpayne@69 92 ft.fillcolor("red")
jpayne@69 93 ft.begin_fill()
jpayne@69 94 ft.fd(size)
jpayne@69 95
jpayne@69 96 ft.hilbert(size, 6, 1)
jpayne@69 97
jpayne@69 98 # frame
jpayne@69 99 ft.fd(size)
jpayne@69 100 for i in range(3):
jpayne@69 101 ft.lt(90)
jpayne@69 102 ft.fd(size*(64+i%2))
jpayne@69 103 ft.pu()
jpayne@69 104 for i in range(2):
jpayne@69 105 ft.fd(size)
jpayne@69 106 ft.rt(90)
jpayne@69 107 ft.pd()
jpayne@69 108 for i in range(4):
jpayne@69 109 ft.fd(size*(66+i%2))
jpayne@69 110 ft.rt(90)
jpayne@69 111 ft.end_fill()
jpayne@69 112 tb=clock()
jpayne@69 113 res = "Hilbert: %.2fsec. " % (tb-ta)
jpayne@69 114
jpayne@69 115 sleep(3)
jpayne@69 116
jpayne@69 117 ft.reset()
jpayne@69 118 ft.speed(0)
jpayne@69 119 ft.ht()
jpayne@69 120 ft.getscreen().tracer(1,0)
jpayne@69 121
jpayne@69 122 ta=clock()
jpayne@69 123 ft.color("black", "blue")
jpayne@69 124 ft.begin_fill()
jpayne@69 125 ft.fractalgon(3, 250, 4, 1)
jpayne@69 126 ft.end_fill()
jpayne@69 127 ft.begin_fill()
jpayne@69 128 ft.color("red")
jpayne@69 129 ft.fractalgon(3, 200, 4, -1)
jpayne@69 130 ft.end_fill()
jpayne@69 131 tb=clock()
jpayne@69 132 res += "Koch: %.2fsec." % (tb-ta)
jpayne@69 133 return res
jpayne@69 134
jpayne@69 135 if __name__ == '__main__':
jpayne@69 136 msg = main()
jpayne@69 137 print(msg)
jpayne@69 138 mainloop()