2
$\begingroup$

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)); 
  • 0
    let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/6859/discussion-between-cool12309-and-amwhy)2012-12-26

4 Answers 4

5

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


  • 0
    For center $(5, 10)$ radius $12$, $y = 10 \pm \sqrt{12^2 - (x - 5)^2} = 10 \pm \sqrt{144 - (x - 5)^2}$2012-12-26
2

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$

1

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

  • 0
    @Cool12309 $y=10\pm \sqrt{12^2-(x-5)^2}$2012-12-27
1

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.