Spaces:
Sleeping
Sleeping
File size: 703 Bytes
021dff5 | 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 | import math
def calculate_line_signed_distance(p1, p2, centroid):
"""
Calculate signed distance from point to line.
Returns (signed_dist, is_within_segment)
"""
x1, y1 = p1
x2, y2 = p2
cx, cy = centroid
dx = x2 - x1
dy = y2 - y1
line_len_sq = dx * dx + dy * dy
if line_len_sq == 0:
return 0.0, False
t = ((cx - x1) * dx + (cy - y1) * dy) / line_len_sq
margin = 0.1
is_within_segment = -margin <= t <= 1.0 + margin
a = y2 - y1
b = x1 - x2
c = x2 * y1 - x1 * y2
denom = math.hypot(a, b)
signed_dist = (a * cx + b * cy + c) / denom if denom != 0 else 0.0
return signed_dist, is_within_segment |