Skip to content

Instantly share code, notes, and snippets.

@nim65s
Created January 5, 2015 12:57
Show Gist options
  • Select an option

  • Save nim65s/5e9902cd67f094ce65b0 to your computer and use it in GitHub Desktop.

Select an option

Save nim65s/5e9902cd67f094ce65b0 to your computer and use it in GitHub Desktop.

Revisions

  1. Guilhem Saurel created this gist Jan 5, 2015.
    12 changes: 12 additions & 0 deletions gistfile1.numpy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    from numpy import arccos, array, dot, pi
    from numpy.linalg import det, norm

    def distance(A, B, P):
    """ segment line AB, point P, where each one is an array([x, y]) """
    if all(A == P) or all(B == P):
    return 0
    if arccos(dot((P - A) / norm(P - A), (B - A) / norm(B - A))) > pi / 2:
    return norm(P - A)
    if arccos(dot((P - B) / norm(P - B), (A - B) / norm(A - B))) > pi / 2:
    return norm(P - B)
    return abs(dot(A - B, P[::-1]) + det([A, B])) / norm(A - B)