@Test public void testNearestPointOnLineSegment() { final IVector2 l1p1 = Vector2.fromXY(-0.8, 0); final IVector2 l1p2 = Vector2.fromXY(0.8, 0); for (double i = 0; i < 10; i += 0.001) { final IVector2 point = Vector2.fromXY(SumatraMath.cos(i), SumatraMath.sin(i)); ILine line = Line.fromPoints(l1p1, l1p2); final IVector2 result = LineMath.nearestPointOnLineSegment(line, point); IVector2 expected; if ((Math.abs(point.x()) - TEST_ACCURACY) > 0.8) { expected = point.x() < 0 ? l1p1 : l1p2; } else { expected = Vector2.fromXY(point.x(), 0); } Assert.assertTrue(result.isCloseTo(expected, TEST_ACCURACY)); } }
public static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point) { final double lambda = getLeadPointLambda(point, line); if (isLambdaInRange(lambda, 0, 1)) { return getPointOnLineForLambda(line, lambda); } final double dist1 = VectorMath.distancePPSqr(line.getStart(), point); final double dist2 = VectorMath.distancePPSqr(line.getEnd(), point); return Vector2.copy(dist1 < dist2 ? line.getStart() : line.getEnd()); }
LineMath { public static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point) { final double lambda = getLeadPointLambda(point, line); if (isLambdaInRange(lambda, 0, 1)) { return getPointOnLineForLambda(line, lambda); } final double dist1 = VectorMath.distancePPSqr(line.getStart(), point); final double dist2 = VectorMath.distancePPSqr(line.getEnd(), point); return Vector2.copy(dist1 < dist2 ? line.getStart() : line.getEnd()); } }
LineMath { public static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point) { final double lambda = getLeadPointLambda(point, line); if (isLambdaInRange(lambda, 0, 1)) { return getPointOnLineForLambda(line, lambda); } final double dist1 = VectorMath.distancePPSqr(line.getStart(), point); final double dist2 = VectorMath.distancePPSqr(line.getEnd(), point); return Vector2.copy(dist1 < dist2 ? line.getStart() : line.getEnd()); } @SuppressWarnings("unused") private  LineMath(); }
LineMath { public static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point) { final double lambda = getLeadPointLambda(point, line); if (isLambdaInRange(lambda, 0, 1)) { return getPointOnLineForLambda(line, lambda); } final double dist1 = VectorMath.distancePPSqr(line.getStart(), point); final double dist2 = VectorMath.distancePPSqr(line.getEnd(), point); return Vector2.copy(dist1 < dist2 ? line.getStart() : line.getEnd()); } @SuppressWarnings("unused") private  LineMath(); static Optional<Double> getSlope(ILine line); static Optional<Double> getYIntercept(ILine line); static Optional<Double> getYValue(ILine line, final double x); static Optional<Double> getXValue(ILine line, final double y); static Optional<Double> getAngle(ILine line); static Vector2 leadPointOnLine(final ILine line, final IVector2 point); static Optional<IVector2> intersectionPoint(final ILine line1, final ILine line2); static Optional<IVector2> intersectionPointOfSegments(
			final ILine line1,
			final ILine line2); static Optional<IVector2> intersectionPointWithSegment(final ILine line, final ILine segment); static Optional<IVector2> intersectionPointOfLineAndHalfLine(final ILine line, final ILine halfLine); static Optional<IVector2> intersectionPointOfHalfLineAndHalfLine(final ILine halfLineA, final ILine halfLineB); static boolean isPointOnLineSegment(ILine line, final IVector2 point, final double margin); static boolean isPointInFront(final ILine line, final IVector2 point); static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point); static Vector2 nearestPointOnHalfLine(final ILine halfLine, final IVector2 point); static double distancePL(final IVector2 point, final ILine line); static double distancePointHalfLine(final IVector2 point, final ILine halfLine); static double distancePointLineSegment(final IVector2 point, final ILine lineSegment); static IVector2 stepAlongLine(final IVector2 start, final IVector2 end, final double stepSize); }
LineMath { public static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point) { final double lambda = getLeadPointLambda(point, line); if (isLambdaInRange(lambda, 0, 1)) { return getPointOnLineForLambda(line, lambda); } final double dist1 = VectorMath.distancePPSqr(line.getStart(), point); final double dist2 = VectorMath.distancePPSqr(line.getEnd(), point); return Vector2.copy(dist1 < dist2 ? line.getStart() : line.getEnd()); } @SuppressWarnings("unused") private  LineMath(); static Optional<Double> getSlope(ILine line); static Optional<Double> getYIntercept(ILine line); static Optional<Double> getYValue(ILine line, final double x); static Optional<Double> getXValue(ILine line, final double y); static Optional<Double> getAngle(ILine line); static Vector2 leadPointOnLine(final ILine line, final IVector2 point); static Optional<IVector2> intersectionPoint(final ILine line1, final ILine line2); static Optional<IVector2> intersectionPointOfSegments(
			final ILine line1,
			final ILine line2); static Optional<IVector2> intersectionPointWithSegment(final ILine line, final ILine segment); static Optional<IVector2> intersectionPointOfLineAndHalfLine(final ILine line, final ILine halfLine); static Optional<IVector2> intersectionPointOfHalfLineAndHalfLine(final ILine halfLineA, final ILine halfLineB); static boolean isPointOnLineSegment(ILine line, final IVector2 point, final double margin); static boolean isPointInFront(final ILine line, final IVector2 point); static Vector2 nearestPointOnLineSegment(final ILine line, final IVector2 point); static Vector2 nearestPointOnHalfLine(final ILine halfLine, final IVector2 point); static double distancePL(final IVector2 point, final ILine line); static double distancePointHalfLine(final IVector2 point, final ILine halfLine); static double distancePointLineSegment(final IVector2 point, final ILine lineSegment); static IVector2 stepAlongLine(final IVector2 start, final IVector2 end, final double stepSize);  }