Divide a circle horizontally

356 Views Asked by At

I'm in the midst of programming, and I'd like to know if this is possible.

Given a circle which we know three facts:

  1. The center point;
  2. The radius; and
  3. The Y coordinate of a line parallel to the X axis,

find the two X coordinates where the circle crosses said line.

I've attempted to mess about with the circle's equation,

$(x-h)²+(y-k)²=r²$

with the Circle center being $(h,k)$ and the radius $r$.

In short, I'm dividing a circle horizontally.

My attempts to get a functional formula (e.g. $x=fancyPants$) seem to be failing. I cannot math good.

Is this possible or am I unwittingly jumping into ridiculously complex math that will blow up the program with trial and error?

If possible, I would like a formula for an ellipsis as well.

Cheers

1

There are 1 best solutions below

2
On BEST ANSWER

Let the circle's center have coordinates $(h, k)$ and radius $r$. Then the points with $y$-coordinate $y_0$ (if there are any) have $x$-coordinates that satisfy the equation

$$ (x-h)^2 + (y_0-k)^2 = r^2 $$

The only unknown here is $x$, so we write

$$ (x-h)^2 = r^2-(y_0-k)^2 $$

and then

$$ x-h = \pm \sqrt{r^2-(y_0-k)^2} $$

Finally, we add $h$ to both sides to get

$$ x = h \pm \sqrt{r^2-(y_0-k)^2} $$

This will yield two (real) solutions when $r > |y_0-k|$, one solution when $r = |y_0-k|$, and no (real) solutions when $r < |y_0-k|$, where $|z|$ is the absolute value of $z$.

Pseudocode is

if (r < fabs(y0-k))
    return {NaN};
else if (r == fabs(y_0-k))
    return {h};
else {
    d = sqrt(r^2-(y0-k)^2);
    return {h-d, h+d};
}