diff --git a/modules/core/src/main/java/org/locationtech/jts/geom/LineSegment.java b/modules/core/src/main/java/org/locationtech/jts/geom/LineSegment.java index 4a608f1bb1..2703a6000a 100644 --- a/modules/core/src/main/java/org/locationtech/jts/geom/LineSegment.java +++ b/modules/core/src/main/java/org/locationtech/jts/geom/LineSegment.java @@ -504,17 +504,32 @@ public Coordinate reflect(Coordinate p) { * @return a Coordinate which is the closest point on the line segment to the point p */ public Coordinate closestPoint(Coordinate p) + { + return closestPoint(p, false); + } + + /** + * Computes the closest point on this line segment to another point. + * @param p the point to find the closest point to + * @param skip0 If true, do not check p0. Client will have determined this is redundant. + * @return a Coordinate which is the closest point on the line segment to the point p + */ + public Coordinate closestPoint(Coordinate p, boolean skip0) { double factor = projectionFactor(p); if (factor > 0 && factor < 1) { return project(p, factor); } + if (skip0) { + return p1; + } double dist0 = p0.distance(p); double dist1 = p1.distance(p); if (dist0 < dist1) return p0; return p1; } + /** * Computes the closest points on two line segments. * diff --git a/modules/core/src/main/java/org/locationtech/jts/operation/distance/DistanceOp.java b/modules/core/src/main/java/org/locationtech/jts/operation/distance/DistanceOp.java index 34542df972..e396ee8856 100644 --- a/modules/core/src/main/java/org/locationtech/jts/operation/distance/DistanceOp.java +++ b/modules/core/src/main/java/org/locationtech/jts/operation/distance/DistanceOp.java @@ -428,7 +428,7 @@ private void computeMinDistance(LineString line, Point pt, if (dist < minDistance) { minDistance = dist; LineSegment seg = new LineSegment(coord0[i], coord0[i + 1]); - Coordinate segClosestPoint = seg.closestPoint(coord); + Coordinate segClosestPoint = seg.closestPoint(coord, i > 0); locGeom[0] = new GeometryLocation(line, i, segClosestPoint); locGeom[1] = new GeometryLocation(pt, 0, coord); }