Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Line2D.DistanceTo(Point2D p) #238

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Spatial.Tests/Euclidean/Line2DTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
jkalias marked this conversation as resolved.
Show resolved Hide resolved
{
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)
Expand Down
13 changes: 13 additions & 0 deletions src/Spatial/Euclidean/Line2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public static Line2D Parse(string startPointString, string endPointString)
return new Line2D(Point2D.Parse(startPointString), Point2D.Parse(endPointString));
}

/// <summary>
/// Returns the straight line Distance to the given point.
/// </summary>
/// <param name="p">the given point</param>
/// <returns>a distance measure</returns>
[Pure]
public double DistanceTo(Point2D p)
{
var closestPoint = ClosestPointTo(p, false); // this is Line2D, not LineSegment2D.
var result = closestPoint.DistanceTo(p);
return result;
}

/// <summary>
/// Returns the shortest line between this line and a point.
/// </summary>
Expand Down