Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import math
  2.  
  3. def dist_segment_to_point(Ax, Ay, Bx, By, Cx, Cy):
  4. """
  5. return the distance of C
  6. to the segment [A, B].
  7.  
  8. A, B, C are given by Ax, Ay, Bx, By, Cx, Cy
  9. """
  10.  
  11. px, py = Bx - Ax, By - Ay
  12.  
  13. dd = px*px + py*py # dd <- |AB|^2
  14.  
  15. if dd == 0:
  16. # A == B, set P:=A
  17. Px, Py = Ax, Ay
  18. else:
  19. u = ((Cx - Ax) * px + (Cy - Ay) * py) # u = AC . AB
  20.  
  21. if u < 0:
  22. # A is the AB segment's closest point to C
  23. Px, Py = Ax, Ay
  24. elif u >= dd:
  25. # B is the AB segment's closest point to C
  26. Px, Py = Bx, By
  27. else:
  28. # P is between points A and B
  29. u = u/float(dd)
  30. Px, Py = Ax + u * px, Ay + u * py
  31.  
  32. dx, dy = (Cx - Px, Cy - Py)
  33.  
  34. return math.sqrt(dx*dx + dy*dy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement