-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.py
More file actions
48 lines (42 loc) · 1.55 KB
/
Copy pathgeometry.py
File metadata and controls
48 lines (42 loc) · 1.55 KB
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
import math
def points_within_dist(x1, y1, x2, y2, dist):
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) <= dist*dist
def rotate_point(x, y, cx, cy, angle_deg):
"""Rotate point (x, y) around center (cx, cy) by angle in degrees (clockwise)."""
angle_rad = math.radians(angle_deg)
cos_a = math.cos(angle_rad)
sin_a = math.sin(angle_rad)
dx = x - cx
dy = y - cy
rx = cx + dx * cos_a - dy * sin_a
ry = cy + dx * sin_a + dy * cos_a
return rx, ry
def connection_point(x, y, width, height, angle_deg, conn_x, conn_y):
"""
Compute location of the rotated connection point of a shape.
Parameters:
x, y, width, height: (unrotated) bounding box of the shape
angle_deg: rotation in degrees (positive = clockwise)
conn_y, conn_y: relative connection point (0 to 1)
Returns:
(x, y): absolute coordinates of rotated connection point
"""
# compute unrotated connection point:
px = x + conn_x * width
py = y + conn_y * height
# center of bounding box:
cx = x + width / 2
cy = y + height / 2
return rotate_point(px, py, cx, cy, angle_deg)
def triangle_tip(x, y, width, height, angle_deg):
"""
Compute location of the tip of unrotated right-pointing triangle after rotation,
along with a "slack" distance within which a connection would be considered close enough.
"""
# unrotated tip:
ax = x + width
ay = y + height / 2
# center of bounding box
cx = x + width / 2
cy = y + height / 2
return rotate_point(ax, ay, cx, cy, angle_deg), height / 16