Determine where a line intersects a circle in C#

The FindLineCircleIntersections method calculates the points of intersection between a line and a circle. It takes as parameters a circle's center point and radius, and two points on the line. It uses out parameters to return the coordinates of the points of intersection. The function returns the number of points of intersection (0, 1, or 2).

To fund the points of intersection, the code considers the line as generated by the equations:

    X(t) = x1 + (x2 - x1) * t
Y(t) = y1 + (y2 - y1) * t

Where t ranges from 0 to 1 to draw the line segment.

The code plugs these equations into the equation for a circle:

    (X - Cx)^2 + (Y - Cy)^2 = radius^2

It then solves for t by using the quadratic formula. The result is 0, 1, or 2 real values for t. It plugs those values back into the equations for the line to get the points of intersection.

// Find the points of intersection.
private int FindLineCircleIntersections(float cx, float cy, float radius,
PointF point1, PointF point2, out PointF intersection1, out PointF intersection2)
{
float dx, dy, A, B, C, det, t;

dx = point2.X - point1.X;
dy = point2.Y - point1.Y;

A = dx * dx + dy * dy;
B = 2 * (dx * (point1.X - cx) + dy * (point1.Y - cy));
C = (point1.X - cx) * (point1.X - cx) + (point1.Y - cy) * (point1.Y - cy) - radius * radius;

det = B * B - 4 * A * C;
if ((A <= 0.0000001) || (det < 0))
{
// No real solutions.
intersection1 = new PointF(float.NaN, float.NaN);
intersection2 = new PointF(float.NaN, float.NaN);
return 0;
}
else if (det == 0)
{
// One solution.
t = -B / (2 * A);
intersection1 = new PointF(point1.X + t * dx, point1.Y + t * dy);
intersection2 = new PointF(float.NaN, float.NaN);
return 1;
}
else
{
// Two solutions.
t = (float)((-B + Math.Sqrt(det)) / (2 * A));
intersection1 = new PointF(point1.X + t * dx, point1.Y + t * dy);
t = (float)((-B - Math.Sqrt(det)) / (2 * A));
intersection2 = new PointF(point1.X + t * dx, point1.Y + t * dy);
return 2;
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.