Skip to content

Commit

Permalink
copy the dist_point_to_segment fxn from mpl v2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
phobson committed Jun 12, 2019
1 parent 7fe963f commit 6594370
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pygridgen/boundary_interactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,39 @@
from matplotlib.artist import Artist
from matplotlib.patches import Polygon, CirclePolygon
from matplotlib.lines import Line2D
from matplotlib.mlab import dist_point_to_segment

from .grid import Gridgen


__docformat__ = "restructuredtext en"


def dist_point_to_segment(p, s0, s1):
"""
Get the distance of a point to a segment.
*p*, *s0*, *s1* are *xy* sequences
This algorithm from
http://geomalgorithms.com/a02-_lines.html
"""
p = np.asarray(p, float)
s0 = np.asarray(s0, float)
s1 = np.asarray(s1, float)
v = s1 - s0
w = p - s0

c1 = np.dot(w, v)
if c1 <= 0:
return dist(p, s0)

c2 = np.dot(v, v)
if c2 <= c1:
return numpy.hypot(p, s1)

b = c1 / c2
pb = s0 + b * v
return numpy.hypot(p, pb)


class BoundaryInteractor(object): # pragma: no cover
"""
Interactive grid creation
Expand Down

0 comments on commit 6594370

Please sign in to comment.