-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.py
380 lines (305 loc) · 10.2 KB
/
shape.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import svgwrite
import math
CENTER = (250, 250) # Center coordinates of the circle
RADIUS = 100 # Radius of the circle
DOT_RADIUS = 14
O_RADIUS = 22
STROKE_WIDTH = 36
MAIN_BRANCH_START_LENGTH = 0.75 * RADIUS
BRANCH_LENGTH = 20
STROKE_COLOR = "#09711c"
def deg2rad(angle):
return angle * 2 * math.pi / 360
CHOUYA = deg2rad(2) # Small angle needed to really finish the arc
def rad2deg(angle):
return angle / 2 / math.pi * 360
def draw_circle(
dwg, circle_center, circle_radius, stroke_color="#ffd42a", stroke_width=STROKE_WIDTH
):
return dwg.circle(
center=circle_center,
r=circle_radius,
fill="none",
stroke=stroke_color,
stroke_width=stroke_width,
)
def draw_dot(dwg, circle_center, circle_radius=DOT_RADIUS, color=STROKE_COLOR):
return dwg.circle(center=circle_center, r=circle_radius, fill=color, stroke=color)
def move(origin, length, angle):
return (origin[0] + length * math.cos(angle), origin[1] + length * math.sin(angle))
def draw_line(dwg, start_point, end_point, strokeColor=STROKE_COLOR):
"""
Draw a line from a start_point to an end_point
"""
coords = [start_point, end_point]
path_data = f"M {' '.join([f'{x},{y}' for x, y in coords])}"
return dwg.path(
d=path_data, fill="none", stroke=strokeColor, stroke_width=STROKE_WIDTH
)
def draw_arc(dwg, origin, radius, angle, angleStart, strokeColor="#555"):
"""
Draw an arc as a Path. The arc is a portion of a circle of origin 'origin'
and radius 'radius', starting at angleStart and lasting until angle.
"""
coords = []
for a in range(1, round(rad2deg(angle))):
c = move(origin, radius, deg2rad(a) + angleStart)
coords.append(c)
path_data = f"M {' '.join([f'{x},{y}' for x, y in coords])}"
return dwg.path(
d=path_data, fill="none", stroke=strokeColor, stroke_width=STROKE_WIDTH
)
def draw_branch(dwg, origin, angle_main_branch, branch_length):
"""
Draw some branches starting from a origin point, orthogonally from the angle_main_branch
"""
start_point = move(origin, branch_length, angle_main_branch + math.pi / 2)
end_point = move(origin, branch_length, angle_main_branch - math.pi / 2)
line = draw_line(dwg, start_point, end_point)
return line
def convex_arc_radius(convexity, angle_quadrant):
"""
Compute a new radius used to draw a convex arc
"""
return RADIUS * math.sqrt(
(1 - convexity * math.cos(angle_quadrant / 2)) ** 2
+ (convexity * math.sin(angle_quadrant / 2)) ** 2
)
def convex_drawing_angle(convexity, angle_quadrant, convex_radius):
"""
Compute an angle along which the convex arc is drawn
"""
return (
2
* (
math.asin(
convexity * math.sin(angle_quadrant / 2) / (convex_radius / RADIUS)
)
+ angle_quadrant / 2
)
+ CHOUYA
)
def concave_arc_radius(concavity, angle_quadrant):
"""
Compute a new radius used to draw a concave arc
"""
return -RADIUS * math.sqrt(
(1 - 2 * concavity * math.cos(angle_quadrant / 2) + concavity**2)
)
def concave_drawing_angle(concavity, angle_quadrant, concave_radius):
"""
Compute an angle along which the concave arc is drawn
"""
return (
2
* math.acos(
RADIUS * (concavity - math.cos(angle_quadrant / 2)) / concave_radius
)
+ CHOUYA
)
def get_filename(leaved, curvity, n, dots, centre):
if leaved == "broadleaved":
l = "B"
else:
l = "C"
if curvity == "convex":
cu = "+"
elif curvity == "concave":
cu = "-"
else:
cu = ""
if dots is None:
d = ""
else:
d = f"d{len(dots)}"
if centre is None:
c = ""
else:
c = f"{centre}"
if curvity == "circular" and n == 1:
return f"{l}{d}{c}.svg"
else:
return f"{l}{n}{cu}{d}{c}.svg"
def get_exp(n):
if n == 1:
return chr(0x00B9)
elif n == 2:
return chr(0x00B2)
elif n == 3:
return chr(0x00B3)
else:
return ""
def get_symbol_label(leaved, curvity, n, dots, centre):
if leaved == "broadleaved":
l = "B"
else:
l = "C"
if curvity == "convex":
cu = "+"
elif curvity == "concave":
cu = "-"
else:
cu = ""
if dots is None:
d = ""
else:
d = f"d{get_exp(len(dots))}"
if centre is None:
c = ""
else:
c = f"{centre}"
if curvity == "circular" and n == 1:
return f"{l}{d}{c}"
else:
return f"{l}{n}{cu}{d}{c}"
def draw_broadleaved_symbol(
dwg, center, n, convex_or_concave, curvity, dots=None, centroid=None, label=False
):
"""
Draw a broadleaved tree symbol with n convex or concave arc, curvity being a convex
or concave factor. Points can be added, as well as a centroid. Returns the drawing
svgwrite object.
"""
if label:
symbol_label = get_symbol_label("broadleaved", convex_or_concave, n, dots, centroid)
paragraph = dwg.add(
dwg.g(
class_="label",
)
)
paragraph.add(dwg.text(symbol_label, (center[0] - 10, center[1] - 180)))
angle_quadrant = 2 * math.pi / n
has_drawn_circle = False
for i in range(n):
angle_quadrant_i = i * angle_quadrant
if convex_or_concave == "circular":
if not has_drawn_circle:
circle = draw_circle(dwg, center, RADIUS, stroke_color=STROKE_COLOR)
dwg.add(circle)
has_drawn_circle = True
else:
if convex_or_concave == "convex":
radius = convex_arc_radius(curvity, angle_quadrant)
drawing_angle = convex_drawing_angle(curvity, angle_quadrant, radius)
elif convex_or_concave == "concave":
radius = concave_arc_radius(curvity, angle_quadrant)
drawing_angle = concave_drawing_angle(curvity, angle_quadrant, -radius)
origin_t = (
center[0] + math.cos(angle_quadrant_i) * curvity * RADIUS,
center[1] + math.sin(angle_quadrant_i) * curvity * RADIUS,
)
angle_start = angle_quadrant_i - drawing_angle / 2
arc = draw_arc(
dwg, origin_t, radius, drawing_angle, angle_start, STROKE_COLOR
)
dwg.add(arc)
if dots:
for p in dots:
new_origin = move(
center, RADIUS * p, angle_quadrant_i - angle_quadrant / 2
)
dot = draw_dot(dwg, new_origin)
dwg.add(dot)
if centroid:
if centroid == "p":
point = draw_dot(dwg, center)
dwg.add(point)
elif centroid == "o":
circle = draw_circle(dwg, center, O_RADIUS, stroke_color=STROKE_COLOR)
dwg.add(circle)
return dwg
def draw_needleleaved_symbol(dwg, center, n, branches=None, centroid=None, label=False):
"""
Draw a needle-leaved tree symbol with n spikes.
Branches can be added, as well as a centroid. Returns the drawing
svgwrite object.
"""
if label:
symbol_label = get_symbol_label("needleleaved", None, n, branches, centroid)
paragraph = dwg.add(
dwg.g(
class_="label",
)
)
paragraph.add(dwg.text(symbol_label, (center[0] - 10, center[1] - 180)))
circle = draw_circle(dwg, center, RADIUS, stroke_color=STROKE_COLOR)
dwg.add(circle)
angle_quadrant = 2 * math.pi / n
for i in range(n):
angle_quadrant_i = i * angle_quadrant + math.pi / n
# draw main branch
if branches:
main_branch_length = RADIUS * (branches[-1] - 1) + 2 * BRANCH_LENGTH
else:
main_branch_length = MAIN_BRANCH_START_LENGTH
start_point = move(center, RADIUS, angle_quadrant_i)
end_point = move(center, RADIUS + main_branch_length, angle_quadrant_i)
line = draw_line(dwg, start_point, end_point)
dwg.add(line)
# draw sub-branches
if branches:
position = len(branches)
ib = 1
for b in branches:
new_origin = move(center, RADIUS * b, angle_quadrant_i)
branch = draw_branch(
dwg, new_origin, angle_quadrant_i, position * BRANCH_LENGTH
)
dwg.add(branch)
position = position - 1
ib = ib + 1
if centroid:
if centroid == "p":
point = draw_dot(dwg, center)
dwg.add(point)
elif centroid == "o":
circle = draw_circle(dwg, center, O_RADIUS, stroke_color=STROKE_COLOR)
dwg.add(circle)
return dwg
def create_broadleaved_symbol(n, convex_or_concave, curvity, dots=None, centroid=None, label=False):
"""
Create a broadleaved tree symbol with n convex or concave arc, curvity being a convex
or concave factor. Points can be added, as well as a centroid. The pictogram is saved
in svg.
"""
filename = get_filename(
"broadleaved",
convex_or_concave,
n,
dots,
centroid,
)
dwg = svgwrite.Drawing(filename, size=("500", "500"), profile="full")
dwg.embed_font(name="Alfphabet", filename="fonts/Alfphabet-III.otf")
dwg.embed_stylesheet(
"""
.label {
font-family: "Alfphabet";
font-size: 14;
font-weight: bold;
}"""
)
dwg = draw_broadleaved_symbol(
dwg, CENTER, n, convex_or_concave, curvity, dots, centroid, label
)
dwg.save(pretty=True)
def create_needleleaved_symbol(n, branches=None, centroid=None, label=False):
filename = get_filename(
"needleleaved",
"",
n,
branches,
centroid,
)
dwg = svgwrite.Drawing(filename, size=("500", "500"), profile="full")
dwg.embed_font(name="Alfphabet", filename="fonts/Alfphabet-III.otf")
dwg.embed_stylesheet(
"""
.label {
font-family: "Alfphabet";
font-size: 14;
font-weight: bold;
}"""
)
dwg = draw_needleleaved_symbol(dwg, CENTER, n, branches, centroid, label)
dwg.save(pretty=True)