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 r_{0} and r_{1}. The points p_{0}, p_{1}, p_{2}, and p_{3} have coordinates (x_{0}, y_{0}) and so forth.

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

- If d > r
_{0}+ r_{1}: The circles are too far apart to intersect. - If d < |r
_{0}– r_{1}|: One circle is inside the other so there is no intersection. - If d = 0 and r
_{0}= r_{1}: The circles are the same. - If d = r
_{0}+ r_{1}: 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 –b^{2} 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 p_{3}.

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 p_{3}:

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. 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; } }

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.

Hello,

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

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

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.

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

Super your the best this wil help me a lot.

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

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

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

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.

Pingback: Find the tangent lines between a point and a circle in C#C# Helper

Pingback: Find the area where circles overlap in C# - C# HelperC# Helper

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.

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

FindCircleCircleIntersectionsmethod so it checks for this case and returns 1 instead of 2 if you like.Thanks for your answer.

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.

if ((dist == radius0 + radius1) || (dist == Math.Abs(radius0 – radius1))) return 1;

Thank you

Thanks for the script.

I adapted it to work with SVG circle intersections and jQuery/Javascript:

https://github.com/michaelslevy/CircleIntersectionJquery

Pingback: Animate two rods connected to a wheel in C# - C# HelperC# Helper

Pingback: Animate a piston driving a wheel in C# - C# HelperC# Helper

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.

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 pointP3to get the bisector.Thankz. Helped me a lot.

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 ??

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.

Pingback: Draw interlocked circles in C# - C# HelperC# Helper

Pingback: Perform trilateration in C# - C# HelperC# Helper