Draw a circle through three points in C#

example

The perpendicular bisector of any chord on a circle passes through the circle’s center. To find the center given three points, simply find the perpendicular bisectors of the first two and last two pairs of points, and see where the bisectors intersect. After you find the center, calculate the distance from the center to one of the points to get the circle’s radius.

If the bisectors do not intersect, the three points are colinear so they do not define a circle.

The following code shows how the program finds the circle defined by three points.

// Find a circle through the three points.
private void FindCircle(PointF a, PointF b, PointF c,
    out PointF center, out float radius)
{
    // Get the perpendicular bisector of (x1, y1) and (x2, y2).
    float x1 = (b.X + a.X) / 2;
    float y1 = (b.Y + a.Y) / 2;
    float dy1 = b.X - a.X;
    float dx1 = -(b.Y - a.Y);

    // Get the perpendicular bisector of (x2, y2) and (x3, y3).
    float x2 = (c.X + b.X) / 2;
    float y2 = (c.Y + b.Y) / 2;
    float dy2 = c.X - b.X;
    float dx2 = -(c.Y - b.Y);

    // See where the lines intersect.
    bool lines_intersect, segments_intersect;
    PointF intersection, close1, close2;
    FindIntersection(
        new PointF(x1, y1), new PointF(x1 + dx1, y1 + dy1),
        new PointF(x2, y2), new PointF(x2 + dx2, y2 + dy2),
        out lines_intersect, out segments_intersect,
        out intersection, out close1, out close2);
    if (!lines_intersect)
    {
        MessageBox.Show("The points are colinear");
        center = new PointF(0, 0);
        radius = 0;
    }
    else
    {
        center = intersection;
        float dx = center.X - a.X;
        float dy = center.Y - a.Y;
        radius = (float)Math.Sqrt(dx * dx + dy * dy);
    }
}

This code gets calculates the point (x1, y1) halfway between the first two points. It then calculates the vector <dx1, dy1> pointing in the direction of the perpendicular bisector for those points. It repeats those steps to find the perpendicular bisector of the second and third points.

The program then uses the FindIntersection method described in the post Determine where two lines intersect in C# to see where the two bisectors intersect.

If the bisectors don’t intersect, then the points are colinear so the three original points don’t define a circle. In that case the method displays an error message. Otherwise it sets the circle’s center and radius and returns.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in algorithms, drawing, graphics, mathematics and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

9 Responses to Draw a circle through three points in C#

  1. E. Anderson says:

    This takes me back to my high school days in beginning drafting where Mr. Holtz showed us how to make a circle given any three points on a piece of paper. For something so simple it still amazes me.

  2. Rod Stephens says:

    I had forgotten all about doing that with a straight edge and compass!

  3. Pingback: Find a minimal bounding circle of a set of points in C# -

  4. Dusty says:

    You write such excellent code that exactly solves the problems I have. It easy to follow, works so well and you have been doing this for years. I think I still have your VB6 algorithms book.

    Thanks,

  5. RodStephens says:

    Thanks for the kind words!

    I have a new algorithms book. It’s not quite the same and uses pseudocode instead of a particular programming language, but it covers some fun topics!

  6. Pingback: geometry - c# how to detect intersecting circles as separate from edgepoints - c++

  7. Matteo says:

    thanks a lot – you saved me a lot of time studying and writing code

  8. Eddie Bole says:

    How could you add “a drag one of the 3 small circles to alter the size the big circle” feature?

    • RodStephens says:

      Many drawing examples let the user select areas or points. You need to decide what method you want to use. Usually I pick whatever seems easiest and you can modify the program if you want a different method.

      For this example, you could place three initial circles and let the user drag them around. (You could even let the user make the initial placement, although that would mean the program would need to have two placement modes so it would be more complicated.)

      To let the user drag the circles, you would use MouseDown, MouseMove, and MouseUp event handlers. The MouseDown event handler would see if the point was over a circle and start dragging it if was. The MouseMove event handler would move the point if a drag was in progress. MouseUp would just end the drag.

      You would then update the big circle either in MouseMove if you wanted to update the picture continuously (I would probably do that) or in MouseUp if you only want to updater when the drag was finished.

      See this post for an example of dragging.

      Draw and move line segments in C#

      Give it a try and let me know if you get stuck.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.