48 lines
1001 B
Python
48 lines
1001 B
Python
#!/usr/bin/env python
|
|
import math
|
|
|
|
|
|
def angle_to_vector(angle):
|
|
angle = angle * math.pi / 180
|
|
return [math.cos(angle), math.sin(angle)]
|
|
|
|
|
|
def get_line_feats(point1, point2):
|
|
x1, y1 = point1
|
|
x2, y2 = point2
|
|
|
|
# if x1 == x2 :
|
|
# x1=x1+1
|
|
a = (y1 - y2) / (x1 - x2)
|
|
b = y2 - a * x2
|
|
return a, b
|
|
|
|
|
|
def segments_intersection(line1, line2):
|
|
p1, p2 = line1
|
|
p3, p4 = line2
|
|
if p1[0] == p2[0]:
|
|
p1 = (p1[0] + 1, p1[1])
|
|
if p3[0] == p4[0]:
|
|
p3 = (p3[0] + 1, p3[1])
|
|
|
|
a1, b1 = get_line_feats(p1, p2)
|
|
a2, b2 = get_line_feats(p3, p4)
|
|
|
|
if a1 == a2:
|
|
return None # parrallel lines
|
|
|
|
x = (b2 - b1) / (a1 - a2)
|
|
|
|
if min(p1[0], p2[0]) <= x <= max(p1[0], p2[0]) and min(p3[0], p4[0]) <= x <= max(
|
|
p3[0], p4[0]
|
|
):
|
|
y = a1 * x + b1
|
|
return x, y
|
|
else:
|
|
return None # intersect is outside segments
|
|
|
|
|
|
def distance(point1, point2):
|
|
return math.hypot(point1[0] - point2[0], point1[1] - point2[1])
|