forked from dmishin/fft-image-experiments
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgosper.py
123 lines (96 loc) · 2.53 KB
/
gosper.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import numpy as np
from matplotlib import pyplot as pp
from math import pi, sqrt
class Turtle:
def __init__(self, angle_step):
self.angle_step = angle_step
self.direction = 0
self.angles = []
self.directions = []
def left(self):
self.direction -= 1
def right(self):
self.direction += 1
def forward(self):
self.angles.append(self.direction)
self.directions.append(1)
def backward(self):
self.angles.append(self.direction)
self.directions.append(-1)
def produce(self):
a = np.array(self.angles) * self.angle_step
d = np.array(self.directions)
dx = np.cos(a) * d
dy = np.sin(a) * d
return np.cumsum(dx), np.cumsum(dy)
def gosper(level):
g = Gosper()
g.A(level)
return g.produce()
class Gosper:
"""L-system is:
A -> A-B--B+A++AA+B-
B -> +A-BB--B-A++A+B
"""
def __init__(self):
self.turtle = Turtle(pi / 3)
def produce(self):
return self.turtle.produce()
def A(self, level):
"""A -> A-B--B+A++AA+B-"""
if level == 0:
self.turtle.forward()
else:
t = self.turtle
level -= 1
self.A(level)
t.left()
self.B(level)
t.left()
t.left()
self.B(level)
t.right()
self.A(level)
t.right()
t.right()
self.A(level)
self.A(level)
t.right()
self.B(level)
t.left()
def B(self, level):
"""+A-BB--B-A++A+B"""
if level == 0:
self.turtle.forward()
else:
t = self.turtle
level -= 1
t.right()
self.A(level)
t.left()
self.B(level)
self.B(level)
t.left()
t.left()
self.B(level)
t.left()
self.A(level)
t.right()
t.right()
self.A(level)
t.right()
self.B(level)
def gosper_diagram(size, order, scale):
from PIL import Image, ImageDraw
xx, yy = gosper(order)
xx *= scale
yy *= scale
xx += int(size / 2 - xx.mean())
yy += int(size / 2 - yy.mean())
image = Image.new("L", (size, size))
draw = ImageDraw.Draw(image)
draw.line(list(zip(xx, yy)), fill=255)
# image.show()
return np.asarray(image)
if __name__ == "__main__":
print(gosper_diagram(512, 5, 4 * sqrt(1.5)))