This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoch_curve.py
82 lines (74 loc) · 1.65 KB
/
koch_curve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from turtle import *
from random import choice
def setup(
*, drawing_speed: int = 0, backward_distance: int = 400, pen_width: int = 2
) -> None:
speed(drawing_speed)
pensize(pen_width)
penup()
backward(backward_distance)
pendown()
def compute_and_draw_koch_curve(
*,
side_length: int | float = 16,
level: int = 4,
angle: int | float = 70,
colors: list[str] = [
"red",
"green",
"blue",
"orange",
"purple",
"brown",
"black",
"lime",
"gray",
"magenta",
"teal",
"olive",
"turquoise",
"cyan",
"olive",
"sienna",
"violet",
]
) -> None:
if level == 0:
current_color: str = choice(colors)
pencolor(current_color)
forward(side_length)
return
compute_and_draw_koch_curve(
side_length=side_length,
level=level - 1,
angle=angle,
colors=colors,
)
left(angle)
compute_and_draw_koch_curve(
side_length=side_length,
level=level - 1,
angle=angle,
colors=colors,
)
right(2 * angle)
compute_and_draw_koch_curve(
side_length=side_length,
level=level - 1,
angle=angle,
colors=colors,
)
left(angle)
compute_and_draw_koch_curve(
side_length=side_length, level=level - 1, angle=angle, colors=colors
)
def main(*args, **kwargs) -> None:
"""
Drawing a Koch curve using turtle graphics.
https://en.wikipedia.org/wiki/L-system
"""
setup()
compute_and_draw_koch_curve()
mainloop()
if __name__ == "__main__":
main()