jpayne@68: #!/usr/bin/env python3 jpayne@68: """ turtle-example-suite: jpayne@68: jpayne@68: tdemo_fractalCurves.py jpayne@68: jpayne@68: This program draws two fractal-curve-designs: jpayne@68: (1) A hilbert curve (in a box) jpayne@68: (2) A combination of Koch-curves. jpayne@68: jpayne@68: The CurvesTurtle class and the fractal-curve- jpayne@68: methods are taken from the PythonCard example jpayne@68: scripts for turtle-graphics. jpayne@68: """ jpayne@68: from turtle import * jpayne@68: from time import sleep, perf_counter as clock jpayne@68: jpayne@68: class CurvesTurtle(Pen): jpayne@68: # example derived from jpayne@68: # Turtle Geometry: The Computer as a Medium for Exploring Mathematics jpayne@68: # by Harold Abelson and Andrea diSessa jpayne@68: # p. 96-98 jpayne@68: def hilbert(self, size, level, parity): jpayne@68: if level == 0: jpayne@68: return jpayne@68: # rotate and draw first subcurve with opposite parity to big curve jpayne@68: self.left(parity * 90) jpayne@68: self.hilbert(size, level - 1, -parity) jpayne@68: # interface to and draw second subcurve with same parity as big curve jpayne@68: self.forward(size) jpayne@68: self.right(parity * 90) jpayne@68: self.hilbert(size, level - 1, parity) jpayne@68: # third subcurve jpayne@68: self.forward(size) jpayne@68: self.hilbert(size, level - 1, parity) jpayne@68: # fourth subcurve jpayne@68: self.right(parity * 90) jpayne@68: self.forward(size) jpayne@68: self.hilbert(size, level - 1, -parity) jpayne@68: # a final turn is needed to make the turtle jpayne@68: # end up facing outward from the large square jpayne@68: self.left(parity * 90) jpayne@68: jpayne@68: # Visual Modeling with Logo: A Structural Approach to Seeing jpayne@68: # by James Clayson jpayne@68: # Koch curve, after Helge von Koch who introduced this geometric figure in 1904 jpayne@68: # p. 146 jpayne@68: def fractalgon(self, n, rad, lev, dir): jpayne@68: import math jpayne@68: jpayne@68: # if dir = 1 turn outward jpayne@68: # if dir = -1 turn inward jpayne@68: edge = 2 * rad * math.sin(math.pi / n) jpayne@68: self.pu() jpayne@68: self.fd(rad) jpayne@68: self.pd() jpayne@68: self.rt(180 - (90 * (n - 2) / n)) jpayne@68: for i in range(n): jpayne@68: self.fractal(edge, lev, dir) jpayne@68: self.rt(360 / n) jpayne@68: self.lt(180 - (90 * (n - 2) / n)) jpayne@68: self.pu() jpayne@68: self.bk(rad) jpayne@68: self.pd() jpayne@68: jpayne@68: # p. 146 jpayne@68: def fractal(self, dist, depth, dir): jpayne@68: if depth < 1: jpayne@68: self.fd(dist) jpayne@68: return jpayne@68: self.fractal(dist / 3, depth - 1, dir) jpayne@68: self.lt(60 * dir) jpayne@68: self.fractal(dist / 3, depth - 1, dir) jpayne@68: self.rt(120 * dir) jpayne@68: self.fractal(dist / 3, depth - 1, dir) jpayne@68: self.lt(60 * dir) jpayne@68: self.fractal(dist / 3, depth - 1, dir) jpayne@68: jpayne@68: def main(): jpayne@68: ft = CurvesTurtle() jpayne@68: jpayne@68: ft.reset() jpayne@68: ft.speed(0) jpayne@68: ft.ht() jpayne@68: ft.getscreen().tracer(1,0) jpayne@68: ft.pu() jpayne@68: jpayne@68: size = 6 jpayne@68: ft.setpos(-33*size, -32*size) jpayne@68: ft.pd() jpayne@68: jpayne@68: ta=clock() jpayne@68: ft.fillcolor("red") jpayne@68: ft.begin_fill() jpayne@68: ft.fd(size) jpayne@68: jpayne@68: ft.hilbert(size, 6, 1) jpayne@68: jpayne@68: # frame jpayne@68: ft.fd(size) jpayne@68: for i in range(3): jpayne@68: ft.lt(90) jpayne@68: ft.fd(size*(64+i%2)) jpayne@68: ft.pu() jpayne@68: for i in range(2): jpayne@68: ft.fd(size) jpayne@68: ft.rt(90) jpayne@68: ft.pd() jpayne@68: for i in range(4): jpayne@68: ft.fd(size*(66+i%2)) jpayne@68: ft.rt(90) jpayne@68: ft.end_fill() jpayne@68: tb=clock() jpayne@68: res = "Hilbert: %.2fsec. " % (tb-ta) jpayne@68: jpayne@68: sleep(3) jpayne@68: jpayne@68: ft.reset() jpayne@68: ft.speed(0) jpayne@68: ft.ht() jpayne@68: ft.getscreen().tracer(1,0) jpayne@68: jpayne@68: ta=clock() jpayne@68: ft.color("black", "blue") jpayne@68: ft.begin_fill() jpayne@68: ft.fractalgon(3, 250, 4, 1) jpayne@68: ft.end_fill() jpayne@68: ft.begin_fill() jpayne@68: ft.color("red") jpayne@68: ft.fractalgon(3, 200, 4, -1) jpayne@68: ft.end_fill() jpayne@68: tb=clock() jpayne@68: res += "Koch: %.2fsec." % (tb-ta) jpayne@68: return res jpayne@68: jpayne@68: if __name__ == '__main__': jpayne@68: msg = main() jpayne@68: print(msg) jpayne@68: mainloop()