Calculate the formula for a circle selected by the user in C#

Calculate the formula for a circle

This example shows how to calculate the formula for a circle that the user has selected by clicking and dragging. The program uses the usual MouseDown, MouseMove, and MouseUp event handlers to let the user draw the circle. The only trick is that it makes the width and height of the object it draws the same, so the result is a circle and not an ellipse.

The following code shows the part of the form’s Paint event handler that draws a new circle while the user is selecting it.

// Draw the new circle if we are drawing one.
if (DrawingCircle)
{
    // Make it a circle.
    int diameter = Math.Max(
        Math.Abs(StartX - EndX), Math.Abs(StartY - EndY));
    e.Graphics.DrawEllipse(Pens.Red,
        Math.Min(StartX, EndX), Math.Min(StartY, EndY),
        diameter, diameter);
}

Here StartX and StartY are the coordinates of the point where the user pressed the mouse down. The values EndX and EndY are the mouse’s current location during the click-and-drag.

This code uses the maximum of the selected area’s width and height as the circle’s diameter. It uses the minimum X and Y coordinates of the selected area as the upper left corner for the circle and draws the circle.

The rest of the code that selects a circle is fairly straightforward. Download the example to see the details.

The more interesting code is in the following GetCircleFormula method.

// Get the equation for this circle.
private void GetCircleFormula(RectangleF rect,
    out float dx, out float dy, out float r)
{
    dx = rect.X + rect.Width / 2f;
    dy = rect.Y + rect.Height / 2f;
    r = rect.Width / 2f;
}


As its name implies, this method calculates the circle’s formula. The figure to the right shows the geometry of a circle.

The GetCircleFormula method finds the center of the circle (Dx, Dy). It divides the circle’s width by 2 to get the radius.

Now the program can display the equation in a Label.



Sometimes it’s convenient to use another type of equation for a circle. Using the equations on the left, you can solve the original equation to make an equation giving y as a function of x.

Note that taking the square root means there are two possible solutions to the equation, one where you take the positive root and one where you take the negative root. The positive and negative roots give the upper and lower halves of the circle.

The following code shows how the program draws the circle after the user selects it. (This technique is really just used to show that we have the right equation and to show one method for drawing a circle. Using the Graphics object’s DrawEllipse method is much more efficient.)

// Draw the circle if we have one.
if (GotCircle)
{
    // Fill the circle.
    e.Graphics.FillEllipse(Brushes.LightBlue, Circle);

    // Plot the circle's equation.
    List points = new List();
    for (float x = Dx - R; x <= Dx + R; x++)
    {
        float radicand = x - Dx;
        radicand = R * R - radicand * radicand;
        if (radicand >= 0f)
        {
            points.Add(new PointF(
                x, (float)(Dy + Math.Sqrt(radicand))));
        }
    }
    for (float x = Dx + R; x >= Dx - R; x--)
    {
        float radicand = x - Dx;
        radicand = R * R - radicand * radicand;
        if (radicand > 0f)
        {
            points.Add(new PointF(
                x, (float)(Dy - Math.Sqrt(radicand))));
        }
    }
    e.Graphics.DrawPolygon(Pens.Blue, points.ToArray());
}

First the code fills the circle with light blue.

Next the program makes variable x go from the circle’s minimum X value to its maximum X value. It uses the function with the positive root to generate points along the top half of the circle. (Vocabulary word for the day: A radicand is the value inside a radical symbol.)

Then the program makes variable x go from the circle’s maximum X value down to its minimum X value. It uses the function with the negative root to generate points along the bottom half of the circle. The program generates these points in order of decreasing X value so they begin where the points on the top half of the circle left off.

Finally the program draws the points as a polygon. You can see in the picture at the top of this post that the blue outline fits the light blue interior of the circle, so you know the formula is correct.


Download Example   Follow me on Twitter   RSS feed   Donate




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

3 Responses to Calculate the formula for a circle selected by the user in C#

  1. Pingback: Calculate the formula for an ellipse selected by the user in C# -

  2. kavin says:

    hi, here i have to draw multiple circle without erasing the previous one.how can i do that?

    • RodStephens says:

      If you mean you want to allow the user to draw multiple circles, then you’ll need to add some sort of data structure to hold the circle data. For example, you could use a List to store their centers and a List to store their radii.

      When you create a new circle, you would add the center and radius to the lists.

      You can decide how you want to display the formulas. For example, you could display them as in:

      (x - 123.45)^2 + (y - 76.54)^2 = 35.79^2

      And display the equations in a ListBox.

Leave a Reply

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