Skip to content

Commit 698a833

Browse files
f-frhsjkalias
andauthored
Fix Line2D.TryIntersect to pass nullable Point2D (#187)
* Fix Line2D.TryIntersect to pass nullable Point2D * Simplified TryIntersect * Cleanup in LineSegment2DTests --------- Co-authored-by: jkalias <[email protected]>
1 parent 62f2369 commit 698a833

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/Spatial.Tests/Euclidean/LineSegment2DTests.cs

+17-18
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public void ConstructorThrowsErrorOnSamePoint()
3232
[TestCase("0,0", "-1,0", 1)]
3333
[TestCase("0,-1", "0,1", 2)]
3434
[TestCase("-1,-1", "2,2", 4.24264068711)]
35-
public void LineLength(string p1s, string p2s, double expected)
35+
public void LineLength(string p1S, string p2S, double expected)
3636
{
37-
var p1 = Point2D.Parse(p1s);
38-
var p2 = Point2D.Parse(p2s);
37+
var p1 = Point2D.Parse(p1S);
38+
var p2 = Point2D.Parse(p2S);
3939
var line = new LineSegment2D(p1, p2);
4040
var len = line.Length;
4141

@@ -46,10 +46,10 @@ public void LineLength(string p1s, string p2s, double expected)
4646
[TestCase("3,0", "0,0", "-1,0")]
4747
[TestCase("2.7,-2.7", "0,0", "-0.707106781,0.707106781")]
4848
[TestCase("11,-1", "11,1", "0,1")]
49-
public void LineDirection(string p1s, string p2s, string exs)
49+
public void LineDirection(string p1S, string p2S, string exs)
5050
{
51-
var p1 = Point2D.Parse(p1s);
52-
var p2 = Point2D.Parse(p2s);
51+
var p1 = Point2D.Parse(p1S);
52+
var p2 = Point2D.Parse(p2S);
5353
var ex = Vector2D.Parse(exs);
5454
var line = new LineSegment2D(p1, p2);
5555

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

5959
[TestCase("0,0", "10,10", "0,0", "10,10", true)]
6060
[TestCase("0,0", "10,10", "0,0", "10,11", false)]
61-
public void EqualityOperator(string p1s, string p2s, string p3s, string p4s, bool expected)
61+
public void EqualityOperator(string p1S, string p2S, string p3S, string p4S, bool expected)
6262
{
63-
var l1 = LineSegment2D.Parse(p1s, p2s);
64-
var l2 = LineSegment2D.Parse(p3s, p4s);
63+
var l1 = LineSegment2D.Parse(p1S, p2S);
64+
var l2 = LineSegment2D.Parse(p3S, p4S);
6565

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

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

7676
Assert.AreEqual(expected, l1 != l2);
7777
}
@@ -139,17 +139,16 @@ public void IntersectWithTest(string s1, string e1, string s2, string e2, string
139139
Assert.AreEqual(e, result);
140140
}
141141

142-
[TestCase("0,0", "-2,-2", "1,0", "1,2", "1,1")]
143-
[TestCase("0,0", "-2,-2", "0,1", "2,1", "1,1")]
144-
[TestCase("0,0", "2,2", "-1,-5", "-1,0", "-1,-1")]
145-
public void IntersectWithTest2(string s1, string e1, string s2, string e2, string expected)
142+
[TestCase("0,0", "-2,-2", "1,0", "1,2")]
143+
[TestCase("0,0", "-2,-2", "0,1", "2,1")]
144+
[TestCase("0,0", "2,2", "-1,-5", "-1,0")]
145+
public void IntersectWithTest2(string s1, string e1, string s2, string e2)
146146
{
147147
var line1 = LineSegment2D.Parse(s1, e1);
148148
var line2 = LineSegment2D.Parse(s2, e2);
149-
var e = string.IsNullOrEmpty(expected) ? (Point2D?)null : Point2D.Parse(expected);
150149
bool success = line1.TryIntersect(line2, out var result, Angle.FromRadians(0.001));
151150
Assert.IsFalse(success);
152-
Assert.AreEqual(e, result);
151+
Assert.AreEqual(null, result);
153152
}
154153

155154
[TestCase("0,0", "0,1", "1,1", "1,2", 0.0001, true)]

src/Spatial/Euclidean/LineSegment2D.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,19 @@ public Point2D ClosestPointTo(Point2D p)
134134
}
135135

136136
/// <summary>
137-
/// Compute the intersection between two lines if the angle between them is greater than a specified
137+
/// Compute the intersection between two line segments if the angle between them is greater than a specified
138138
/// angle tolerance.
139139
/// </summary>
140-
/// <param name="other">The other line to compute the intersection with</param>
141-
/// <param name="intersection">When this method returns, contains the intersection point, if the conversion succeeded, or the default point if the conversion failed.</param>
142-
/// <param name="tolerance">The tolerance used when checking if the lines are parallel</param>
140+
/// <param name="other">The other line segment to compute the intersection with</param>
141+
/// <param name="intersection">The intersection if it exists; otherwise null</param>
142+
/// <param name="tolerance">The tolerance used when checking if the line segments are parallel</param>
143143
/// <returns>True if an intersection exists; otherwise false</returns>
144144
[Pure]
145-
public bool TryIntersect(LineSegment2D other, out Point2D intersection, Angle tolerance)
145+
public bool TryIntersect(LineSegment2D other, out Point2D? intersection, Angle tolerance)
146146
{
147+
intersection = null;
147148
if (IsParallelTo(other, tolerance))
148-
{
149-
intersection = default(Point2D);
149+
{
150150
return false;
151151
}
152152

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

162-
intersection = p + (t * r);
163-
return (0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0);
162+
var isIntersected = (0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0);
163+
if (isIntersected)
164+
{
165+
intersection = p + t * r;
166+
}
167+
168+
return intersection != null;
164169
}
165170

166171
/// <summary>

0 commit comments

Comments
 (0)