-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
84 lines (83 loc) · 2.42 KB
/
utils.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
from math import *
rotatez = lambda angle:(
(cos(angle),-sin(angle),0),
(sin(angle), cos(angle),0),
( 0 ,0, 1 )
)
rotatey = lambda angle:(
(cos(angle) ,0,-sin(angle)),
( 0 ,1, 0 ),
(-sin(angle),0,cos(angle))
)
rotatex = lambda angle:(
(1,0,0),
(0,cos(angle),-sin(angle)),
(0,sin(angle),cos(angle))
)
def matmul(X,Y):
result = []
for i in range(len(X)):
ls=[]
for j in range(len(Y[0])):
out=0
for k in range(len(Y)):
out += X[i][k] * Y[k][j]
ls.append(out)
result.append(ls)
return tuple(tuple(i) for i in result)
def into2d(point):
if len(point)==2:
return point
return list(point)[:-1]
def rotatedpoint(angle,point):
return matmul(rotatey(angle),((i,)for i in point))
def percentopxs(pos,max_h,max_w):
# X-axis Y-axis
return max_h*pos[0]/100,max_w*pos[1]/100
def percentopx(v,mx):
return mx*v/100
# Distance Formula[sqrt((x2-x1)^2 + (y2-y1)^2)]
dform = lambda p1,p2:((p2[0]-p1[0])**2+(p2[1]-p1[1])**2)**(1/2)
def increase(p1,p2,size):
d = dform(p1,p2)
bx,by = p2
bx +=((bx- p1[0])/d)* size;
by +=((by- p1[1])/d)* size;
return bx,by
style2dict = lambda style:\
{
part[0].strip():part[1]
for part in [
part.split(":")
for part in style.split(";")
if part.strip()
]
}
dict2style = lambda dic:\
";".join(
map(
lambda c:'{0}:{1}'.format(c[0],c[1]),
dic.items()
)
)+';'
def centerer(center,ls):
x,y = center
l1,l2 =ls
l1/=2
l2/=2
return (x-l1,y-l2),(x+l1,y+l2)
from itertools import zip_longest as zip
def combineGen(gens):
for gen in zip(*gens):
d1=gen[0].attribs
style={}
if 'style' in d1:
style.update(style2dict(d1['style']))
for d2 in gen[1:]:
if d2:#Not None
n_attribs=d2.attribs
if 'style' in n_attribs:
style.update(style2dict(n_attribs.pop('style')))
d1.update(n_attribs)
d1['style']=dict2style(style)
yield gen[0]