Draw a circle through three points in C#

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.
    float cx = (y1 * dx1 * dx2 + x2 * dx1 * dy2 - x1 * dy1 * dx2 - y2 * dx1 * dx2)
        / (dx1 * dy2 - dy1 * dx2);
    float cy = (cx - x1) * dy1 / dx1 + y1;
    center = new PointF(cx, cy);

    float dx = cx - a.X;
    float dy = cy - a.Y;
    radius = (float)Math.Sqrt(dx * dx + dy * dy);
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 11/9/2011 2:28 PM E. Anderson wrote:
    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.
    Reply to this
  • 11/9/2011 11:11 PM Rod Stephens wrote:
    I had forgotten all about doing that with a straight edge and compass!
    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.