[Math] How to calculate the intersection(s) of a straight line and a circle

circles

The basic equation for a straight line is $y = mx + b$, where $b$ is the height of the line at $x = 0$ and $m$ is the gradient. The basic equation for a circle is $(x – c)^2 + (y – d)^2 = r^2$, where $r$ is the radius and $c$ and $d$ are the $x$ and $y$ shifts of the center of the circle away from $(0,\ 0)$.

I'm trying to come up with an equation for determining the intersection points for a straight line through a circle. I've started by substituting the "y" value in the circle equation with the straight line equation, seeing as at the intersection points, the y values of both equations must be identical. This is my work so far:

$$(x – c)^2 + (mx + b – d)^2 = r^2$$
$$x^2 + c^2 – 2xc + m^2x^2 + (b – d)^2 + 2mx(b – d) = r^2$$
I'll shift all the constants to one side
$$x^2 – 2xc + m^2x^2 + 2mx(b – d) = r^2 – c^2 – (b – d)^2$$
$$(m^2 – 1)x^2 + 2x(m(b – d) – c) = r^2 – c^2 – (b – d)^2$$

That's as far as I can get. From what I've gathered so far, I ought to be able to break down the left side of this equation into a set of double brackets like so:
$$(ex + f)(gx + h)$$
where $e,\ f,\ g$ and $h$ are all constants. Then I simply have to solve each bracket for a result of $0$, and I have my $x$ coordinates for the intersections of my two equations. Unfortunately, I can't figure out how to break this equation down.

Any help would be appreciated.

Best Answer

Let's say you have the line $y = mx + c$ and the circle $(x-p)^2 + (y-q)^2 = r^2$.

First, substitute $y = mx + c$ into $(x-p)^2 + (y-q)^2 = r^2$ to give

$$(x-p)^2 + (mx+c-q)^2 = r^2 \, . $$

Next, expand out both brackets, bring the $r^2$ over to the left, and collect like terms:

$$(m^2+1)x^2 + 2(mc-mq-p)x + (q^2-r^2+p^2-2cq+c^2) = 0 \, .$$

This is a quadratic in $x$ and can be solved using the quadratic formula. Let us relabel the coefficients to give $Ax^2 + Bx + C = 0$, then we have

$$x = \frac{-B \pm \sqrt{B^2-4AC}}{2A} \, . $$

If $B^2-4AC < 0$ then the line misses the circle. If $B^2-4AC=0$ then the line is tangent to the circle. If $B^2-4AC > 0$ then the line meets the circle in two distinct points.

Since $y=mx+c$, we can see that if $x$ is as above then

$$y = m\left(\frac{-B \pm \sqrt{B^2-4AC}}{2A}\right) + c \, . $$

EDIT: The lines $y=mx+c$ do not cover the vertical lines $x=k$. In that case, substitute $x=k$ into $(x-p)^2+(y-q)^2=r^2$ to give

$$y^2 - 2qy + (p^2+q^2-r^2 - 2kp+k^2) = 0$$

This gives a quadratic in $y$, namely $y^2+By+C=0$, where $B=-2q$ and $C=p^2+q^2-r^2 - 2kp+k^2$. Solve using the Quadratic Formula. The solutions are $(k,y_1)$ and $(k,y_2)$, where the $y_i$ are solutions to $y^2+By+C=0$.