File size: 572 Bytes
a018ed7
5cd1866
 
a018ed7
5cd1866
 
a018ed7
 
 
 
f048b9a
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np

def _side(p, a, b):
    return np.sign((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]))

def _point_to_segment_dist(px, py, ax, ay, bx, by):
    A = np.array([ax, ay], dtype=float)
    B = np.array([bx, by], dtype=float)
    P = np.array([px, py], dtype=float)
    AB = B - A
    denom = np.dot(AB, AB)
    if denom == 0:                       # degenerate segment: fall back to point distance
        return float(np.linalg.norm(P - A))
    t = np.clip(np.dot(P - A, AB) / denom, 0, 1)
    return float(np.linalg.norm(P - (A + t * AB)))