I’m trying to find the distance between a point and a given line segment.
I’m working on line simplification algorithms. I chose the Ramer–Douglas–Peucker algorithm. I found this Python implementation but I didn’t understand how they calculate the distance between a point and a line segment in the pldist
function. How does this function work?
def pldist(point, start, end):
"""
Calculates the distance from ``point`` to the line given
by the points ``start`` and ``end``.
:param point: a point
:type point: numpy array
:param start: a point of the line
:type start: numpy array
:param end: another point of the line
:type end: numpy array
"""
if np.all(np.equal(start, end)):
return np.linalg.norm(point - start)
return np.divide(
np.abs(np.linalg.norm(np.cross(end - start, start - point))),
np.linalg.norm(end - start))
New contributor
user25018201 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2