[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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 the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.