# Determine where two circles intersect in C# This example shows one method for finding 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 <dx, dy>, 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.

# The Code

The following code shows the FindCircleCircleIntersections method that the program uses to find 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 many solutions there are.
{
// No solutions, the circles are too far apart.
intersection1 = new PointF(float.NaN, float.NaN);
intersection2 = new PointF(float.NaN, float.NaN);
return 0;
}
{
// No solutions, one circle contains the other.
intersection1 = new PointF(float.NaN, float.NaN);
intersection2 = new PointF(float.NaN, float.NaN);
return 0;
}
{
// 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.

// 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.
return 2;
}
}```

This code follows the explanation above.

Download the example to see additional details. When you click and drag twice to define two circles, the program uses the FindCircleCircleIntersections method to find their points of intersection (if any). It then draws the circles and the intersections.   This entry was posted in algorithms, geometry, graphics, mathematics and tagged , , , , , , , , , , , , . Bookmark the permalink.

### 25 Responses to Determine where two circles intersect in C#

1. Ate Peeringa says:

Hello,

Great piece of code and math my question is can you do that with three circles and find the radial center of intercection

2. Rod Stephens says:

Sorry but I don’t understand what you mean by “radial center of intersection.” Can you clarify what you mean?

3. Ate Peeringa says:

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.

4. Rod Stephens says:

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.)

5. Ate Peeringa says:

Super your the best this wil help me a lot.

6. Samaras says:

I have implemented the same program in C++. Moreover, I have corrected the wrong math (?) for the case that the one circle is inside the other.
http://gsamaras.wordpress.com/code/determine-where-two-circles-intersect-c/
You can see the wrong math, when giving:
c1 = (2, 2) and r1 = 1
c2 = (2, 0) and r2 = 1.5

Good work however. 🙂

7. Rod Stephens says:

I’m not sure what you mean by “one circle is inside the other.” I think in the example you give, the circle’s intersect so one isn’t inside the other. Unless you meant c2 = (2, 2)?

8. Iza says:

I don’t understand how did you get this cx2 = cx0 + a * (cx1 – cx0) / dist. Could you explain it?

9. Rod Stephens says:

The point (cx2, cy2) is the point p2 in the first picture. To get those coordinates, this equation starts at p0 (the cx0 term). It then moves towards p1.

To move the full distance to p1, the equation would use (p1 – p0). Unfortunately, the picture reversed the roles of a and b. I’ll fix that.

So to go only from p0 to p2, it should go only part of the distance (p1 – p0). The fraction of that distance it should go is (a / dist).

To look at it another way, point p2 is (a / dist) of the distance from p0 to p1.

I hope that helps. Look at the new version of the first figure where I’ve switched a and b.

10. Olivier L says:

Hello,
Great job!
I translated this method in Java but I found 2 uncovered cases. I made a small scheme at http://i.imgur.com/uh4aBCe.png

Excuse me for my English.

• RodStephens says:

I think the program handles the case on the left. It finds the two points of intersection.

It also sort of handles the case on the right. You can’t easily draw the circles by hand, but if you modify the code to create two circles in that configuration the program finds the single point of intersection twice so it incorrectly thinks it has two points of intersection. You could modify the end of the FindCircleCircleIntersections method so it checks for this case and returns 1 instead of 2 if you like.

11. Olivier L says:

I redid the test and it works effectively as you say. I can be wrong in the coordinates.
The last condition is :
// See if we have 1 or 2 solutions.

Thank you

12. Michael says:

Thanks for the script.
I adapted it to work with SVG circle intersections and jQuery/Javascript:
https://github.com/michaelslevy/CircleIntersectionJquery

13. Bala says:

What is the value of (X3, Y3) When two circles intersects in a single point ?.
According to the formula here for X3, Y3 i get X3 = X2, Y3 = Y2
because on single point intersection value of ‘h’ will be 0(Zero), so the term h(y0 – y1)/(2*d) will be zero . so X3 will get the value of x2.

• RodStephens says:

That’s right. The points p3=(X3, Y3) are the points where the circles intersect. If they intersect at a single point, then you get a single result. You get the same result twice because of the way the equations work.

If you want to find the perpendicular bisector (the line between the two circles), you need to use another method. Just take the vector connecting the circles’ centers, swap them, and reverse one of the signs. For example, suppose the vector is <dx, dy>. Then the perpendicular vectors are <dy, -dx> and <-dy, dx>. You can use those and the single point P3 to get the bisector.

14. AmilaSAS says:

Thankz. Helped me a lot.

15. Jonny says:

Hi Rod, thanks so much for your work! What I don’t understand is where this part of the formula comes from: X3 = X2 + h(Y1 – Y0) / d ??

• RodStephens says:

Sorry, that was perhaps too big a step.

The vector gives a vector from point p0 to point p1.

Switching them and changing their signs with the +/- and -/+ give perpendicular vectors. Those point in the direction from point p2 toward the two points p3.

All we need to do now is scale the new vectors so they have the correct lengths. To do that, we divide them by the vectors’ length d and then multiply by the desired length h.

The result is two vectors with lengths d pointing from point p2 to the points p3.

16. Teron says:

Great! thanks!!!