Index Books FAQ Contact About Rod

# Title: 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.