-
Notifications
You must be signed in to change notification settings - Fork 138
Circle2D intersection with Line(Segment)2D #237
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
Changes from 17 commits
0a2521d
c3ad590
037d3c5
fb0314b
baf6605
c2612b6
4685302
f3e4731
847cdaf
2d222ac
571cf7d
9e33c1b
7c5df6a
1a527b1
b27fdb1
dc7ea85
5930b12
0e7eb69
5c3cd70
ab59a23
451a87d
3d3748b
6b8c3db
f3815ed
acccdbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
using MathNet.Spatial.Internals; | ||
using System; | ||
using System.Diagnostics.Contracts; | ||
using System.Linq; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
using System.Xml.Serialization; | ||
using MathNet.Numerics; | ||
using HashCode = MathNet.Spatial.Internals.HashCode; | ||
|
||
namespace MathNet.Spatial.Euclidean | ||
|
@@ -131,6 +133,67 @@ public static Circle2D FromPoints(Point2D pointA, Point2D pointB, Point2D pointC | |
return new Circle2D(center, radius); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the intersections of this circle with the given line. | ||
/// </summary> | ||
/// <param name="line">the given line</param> | ||
/// <returns>intersections as a Point2D Array, depending on the count.</returns> | ||
public Point2D[] IntersectWith(Line2D line) | ||
{ | ||
var ts = this.findParameterTs(line); | ||
var result = ts | ||
.Select(t => line.StartPoint + t * line.Direction) | ||
.ToArray(); | ||
return result; | ||
} | ||
|
||
private double[] findParameterTs(Line2D line) | ||
{ | ||
// These 2 equations in vector form can be described | ||
// (p-cc)^2=r^2 (eq1) | ||
// p=s+t*d (eq2) | ||
// , where p is the point on the line and/or circle, | ||
jkalias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// cc is the center of the circle, | ||
// r is the radius of the circle, | ||
// s is the starting point of the line, | ||
// t is the parameter and | ||
// d is the line direction. | ||
// Substituting (eq2) into (eq1) yields: | ||
// ((s+t*d)-cc)^2=r^2 (eq3) | ||
// (eq3) reduces to the following quadratic equation: a*t^2 + b*t + c==0 | ||
|
||
var cc = this.Center.ToVector2D(); //center of circle | ||
var s = line.StartPoint.ToVector2D(); | ||
var d = line.Direction; | ||
var r = this.Radius; | ||
|
||
var a = 1d; | ||
|
||
var b = 2 * (s.DotProduct(d) - d.DotProduct(cc)); | ||
var c = (s - cc).DotProduct(s - cc) - r * r; | ||
|
||
var solutions = FindRoots.Polynomial(new[] { c, b, a }); | ||
var ts = solutions | ||
.Where(z => z.IsReal()) | ||
.Select(z => z.Real) | ||
.Distinct() | ||
.ToArray(); | ||
return ts; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the intersections of this circle with the given line segment, which lie within the segment. | ||
/// </summary> | ||
/// <param name="segment">the given line-segment</param> | ||
/// <returns>intersections as a Point2D Array, depending on the count.</returns> | ||
public Point2D[] IntersectWith(LineSegment2D segment) | ||
{ | ||
var ts = findParameterTs(segment.ToLine2D()) | ||
.Where(t => 0 <= t && t <= segment.Length); | ||
var result = ts.Select(t => segment.StartPoint + t * segment.Direction).ToArray(); | ||
return result; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns a value to indicate if a pair of circles are equal | ||
/// </summary> | ||
|
Uh oh!
There was an error while loading. Please reload this page.