Determine where two circles intersect in C#

If you don't like math, skip to the code below.

Consider the figure on the right showing two circles with radii r0 and r1. The points p0, p1, p2, and p3 have coordinates (x0, y0) and so forth.

Let d = the distance between the circles' centers so . Solving for a gives .Now there are three cases:

  • If d > r0 + r1: The circles are too far apart to intersect.
  • If d < |r0 – r1|: One circle is inside the other so there is no intersection.
  • If d = 0 and r0 = r1: The circles are the same.
  • If d = r0 + r1: The circles touch at a single point.
  • Otherwise: The circles touch at two points.

The Pythagorean theorem gives:

    

So:

    

Substituting and multiplying this out gives:

    

The –b2 terms on each side cancel out. You can then solve for b to get:

    

Similarly:

    

All of these values are known so you can solve for a and b. All that remains is using those distances to find the points p3.

If a line points in direction , then two perpendicular lines point in the directions <dy, –dx> and <–dy, dx>. Scaling the result gives the following coordinates for the points p3:

    

Be careful to notice the ± and ∓ symbols.

Click and drag to create two circles on the example program. The following code shows the FindCircleCircleIntersections method that the program uses to find the intersections.

// Find the points where the two circles intersect.
private int FindCircleCircleIntersections(
float cx0, float cy0, float radius0,
float cx1, float cy1, float radius1,
out PointF intersection1, out PointF intersection2)
{
// Find the distance between the centers.
float dx = cx0 - cx1;
float dy = cy0 - cy1;
double dist = Math.Sqrt(dx * dx + dy * dy);

// See how manhym solutions there are.
if (dist > radius0 + radius1)
{
// No solutions, the circles are too far apart.
intersection1 = new PointF(float.NaN, float.NaN);
intersection2 = new PointF(float.NaN, float.NaN);
return 0;
}
else if (dist < Math.Abs(radius0 - radius1))
{
// No solutions, one circle contains the other.
intersection1 = new PointF(float.NaN, float.NaN);
intersection2 = new PointF(float.NaN, float.NaN);
return 0;
}
else if ((dist == 0) && (radius0 == radius1))
{
// No solutions, the circles coincide.
intersection1 = new PointF(float.NaN, float.NaN);
intersection2 = new PointF(float.NaN, float.NaN);
return 0;
}
else
{
// Find a and h.
double a = (radius0 * radius0 -
radius1 * radius1 + dist * dist) / (2 * dist);
double h = Math.Sqrt(radius0 * radius0 - a * a);

// Find P2.
double cx2 = cx0 + a * (cx1 - cx0) / dist;
double cy2 = cy0 + a * (cy1 - cy0) / dist;

// Get the points P3.
intersection1 = new PointF(
(float)(cx2 + h * (cy1 - cy0) / dist),
(float)(cy2 - h * (cx1 - cx0) / dist));
intersection2 = new PointF(
(float)(cx2 - h * (cy1 - cy0) / dist),
(float)(cy2 + h * (cx1 - cx0) / dist));

// See if we have 1 or 2 solutions.
if (dist == radius0 + radius1) return 1;
return 2;
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 8/14/2010 6:53 AM Ate Peeringa wrote:
    Hello,

    Great piece of code and math my question is can you do that with three circles and find the radial center of intercection
    Reply to this
  • 8/19/2010 7:28 AM Rod Stephens wrote:
    Sorry but I don't understand what you mean by "radial center of intersection." Can you clarify what you mean?
    Reply to this
    1. 8/20/2010 12:11 AM Ate Peeringa wrote:
      Hello,

      Sorry i will explain better.
      I started a project to portioning with wifi. So if i have three circles that intersect i can get de midpoint op intersection of the three circles this is called Trilateration.
      Or must i use a other algorithm. Sorry for my bad englisch im coming from holland.
      Reply to this
      1. 8/23/2010 7:26 AM Rod Stephens wrote:
        I think this example will help:

        - Find the area where two or more circles overlap in C#

        And this one, which will I'll post tomorrow:

        - Find a Region's centroid in C#

        (Sorry but I don't see how to include a link in this comment.)
        Reply to this
        1. 8/23/2010 10:50 PM Ate Peeringa wrote:
          Super your the best this wil help me a lot.
          Reply to this
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.