Do you want to delete “Recursion: Koch Snowflake” permanently? You will not be able to undo this action.
import math # This recursive (that is, self-calling) function draws # a line with a detour. Each detour is also drawn with # this function, meaning that there can be detours on # detours. This creates a "snowflake" shape known as # the Koch Snowflake or Koch Curve. def koch(coords, direction, length, detours): if detours > 0: # We have detours left to take; we'll draw four sides. # Each koch() call returns its end-point; we save that # value and use it as the start point for the next side. a = koch(coords, direction, length / 3, detours-1) b = koch(a, direction - 1, length / 3, detours-1) c = koch(b, direction + 1, length / 3, detours-1) d = koch(c, direction, length / 3, detours-1) return d else: # No detours left; just draw a straight line. end = ( coords + length * math.cos(direction * math.pi / 3), coords + length * math.sin(direction * math.pi / 3) ) Line([coords, end], color='white') return end # a nice background Rectangle(color='#004') # This is where we draw the actual Koch Snowflake. # Try altering the last argument (the 2) to another # number from 0 to 3, and see what happens! a = koch((10, 27), 0, 80, 2) a = koch(a, 2, 80, 2) a = koch(a, 4, 80, 2)
Created 1 year, 11 months ago.