Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def dist_segment_to_point(Ax, Ay, Bx, By, Cx, Cy):
- """
- return the distance of C
- to the segment [A, B].
- A, B, C are given by Ax, Ay, Bx, By, Cx, Cy
- """
- px, py = Bx - Ax, By - Ay
- dd = px*px + py*py # dd <- |AB|^2
- if dd == 0:
- # A == B, set P:=A
- Px, Py = Ax, Ay
- else:
- u = ((Cx - Ax) * px + (Cy - Ay) * py) # u = AC . AB
- if u < 0:
- # A is the AB segment's closest point to C
- Px, Py = Ax, Ay
- elif u >= dd:
- # B is the AB segment's closest point to C
- Px, Py = Bx, By
- else:
- # P is between points A and B
- u = u/float(dd)
- Px, Py = Ax + u * px, Ay + u * py
- dx, dy = (Cx - Px, Cy - Py)
- return math.sqrt(dx*dx + dy*dy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement