-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.py
182 lines (148 loc) · 5 KB
/
svg.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
import json
import sys
import os
import math
size = 800
precedence = {
"Circle": 0,
"Polygon": 1,
"Angle": 2,
"Line": 3,
"Point": 4
}
def to_hex(color):
red = int(color[0]*255)
green = int(color[1]*255)
blue = int(color[2]*255)
return '#{r:02x}{g:02x}{b:02x}'.format(r=red,g=green,b=blue)
def coordinate(val):
"""Converts [-1,1] to a real coordinate"""
return int(size/2*val) + size/2
def lerp(x1, y1, x2, y2, t):
return x1 + t*(x2-x1), y1+t*(y2-y1)
def dist(x1, y1, x2, y2):
xs = (x1-x2)*(x1-x2)
ys = (y1-y2)*(y1-y2)
return math.sqrt(xs+ys)
def circumcircle(geometry, g):
p1 = geometry[g['data']['p1']]['data']
p2 = geometry[g['data']['p2']]['data']
p3 = geometry[g['data']['p3']]['data']
m1 = (p1['y']-p2['y'])/(p1['x']-p2['x'])
m2 = (p3['y']-p2['y'])/(p3['x']-p2['x'])
m1 = m1 if m1 != 0.0 else 10000000000000
m2 = m2 if m2 != 0.0 else 10000000000000
ma = -1.0*(1.0/m1)
mb = -1.0*(1.0/m2)
xa = 0.5*(p1['x']+p2['x'])
ya = 0.5*(p1['y']+p2['y'])
xb = 0.5*(p2['x']+p3['x'])
yb = 0.5*(p2['y']+p3['y'])
x = ((ma*xa)-(mb*xb)+yb-ya)/(ma-mb)
y = (mb*(x-xb))+yb
center = dict(data=dict(x=x, y=y))
radius = dist(x, y, p1['x'], p1['y'])
return center, radius
def point(g):
point = '<circle cx="{0}" cy="{1}" r="{2}" fill="{3}"/>'
x = coordinate(g['data']['x'])
y = coordinate(g['data']['y'])
color = to_hex(g['color'])
text = '<text x="{0}" y="{1}" font-family="Verdana" font-size="20">{2}</text>'
return point.format(x, y, size/100, color) + text.format(x+10, y-10, g['id'])
def circle(geometry, g):
text = '<circle cx="{0}" cy="{1}" r="{2}" stroke="{3}" fill="{3}" fill-opacity="0" stroke-width="5"/>'
radius = g['data']['radius']
c = None
if radius is None:
c, radius = circumcircle(geometry, g)
else:
c = geometry[g['data']['center']]
r = int(size/2 * radius)
color = to_hex(g['color'])
x = coordinate(c['data']['x'])
y = coordinate(c['data']['y'])
return text.format(x, y, r, color)
def line(geometry, g):
text = '<line x1="{0}" y1="{1}" x2="{2}" y2="{3}" stroke="{4}" stroke-width="5"/>'
color = to_hex(g['color'])
c = geometry[g['data']['p1']]
x1 = coordinate(c['data']['x'])
y1 = coordinate(c['data']['y'])
c = geometry[g['data']['p2']]
x2 = coordinate(c['data']['x'])
y2 = coordinate(c['data']['y'])
return text.format(x1, y1, x2, y2, color)
def polygon(geometry, g):
polygon = '<polygon points="{0}" stroke="{1}" stroke-width="5" fill="{1}" fill-opacity="0.2"/>'
color = to_hex(g['color'])
points = g['data']['points']
text = ""
for i in range(len(points)):
p = geometry[points[i]]
x = coordinate(p['data']['x'])
y = coordinate(p['data']['y'])
text += "{0},{1} ".format(x,y)
return polygon.format(text, color)
def angle(geometry, g):
path = '<path d="{0}" stroke="{1}" stroke-width="5" fill="none"/>'
inner = 'M {sx} {sy} A {r} {r} 0 {large} {sweep} {ex} {ey}'
color = to_hex(g['color'])
p1, p2, p3 = [geometry[x]['data'] for x in g['data']['points']]
cx = coordinate(p2['x'])
cy = coordinate(p2['y'])
x1 = coordinate(p3['x'])
y1 = coordinate(p3['y'])
x2 = coordinate(p1['x'])
y2 = coordinate(p1['y'])
d1 = dist(cx, cy, x1, y1)
d2 = dist(cx, cy, x2, y2)
radius = min(d1/3, d2/3)
sweep = 1
if d1 > d2:
x1, x2, y1, y2 = x2, x1, y2, y1
sweep = 0
d1, d2 = d2, d1
sx, sy = lerp(x1, y1, cx, cy, 0.7)
ds = dist(cx, cy, sx, sy)
ex = cx + (ds/d2)*(x2-cx)
ey = cy + (ds/d2)*(y2-cy)
d = dict(sx=sx, sy=sy, r=radius, large=0, sweep=sweep, ex=ex, ey=ey)
return path.format(inner.format(**d), color)
def get_svg(geometry, to_draw):
svg = '<svg width="{0}" height="{1}" xmlns="http://www.w3.org/2000/svg">'.format(size, size) + '{0}</svg>'
shapes = ""
to_draw.sort(key=lambda g: precedence[geometry[g]['type']])
for name in to_draw:
g = geometry[name]
if g["type"] == "Point":
shapes += point(g)
elif g["type"] == "Circle":
shapes += circle(geometry, g)
elif g["type"] == "Line":
shapes += line(geometry, g)
elif g["type"] == "Polygon":
shapes += polygon(geometry, g)
elif g["type"] == "Angle":
shapes += angle(geometry, g)
return svg.format(shapes)
def write(name, svg):
with open(name, "w") as f:
f.write(svg)
def main(fname):
f = open(fname, "r")
text = f.read()
f.close
name = fname.split(".")[0]
os.makedirs(name, exist_ok=True)
geo = json.loads(text)
svg = ""
for i, step in enumerate(geo["animations"]):
svg = get_svg(geo["geometry"], step)
write("{0}/{0}_{1}.svg".format(name, i), svg)
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Please give json files as args")
exit()
for i in range(1, len(sys.argv)):
main(sys.argv[i])