# How do I find the points of a circle?

Say you have a center of $(5, 5)$ and a radius of $2$. If you went for each x-value in $\{3, 4, 5, 6, 7\}$, how would you find the y value?

EDIT: I have this code in C#

for (int i = centerx - radius; i <= centerx + radius; i++)
{
double y0 = centery + Math.Sqrt((double)(radius ^ 2 - (i - centerx) ^ 2));
double y1 = centery - Math.Sqrt((double)(radius ^ 2 - (i - centerx) ^ 2));
int y2 = Convert.ToInt32(Math.Round(y0));
int y3 = Convert.ToInt32(Math.Round(y1));

This gives some weird results, it makes a weird line, not anything like a circle at all. Any help is appreciated.

EDIT2: Same thing as before

int x = 0;
{
array_x[x] = i;
x++;
}
for (int i = 0; i <= array_x.Length; i++)
{
double y0 = centery + Math.Sqrt((double)(radius ^ 2 - (array_x[i] - centerx) ^ 2));
double y1 = centery - Math.Sqrt((double)(radius ^ 2 - (array_x[i] - centerx) ^ 2));
int y2 = Convert.ToInt32(Math.Round(y0));
int y3 = Convert.ToInt32(Math.Round(y1));
-
See here. –  Daryl Dec 26 '12 at 20:56
The problem, I think, is in your defining i as you do: centerx - r. You want i to reference your x values. What you are doing, then, in the formulas, is squaring (centerx - radius - centerx) which means squaring radius. You want to square the (given-x_i - centerx). If you define an array called array_x consisting of the input x values, then i should range from first index to last index of array, and you'd use something like centery + - Math.Sqrt((double)(radius^2 - (array_x[i] - centerx)^2) in your y0, y1 –  amWhy Dec 26 '12 at 22:06
How would I do this? –  Cool12309 Dec 26 '12 at 22:23
I could tell you in JAVA, which seems close to C++ given your code. BTW, I don't get pinged unless you address @amWhy, simply use an unaddressed comment below MY answer. (Askers are pinged for any comment/answer on or below the posts. Answerers are pinged for any comment left below his/her answer, or when addressed @so-and-so.) –  amWhy Dec 26 '12 at 22:36
@amWhy Okay, I updated the post. However, it still does the weird line thing. (It also errors at the end, given y0 and y1 return NaN) –  Cool12309 Dec 26 '12 at 22:42

Hint: the equation for a circle is given by $$(x - a)^2 + (y-b)^2 = r^2$$

$$\text{OR}\quad y=b\pm \sqrt{r^2-(x-a)^2}$$

where $(a, b)$ is the center of the circle, and $r$ it's radius.

$(1)$ What is your center? So $a = b = \;?\;\;$ And radius $\;r=\;?\;\;$
$\quad\;$ Just substitute the values you have into the equation for a circle to define your circle.

$(2)$ Then use each of your given $x$-values to determine/solve for the corresponding $y$-values using
$\quad\;$the formula you arrive at in $(1)$. (You should find two $y$-values for each $x$.)

-
I am doing this for coding, so I need to find the value of y. In C#. –  Cool12309 Dec 26 '12 at 21:07
For center $(5, 10)$ radius $12$, $y = 10 \pm \sqrt{12^2 - (x - 5)^2} = 10 \pm \sqrt{144 - (x - 5)^2}$ –  amWhy Dec 26 '12 at 21:14

Hint: Your circle is $$(x-5)^2+(y-5)^2=2^2$$ Can you solve for $y$ (there will be two solutions in the end, one the upper semicirle and one in the lower one)? And even if you can't, you don't have to. Jut plug in $x$ and get the two corresponding values for $y$

-

You start with $$(x-5)^2+(y-5)^2=2^2$$ which gives $$y=5\pm \sqrt{4-(x-5)^2}$$ and you then substitute the various values of $x$.

-
Just for clarification, if the center was at (5, 10) and radius was 12, what would that be –  Cool12309 Dec 26 '12 at 21:11
@Cool12309 $$y=10\pm \sqrt{12^2-(x-5)^2}$$ –  Henry Dec 27 '12 at 1:10

I think you are asking the wrong question. Instead of asking "What is the $y$ for specified $x$?", ask "What $(x, y)$ pairs should I use to make the best looking circle?"

The simplest answer to this is to divide the circle into a number of equal circular segments with $(x_k, y_k) = (a+r \cos{k \theta}, b+r \sin{k \theta})$, for $k = 0$ to $n-1$ and $\theta = 2 \pi/n$, where $n$ is the number of point you want to have on the circle.

This can be optimized computationally so only a couple of multiplies and adds are needed after the first two points are computed, and you can also use Bresenham's Circle Generation algorithm (look it up) if you just want to draw a circle.

-