From 0010a47f62ee61e82dc2f9c06022db674b2d6032 Mon Sep 17 00:00:00 2001 From: f-frhs Date: Tue, 26 Dec 2023 01:30:16 +0900 Subject: [PATCH 1/5] Feat: Line2D.DistanceTo(Point2D p) --- src/Spatial.Tests/Euclidean/Line2DTests.cs | 14 ++++++++++++++ src/Spatial/Euclidean/Line2D.cs | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Spatial.Tests/Euclidean/Line2DTests.cs b/src/Spatial.Tests/Euclidean/Line2DTests.cs index b4a5fc5..2a0b1f4 100644 --- a/src/Spatial.Tests/Euclidean/Line2DTests.cs +++ b/src/Spatial.Tests/Euclidean/Line2DTests.cs @@ -56,6 +56,20 @@ 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,0", "0,+1", "0,0", 0.70710678)] + [TestCase("+1,-1", "+1,+1", "0,0", 1.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. /// From ffba33569664f732f81f8c4f1452d42297fd7466 Mon Sep 17 00:00:00 2001 From: f-frhs Date: Tue, 26 Dec 2023 21:22:34 +0900 Subject: [PATCH 2/5] Test: add testcases where the line pass through the point --- src/Spatial.Tests/Euclidean/Line2DTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Spatial.Tests/Euclidean/Line2DTests.cs b/src/Spatial.Tests/Euclidean/Line2DTests.cs index 2a0b1f4..68bd66c 100644 --- a/src/Spatial.Tests/Euclidean/Line2DTests.cs +++ b/src/Spatial.Tests/Euclidean/Line2DTests.cs @@ -58,8 +58,11 @@ public void LineDirection(string p1s, string p2s, string exs) [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)] public void DistanceFromLineToPoint(string p1s, string p2s, string ps, double expectedDistance) { From 00ce8a6e42ca608cbb126efc31b468d2d3aea84f Mon Sep 17 00:00:00 2001 From: f-frhs Date: Tue, 26 Dec 2023 21:38:08 +0900 Subject: [PATCH 3/5] Test: Add tests where sp1 and sp2 are swapped --- src/Spatial.Tests/Euclidean/Line2DTests.cs | 37 +++++++++++++++------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Spatial.Tests/Euclidean/Line2DTests.cs b/src/Spatial.Tests/Euclidean/Line2DTests.cs index 68bd66c..574930b 100644 --- a/src/Spatial.Tests/Euclidean/Line2DTests.cs +++ b/src/Spatial.Tests/Euclidean/Line2DTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using MathNet.Spatial.Euclidean; using MathNet.Spatial.Units; using NUnit.Framework; @@ -56,17 +56,32 @@ 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)] - public void DistanceFromLineToPoint(string p1s, string p2s, string ps, double expectedDistance) + public static object[] DistanceTestCases = { - var line = new Line2D(Point2D.Parse(p1s), Point2D.Parse(p2s)); + new object[] { "-1,+1", "+1,+1", "0,0", 1.0 }, + new object[] { "-1,+1", "+1,+1", "0,3", 2.0 }, + new object[] { "-1,+1", "+1,+1", "0,1", 0.0 }, + new object[] { "-1,0", "0,+1", "0,0", 0.70710678 }, + new object[] { "-1,0", "0,+1", "-0.5,0.5", 0.0 }, + new object[] { "+1,-1", "+1,+1", "0,0", 1.0 }, + new object[] { "+1,-1", "+1,+1", "1,0", 0.0 }, + new object[] { "+1,-1", "+1,+1", "3,0", 2.0 }, + }; + + [TestCaseSource(nameof(DistanceTestCases))] + public void DistanceFromLineToPoint(string sp1, string sp2, string ps, double expectedDistance) + { + var line = new Line2D(Point2D.Parse(sp1), Point2D.Parse(sp2)); + var p = Point2D.Parse(ps); + + var actual = line.DistanceTo(p); + Assert.That(actual, Is.EqualTo(expectedDistance).Within(1e-6)); + } + + [TestCaseSource(nameof(DistanceTestCases))] + public void DistanceFromLineToPoint_SwappedP1sAndP2s(string sp1, string sp2, string ps, double expectedDistance) + { + var line = new Line2D(Point2D.Parse(sp2), Point2D.Parse(sp1)); // swapped var p = Point2D.Parse(ps); var actual = line.DistanceTo(p); From 1c444c7fd603eb54ae8390a2ae136dffc4e969a7 Mon Sep 17 00:00:00 2001 From: f-frhs Date: Sun, 7 Jan 2024 22:40:46 +0900 Subject: [PATCH 4/5] Revert "Test: Add tests where sp1 and sp2 are swapped" This reverts commit 00ce8a6e42ca608cbb126efc31b468d2d3aea84f. --- src/Spatial.Tests/Euclidean/Line2DTests.cs | 37 +++++++--------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/Spatial.Tests/Euclidean/Line2DTests.cs b/src/Spatial.Tests/Euclidean/Line2DTests.cs index 574930b..68bd66c 100644 --- a/src/Spatial.Tests/Euclidean/Line2DTests.cs +++ b/src/Spatial.Tests/Euclidean/Line2DTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using MathNet.Spatial.Euclidean; using MathNet.Spatial.Units; using NUnit.Framework; @@ -56,32 +56,17 @@ public void LineDirection(string p1s, string p2s, string exs) AssertGeometry.AreEqual(ex, line.Direction); } - public static object[] DistanceTestCases = + [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)] + public void DistanceFromLineToPoint(string p1s, string p2s, string ps, double expectedDistance) { - new object[] { "-1,+1", "+1,+1", "0,0", 1.0 }, - new object[] { "-1,+1", "+1,+1", "0,3", 2.0 }, - new object[] { "-1,+1", "+1,+1", "0,1", 0.0 }, - new object[] { "-1,0", "0,+1", "0,0", 0.70710678 }, - new object[] { "-1,0", "0,+1", "-0.5,0.5", 0.0 }, - new object[] { "+1,-1", "+1,+1", "0,0", 1.0 }, - new object[] { "+1,-1", "+1,+1", "1,0", 0.0 }, - new object[] { "+1,-1", "+1,+1", "3,0", 2.0 }, - }; - - [TestCaseSource(nameof(DistanceTestCases))] - public void DistanceFromLineToPoint(string sp1, string sp2, string ps, double expectedDistance) - { - var line = new Line2D(Point2D.Parse(sp1), Point2D.Parse(sp2)); - var p = Point2D.Parse(ps); - - var actual = line.DistanceTo(p); - Assert.That(actual, Is.EqualTo(expectedDistance).Within(1e-6)); - } - - [TestCaseSource(nameof(DistanceTestCases))] - public void DistanceFromLineToPoint_SwappedP1sAndP2s(string sp1, string sp2, string ps, double expectedDistance) - { - var line = new Line2D(Point2D.Parse(sp2), Point2D.Parse(sp1)); // swapped + var line = new Line2D(Point2D.Parse(p1s), Point2D.Parse(p2s)); var p = Point2D.Parse(ps); var actual = line.DistanceTo(p); From cd80cf195a07cb3a79f0ccf35dad3f26f14cc89e Mon Sep 17 00:00:00 2001 From: f-frhs Date: Sun, 7 Jan 2024 22:44:29 +0900 Subject: [PATCH 5/5] Test: Add tests where sp1 and sp2 are swapped --- src/Spatial.Tests/Euclidean/Line2DTests.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Spatial.Tests/Euclidean/Line2DTests.cs b/src/Spatial.Tests/Euclidean/Line2DTests.cs index 68bd66c..7dcc4ed 100644 --- a/src/Spatial.Tests/Euclidean/Line2DTests.cs +++ b/src/Spatial.Tests/Euclidean/Line2DTests.cs @@ -64,6 +64,14 @@ public void LineDirection(string p1s, string p2s, string exs) [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));