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

Fix Line2D.TryIntersect to pass nullable Point2D #187

Merged
merged 4 commits into from
Mar 27, 2023
Merged
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
35 changes: 17 additions & 18 deletions src/Spatial.Tests/Euclidean/LineSegment2DTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void ConstructorThrowsErrorOnSamePoint()
[TestCase("0,0", "-1,0", 1)]
[TestCase("0,-1", "0,1", 2)]
[TestCase("-1,-1", "2,2", 4.24264068711)]
public void LineLength(string p1s, string p2s, double expected)
public void LineLength(string p1S, string p2S, double expected)
{
var p1 = Point2D.Parse(p1s);
var p2 = Point2D.Parse(p2s);
var p1 = Point2D.Parse(p1S);
var p2 = Point2D.Parse(p2S);
var line = new LineSegment2D(p1, p2);
var len = line.Length;

Expand All @@ -46,10 +46,10 @@ public void LineLength(string p1s, string p2s, double expected)
[TestCase("3,0", "0,0", "-1,0")]
[TestCase("2.7,-2.7", "0,0", "-0.707106781,0.707106781")]
[TestCase("11,-1", "11,1", "0,1")]
public void LineDirection(string p1s, string p2s, string exs)
public void LineDirection(string p1S, string p2S, string exs)
{
var p1 = Point2D.Parse(p1s);
var p2 = Point2D.Parse(p2s);
var p1 = Point2D.Parse(p1S);
var p2 = Point2D.Parse(p2S);
var ex = Vector2D.Parse(exs);
var line = new LineSegment2D(p1, p2);

Expand All @@ -58,20 +58,20 @@ public void LineDirection(string p1s, string p2s, string exs)

[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)
public void EqualityOperator(string p1S, string p2S, string p3S, string p4S, bool expected)
{
var l1 = LineSegment2D.Parse(p1s, p2s);
var l2 = LineSegment2D.Parse(p3s, p4s);
var l1 = LineSegment2D.Parse(p1S, p2S);
var l2 = LineSegment2D.Parse(p3S, p4S);

Assert.AreEqual(expected, l1 == l2);
}

[TestCase("0,0", "10,10", "0,0", "10,10", false)]
[TestCase("0,0", "10,10", "0,0", "10,11", true)]
public void InequalityOperator(string p1s, string p2s, string p3s, string p4s, bool expected)
public void InequalityOperator(string p1S, string p2S, string p3S, string p4S, bool expected)
{
var l1 = new LineSegment2D(Point2D.Parse(p1s), Point2D.Parse(p2s));
var l2 = new LineSegment2D(Point2D.Parse(p3s), Point2D.Parse(p4s));
var l1 = new LineSegment2D(Point2D.Parse(p1S), Point2D.Parse(p2S));
var l2 = new LineSegment2D(Point2D.Parse(p3S), Point2D.Parse(p4S));

Assert.AreEqual(expected, l1 != l2);
}
Expand Down Expand Up @@ -139,17 +139,16 @@ public void IntersectWithTest(string s1, string e1, string s2, string e2, string
Assert.AreEqual(e, result);
}

[TestCase("0,0", "-2,-2", "1,0", "1,2", "1,1")]
[TestCase("0,0", "-2,-2", "0,1", "2,1", "1,1")]
[TestCase("0,0", "2,2", "-1,-5", "-1,0", "-1,-1")]
public void IntersectWithTest2(string s1, string e1, string s2, string e2, string expected)
[TestCase("0,0", "-2,-2", "1,0", "1,2")]
[TestCase("0,0", "-2,-2", "0,1", "2,1")]
[TestCase("0,0", "2,2", "-1,-5", "-1,0")]
public void IntersectWithTest2(string s1, string e1, string s2, string e2)
{
var line1 = LineSegment2D.Parse(s1, e1);
var line2 = LineSegment2D.Parse(s2, e2);
var e = string.IsNullOrEmpty(expected) ? (Point2D?)null : Point2D.Parse(expected);
bool success = line1.TryIntersect(line2, out var result, Angle.FromRadians(0.001));
Assert.IsFalse(success);
Assert.AreEqual(e, result);
Assert.AreEqual(null, result);
}

[TestCase("0,0", "0,1", "1,1", "1,2", 0.0001, true)]
Expand Down
23 changes: 14 additions & 9 deletions src/Spatial/Euclidean/LineSegment2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ public Point2D ClosestPointTo(Point2D p)
}

/// <summary>
/// Compute the intersection between two lines if the angle between them is greater than a specified
/// Compute the intersection between two line segments if the angle between them is greater than a specified
/// angle tolerance.
/// </summary>
/// <param name="other">The other line to compute the intersection with</param>
/// <param name="intersection">When this method returns, contains the intersection point, if the conversion succeeded, or the default point if the conversion failed.</param>
/// <param name="tolerance">The tolerance used when checking if the lines are parallel</param>
/// <param name="other">The other line segment to compute the intersection with</param>
/// <param name="intersection">The intersection if it exists; otherwise null</param>
/// <param name="tolerance">The tolerance used when checking if the line segments are parallel</param>
/// <returns>True if an intersection exists; otherwise false</returns>
[Pure]
public bool TryIntersect(LineSegment2D other, out Point2D intersection, Angle tolerance)
public bool TryIntersect(LineSegment2D other, out Point2D? intersection, Angle tolerance)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization intersection = null;

intersection = null;
if (IsParallelTo(other, tolerance))
{
intersection = default(Point2D);
{
return false;
}

Expand All @@ -159,8 +159,13 @@ public bool TryIntersect(LineSegment2D other, out Point2D intersection, Angle to
var t = (q - p).CrossProduct(s) / r.CrossProduct(s);
var u = (p - q).CrossProduct(r) / s.CrossProduct(r);

intersection = p + (t * r);
return (0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0);
var isIntersected = (0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0);
if (isIntersected)
{
intersection = p + t * r;
}

return intersection != null;
}

/// <summary>
Expand Down