Skip to content

Commit fb0314b

Browse files
committed
refactor: extract the private method findParameterTs(Line2D)
Aleady existing test cases must be changed because of the implementation of FindRoots.Polynomial(coeffs), that is, the order of the solutions.
1 parent 037d3c5 commit fb0314b

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/Spatial.Tests/Euclidean/Circle2DTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using MathNet.Spatial.Euclidean;
33
using NUnit.Framework;
44

@@ -59,9 +59,9 @@ public void CircleFromThreePointsArgumentException()
5959
Assert.Throws<ArgumentException>(() => { Circle2D.FromPoints(p1, p2, p3); });
6060
}
6161

62-
[TestCase("0,0", 1.41421356/*=sqrt(2)*/, "-1,-1", "+1,+1", "-1,-1", "+1,+1")]
63-
[TestCase("0,0", 1, "-1,0", "+1,0", "-1,0", "+1,0")]
64-
[TestCase("0,0", 1, "-1,0", "+1,0", "-1,0", "+1,0")]
62+
[TestCase("0,0", 1.41421356 /*=sqrt(2)*/, "-1,-1", "+1,+1", "+1,+1", "-1,-1")]
63+
[TestCase("0,0", 1, "-1,0", "+1,0", "+1,0", "-1,0")]
64+
[TestCase("0,0", 1, "0,-1", "0,+1", "0,+1", "0,-1")]
6565
public void CircleIntersectWithLine2D(string sc, double radius, string sps, string spe, string esp0, string esp1)
6666
{
6767
var circle = new Circle2D(Point2D.Parse(sc), radius);

src/Spatial/Euclidean/Circle2D.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using MathNet.Spatial.Internals;
1+
using MathNet.Spatial.Internals;
22
using System;
33
using System.Diagnostics.Contracts;
44
using System.Linq;
55
using System.Xml;
66
using System.Xml.Schema;
77
using System.Xml.Serialization;
8+
using MathNet.Numerics;
89
using HashCode = MathNet.Spatial.Internals.HashCode;
910
using MathNet.Spatial.Extensions;
1011

@@ -139,6 +140,15 @@ public static Circle2D FromPoints(Point2D pointA, Point2D pointB, Point2D pointC
139140
/// <param name="line">the given line</param>
140141
/// <returns>intersections as a Point2D Array, depending on the count.</returns>
141142
public Point2D[] IntersectWith(Line2D line)
143+
{
144+
var ts = this.findParameterTs(line);
145+
var result = ts
146+
.Select(t => line.StartPoint + t * line.Direction)
147+
.ToArray();
148+
return result;
149+
}
150+
151+
private double[] findParameterTs(Line2D line)
142152
{
143153
// These 2 equations in vector form can be described
144154
// (p-cc)^2=r^2 (eq1)
@@ -150,20 +160,22 @@ public Point2D[] IntersectWith(Line2D line)
150160
// ((s+t*d)-c)^2=r^2 (eq3)
151161
// (eq3) reduces to the following quadratic equation: a*t^2 + b*t + c==0
152162

153-
var cc = Center.ToVector2D(); //center of circle
163+
var cc = this.Center.ToVector2D(); //center of circle
154164
var s = line.StartPoint.ToVector2D();
155165
var d = line.Direction;
156-
var r = Radius;
166+
var r = this.Radius;
157167

158168
var a = d.DotProduct(d);
159169
var b = 2 * (s.DotProduct(d) - d.DotProduct(cc));
160-
var c = (s-cc).DotProduct(s-cc) - r * r;
170+
var c = (s - cc).DotProduct(s - cc) - r * r;
161171

162-
var discriminant = b * b - 4 * a * c;
163-
if (discriminant < 0)
164-
{
165-
return new Point2D[] { }; // no intersections found.
166-
}
172+
var soluions = FindRoots.Polynomial(new[] { c, b, a });
173+
var ts = soluions
174+
.Where(z => z.IsReal())
175+
.Select(z => z.Real)
176+
.ToArray();
177+
return ts;
178+
}
167179

168180
if (discriminant.IsNearlyEqualTo(0, 1e-6))
169181
{

0 commit comments

Comments
 (0)