Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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[] array_x = new int[radius*2+1];
int x = 0;
for (int i = -radius; i <= radius; i++)
{
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));
share|improve this question
1  
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

4 Answers 4

up vote 6 down vote accepted

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$.)


share|improve this answer
    
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$

share|improve this answer

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$.

share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.