-
Notifications
You must be signed in to change notification settings - Fork 0
/
koch_snowflake.py
executable file
·74 lines (53 loc) · 1.53 KB
/
koch_snowflake.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
#!/usr/bin/env python
"""
simple representation of the Koch snowflake
"""
import sys
import math
import pygame
BLACK = (0, 0, 0)
ANGLE = math.sqrt(3)/6
def grow(edges):
for i in range(0, len(edges), 2):
beg = edges[i]
end = edges[i+1]
# splits in 4 edges, like this : _/\_
# so, there are 3 new nodes
mid = ((end[0] + beg[0]) / 2, (end[1] + beg[1]) / 2)
node1 = ((end[0] + beg[0]) * 1/3, (end[1] + beg[1]) * 1/3)
node2 = (mid[0] - (end[1] - beg[1]) * ANGLE, mid[1] + (end[0] - beg[0]) * ANGLE)
node3 = ((end[0] + beg[0]) * 2/3, (end[1] + beg[1]) * 2/3)
edges.insert(i+1, node3)
edges.insert(i+1, node2)
edges.insert(i+1, node1)
def display(edges, screen):
for edge in edges:
pygame.draw.rect(screen,
BLACK,
[edge[0], edge[1], 1, 1])
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
def main(args):
"""
main loop
"""
try:
steps = int(args[1])
except IndexError:
steps = 10
edges = [] # paires of coords
edges.append((0., 0.))
edges.append((1000., 0.))
pygame.init()
screen = pygame.display.set_mode([1000, 1000])
pygame.display.set_caption("Koch Snowflake")
screen.fill((255, 255, 255))
for _ in range(steps):
grow(edges)
display(edges, screen)
for i in edges:
print(i)
if __name__ == "__main__":
main(sys.argv)