diff --git a/src/Spatial.Tests/Euclidean/Line2DTests.cs b/src/Spatial.Tests/Euclidean/Line2DTests.cs index b4a5fc5..7dcc4ed 100644 --- a/src/Spatial.Tests/Euclidean/Line2DTests.cs +++ b/src/Spatial.Tests/Euclidean/Line2DTests.cs @@ -56,6 +56,31 @@ public void LineDirection(string p1s, string p2s, string exs) AssertGeometry.AreEqual(ex, line.Direction); } + [TestCase("-1,+1", "+1,+1", "0,0", 1.0)] + [TestCase("-1,+1", "+1,+1", "0,3", 2.0)] + [TestCase("-1,+1", "+1,+1", "0,1", 0.0)] + [TestCase("-1,0", "0,+1", "0,0", 0.70710678)] + [TestCase("-1,0", "0,+1", "-0.5,0.5", 0.0)] + [TestCase("+1,-1", "+1,+1", "0,0", 1.0)] + [TestCase("+1,-1", "+1,+1", "1,0", 0.0)] + [TestCase("+1,-1", "+1,+1", "3,0", 2.0)] + [TestCase("+1,+1", "-1,+1", "0,0", 1.0)] + [TestCase("+1,+1", "-1,+1", "0,3", 2.0)] + [TestCase("+1,+1", "-1,+1", "0,1", 0.0)] + [TestCase("0,+1", "-1,0", "0,0", 0.70710678)] + [TestCase("0,+1", "-1,0", "-0.5,0.5", 0.0)] + [TestCase("+1,+1", "+1,-1", "0,0", 1.0)] + [TestCase("+1,+1", "+1,-1", "1,0", 0.0)] + [TestCase("+1,+1", "+1,-1", "3,0", 2.0)] + public void DistanceFromLineToPoint(string p1s, string p2s, string ps, double expectedDistance) + { + var line = new Line2D(Point2D.Parse(p1s), Point2D.Parse(p2s)); + var p = Point2D.Parse(ps); + + var actual = line.DistanceTo(p); + Assert.That(actual, Is.EqualTo(expectedDistance).Within(1e-6)); + } + [TestCase("0,0", "10,10", "0,0", "10,10", true)] [TestCase("0,0", "10,10", "0,0", "10,11", false)] public void EqualityOperator(string p1s, string p2s, string p3s, string p4s, bool expected) diff --git a/src/Spatial/Euclidean/Line2D.cs b/src/Spatial/Euclidean/Line2D.cs index d1c516a..81a50e4 100644 --- a/src/Spatial/Euclidean/Line2D.cs +++ b/src/Spatial/Euclidean/Line2D.cs @@ -123,6 +123,19 @@ public static Line2D Parse(string startPointString, string endPointString) return new Line2D(Point2D.Parse(startPointString), Point2D.Parse(endPointString)); } + /// + /// Returns the straight line Distance to the given point. + /// + /// the given point + /// a distance measure + [Pure] + public double DistanceTo(Point2D p) + { + var closestPoint = ClosestPointTo(p, false); // this is Line2D, not LineSegment2D. + var result = closestPoint.DistanceTo(p); + return result; + } + /// /// Returns the shortest line between this line and a point. ///