-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathflake.py
executable file
·170 lines (148 loc) · 6.7 KB
/
flake.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
import math, io
class Rectangle(object):
def __init__(self):
self.minX = 0
self.maxX = 0
self.minY = 0
self.maxY = 0
def iterate_petal(length, angle, limit = 4, depth = 0): # type: (int, float, int, int)->list[tuple[float, float]]
nodes = []
if depth >= limit:
return [(length * math.cos(angle), length * math.sin(angle))]
delta = length / 3.0
nodes += iterate_petal(length=delta, angle=angle, depth=depth + 1, limit=limit)
nodes += iterate_petal(length=delta, angle=angle + math.pi / 3, depth=depth + 1, limit=limit)
nodes += iterate_petal(length=delta, angle=angle - math.pi / 3, depth=depth + 1, limit=limit)
nodes += iterate_petal(length=delta, angle=angle, depth=depth + 1, limit=limit)
return nodes
def iterate_petal_square(length, angle, limit = 4, depth = 0): # type: (int, float, int, int)->list[tuple[float, float]]
nodes = []
if depth >= limit:
return [(length * math.cos(angle), length * math.sin(angle))]
delta = length / 3.0
nodes += iterate_petal_square(length=delta, angle=angle, depth=depth + 1, limit=limit)
nodes += iterate_petal_square(length=delta, angle=angle + math.pi / 2, depth=depth + 1, limit=limit)
nodes += iterate_petal_square(length=delta, angle=angle, depth=depth + 1, limit=limit)
nodes += iterate_petal_square(length=delta, angle=angle - math.pi / 2, depth=depth + 1, limit=limit)
nodes += iterate_petal_square(length=delta, angle=angle, depth=depth + 1, limit=limit)
return nodes
def generate_flake_square(options):
dimension = options.dimension
rotation = options.rotation / 180 * math.pi
posX, posY = 0, 0
rect = Rectangle()
rect.minX, rect.minY, rect.maxX, rect.maxY = 0, 0, 0, 0
path = io.StringIO()
for _ in range(4):
points = iterate_petal_square(length=dimension, angle=rotation, limit=options.level)
for p in points: # type: tuple[float, float]
path.write(' l{:.2f} {:.2f}'.format(*p))
posX += p[0]
posY += p[1]
if posX > rect.maxX: rect.maxX = posX
if posX < rect.minX: rect.minX = posX
if posY > rect.maxY: rect.maxY = posY
if posY < rect.minY: rect.minY = posY
rotation -= math.pi / 2
path.write(' z')
path.seek(0)
return rect, path.read()
def generate_flake(options):
dimension = options.dimension
rotation = options.rotation / 180 * math.pi
posX, posY = 0, 0
rect = Rectangle()
rect.minX, rect.minY, rect.maxX, rect.maxY = 0, 0, 0, 0
path = io.StringIO()
for _ in range(3):
points = iterate_petal_square(length=dimension, angle=rotation, limit=options.level)
for p in points: # type: tuple[float, float]
path.write(' l{:.2f} {:.2f}'.format(*p))
posX += p[0]
posY += p[1]
if posX > rect.maxX: rect.maxX = posX
if posX < rect.minX: rect.minX = posX
if posY > rect.maxY: rect.maxY = posY
if posY < rect.minY: rect.minY = posY
rotation -= math.pi / 3 * 2
path.write(' z')
path.seek(0)
return rect, path.read()
def iterate_leaves(length, angle, move=(0, 0), limit = 4, depth = 0): # type: (int, float, tuple[float, float], int, int)->list[tuple[float, float]]
position = length * math.cos(angle) + move[0], length * math.sin(angle) + move[1]
nodes = [(move, position)]
if depth >= limit: return nodes
leaf_length = length * 0.7
nodes += iterate_leaves(length=leaf_length, angle=angle - math.pi / 5.8, limit=limit, depth=depth + 1, move=position)
nodes += iterate_leaves(length=leaf_length, angle=angle + math.pi / 5.8, limit=limit, depth=depth + 1, move=position)
return nodes
def iterate_eggs(length, angle, limit = 4, depth = 0): # type: (int, float, int, int)->list[tuple[float, float]]
nodes = []
if depth >= limit:
return [(length * math.cos(angle), length * math.sin(angle))]
nodes += iterate_leaves(length=length, angle=angle - 30 / 180.0 * math.pi, limit=limit, depth=depth + 1)
nodes += iterate_leaves(length=length, angle=angle + 30 / 180.0 * math.pi, limit=limit, depth=depth + 1)
return nodes
def generate_eggs(options):
dimension = options.dimension
rotation = options.rotation / 180 * math.pi
posX, posY = 0, 0
rect = Rectangle()
rect.minX, rect.minY, rect.maxX, rect.maxY = 0, 0, 0, 0
path = io.StringIO()
for _ in range(3):
points = iterate_leaves(length=dimension, angle=rotation, limit=options.level)
for p in points: # type: tuple[float, float]
path.write(' l{:.2f} {:.2f}'.format(*p))
posX += p[0]
posY += p[1]
if posX > rect.maxX: rect.maxX = posX
if posX < rect.minX: rect.minX = posX
if posY > rect.maxY: rect.maxY = posY
if posY < rect.minY: rect.minY = posY
rotation -= math.pi / 3 * 2
path.write(' z')
path.seek(0)
return rect, path.read()
def generate_tree(options):
dimension = options.dimension
rotation = options.rotation / 180 * math.pi
rect = Rectangle()
rect.minX, rect.minY, rect.maxX, rect.maxY = 0, 0, 0, 0
path = io.StringIO()
nodes = iterate_leaves(length=dimension, angle=rotation, limit=options.level)
for m, p in nodes: # type: tuple[float, float]
path.write(' M{:.2f} {:.2f}'.format(*m))
path.write(' L{:.2f} {:.2f}'.format(*p))
posX = p[0]
posY = p[1]
if posX > rect.maxX: rect.maxX = posX
if posX < rect.minX: rect.minX = posX
if posY > rect.maxY: rect.maxY = posY
if posY < rect.minY: rect.minY = posY
# path.write(' z')
path.seek(0)
return rect, path.read()
def main():
import argparse, sys
arguments = argparse.ArgumentParser()
arguments.add_argument('--dimension', '-d', default=1000, type=int)
arguments.add_argument('--rotation', '-r', default=0, type=float)
arguments.add_argument('--margin', '-m', default=4, type=float)
arguments.add_argument('--level', '-l', default=4, type=int)
options = arguments.parse_args(sys.argv[1:])
margin = options.margin
rect, path = generate_tree(options=options)
buf = io.StringIO()
buf.write('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="{:.2f} {:.2f} {:.2f} {:.2f}">\n'.format(
rect.minX-margin, rect.minY-margin, rect.maxX - rect.minX + 2 * margin, rect.maxY - rect.minY + 2 * margin
))
buf.write('<path style="stroke:rgb(0,0,0);stroke-width:2;fill:none" d="')
buf.write('M{:.2f} {:.2f}'.format(0, 0))
buf.write(path)
buf.write('" />\n</svg>\n')
buf.seek(0)
print(buf.read())
if __name__ == '__main__':
main()