-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
79 lines (63 loc) · 1.73 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
import math
import random
from kivy.utils import get_color_from_hex
# copied from SO
def rotate_point_around_point(cx, cy, angle, p):
s = math.sin(angle);
c = math.cos(angle);
# // translate point back to origin:
p[0] -= cx;
p[1] -= cy;
# // rotate point
xnew = p[0] * c - p[1] * s;
ynew = p[0] * s + p[1] * c;
# // translate point back:
p[0] = xnew + cx;
p[1] = ynew + cy;
return p
def distance(p1, p2):
x1, y1 = p1
x2, y2 = p2
return math.sqrt((x1-x2)**2+(y1-y2)**2)
def random_color(alpha=1):
colors = [
"#33B5E5",
"#0099CC",
"#AA66CC",
"#9933CC",
"#99CC00",
"#669900",
"#FFBB33",
"#FF8800",
"#FF4444",
"#CC0000"
]
colors = [get_color_from_hex(x) for x in colors]
c = random.choice(colors)
c[-1] = alpha
return c
def get_triangle_points(x, y, x2, y2):
d = distance((x, y), (x2, y2))
side = (2*d)/math.sqrt(5)
hs = side/2
return [x2-hs, y2, x2+hs, y2, x, y]
# From Physics Activity (original for Sugar)
def constructTriangleFromLine(p1, p2):
"""
Returns list of ordered pairs describing equilteral triangle around
segment pt1 --> pt2.
"""
halfHeightVector = (0.57735 * (p2[1] - p1[1]), 0.57735 * (p2[0] - p1[0]))
p3 = (p1[0] + halfHeightVector[0], p1[1] - halfHeightVector[1])
p4 = (p1[0] - halfHeightVector[0], p1[1] + halfHeightVector[1])
return [p2[0], p2[1], p3[0], p3[1], p4[0], p4[1]]
def calc_center(points):
""" Calculate the center of a polygon
Return: The center (x,y)
"""
tot_x, tot_y = 0,0
for p in points:
tot_x += p[0]
tot_y += p[1]
n = len(points)
return (tot_x/n, tot_y/n)