Created
December 15, 2022 08:02
-
-
Save stephengruppetta/22695bb919e2587ac9ca1dc7bcb01427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import math | |
WIDTH, HEIGHT = 1200, 1200 | |
def generate_thue_morse_sequence(n): | |
value = 0 | |
for item in range(n): | |
if (len(bin(item ^ (item - 1))) - 2) % 2: | |
value = 1 - value | |
yield value | |
def draw_koch_snowflake(n_iterations, colour="white"): | |
koch.color(colour) | |
def instruction_0(): | |
koch.left(180) | |
def instruction_1(): | |
koch.forward(WIDTH / (3**n_iterations)) | |
koch.left(60) | |
instructions = { | |
0: instruction_0, | |
1: instruction_1, | |
} | |
for idx in range(3): | |
sequence = generate_thue_morse_sequence(2 ** (n_iterations * 2 - 1)) | |
# print(list(sequence)) | |
for instruction in sequence: | |
instructions[instruction]() | |
window.update() | |
if __name__ == "__main__": | |
window = turtle.Screen() | |
window.bgcolor(0.2, 0.2, 0.2) | |
window.setup(WIDTH, HEIGHT) | |
window.tracer(0) | |
koch = turtle.Turtle() | |
koch.pensize(2) | |
koch.penup() | |
koch.setposition((WIDTH / 6), -(WIDTH / 6) * math.sin(math.pi / 6)) | |
koch.hideturtle() | |
koch.pendown() | |
draw_koch_snowflake(1) | |
draw_koch_snowflake(2, "light green") | |
draw_koch_snowflake(3, "turquoise") | |
draw_koch_snowflake(4, "firebrick") | |
draw_koch_snowflake(5, "dark salmon") | |
turtle.done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment