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