# How do I calculate the intersection(s) of a straight line and a circle?

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.

• Why don't you just solve for $x$? You know the constans, right? – M.B. Nov 4 '12 at 12:22
• I'm trying to develop an equation that will deduce what x is for any set of constants, not just a specific set. – Cambot Nov 4 '12 at 12:41

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

• Ah! Thank you! This certainly looks like it'll do what I'm trying to do! – Cambot Nov 4 '12 at 13:20
• Be careful to manage vertical line !!! – Eric Ouellet May 13 '16 at 19:26
• @EricOuellet Thank you, my friend. I think I've fixed it. – Fly by Night Mar 12 '19 at 17:51

You've got a quadratic in $x$. Use the discriminant to see if it has real solutions, and if so how many. Then solve the quadratic for $x$, and substitute the solution(s), if any, back into the equation for the line.

Here's an equation which works even if the line is vertical. This is useful from a computer programming perspective. Let's set up the system of equations using standard formulae:

$$\begin{cases} (x - x_0)^2 + (y - y_0)^2 = r^2 \\ Ax + By + C = 0 \end{cases}$$

where the circle with radius $$r$$ is centered at $$(x_0, y_0)$$; the line contains the points $$(x_1, y_1)$$ and $$(x_2, y_2)$$.

From the line equation, we know:

$$y = \frac{-Ax-C}{B} = - \frac{Ax+C}{B}$$

($$B \neq 0$$, we'll get into this in a second)

Therefore,

\begin{align*} (x - x_0)^2 + \left( - \frac{Ax+C}{B} - y_0 \right)^2 &= r^2 \\ x^2 - 2x_0x + {x_0}^2 + \frac{A^2 x^2 + 2ACx + C^2}{B^2} + 2y_0 \cdot \frac{Ax+C}{B} + {y_0}^2 &= r^2 \\ x^2 - 2x_0x + \frac{A^2 x^2 + 2ACx + C^2}{B^2} + 2y_0 \cdot \frac{Ax+C}{B} &= r^2 - {x_0}^2 - {y_0}^2 \end{align*}

Multiply both sides by $$B^2$$. \begin{align*} B^2 x^2 - (2B^2 x_0)x + A^2 x^2 + (2AC)x + C^2 + 2By_0(Ax+C) &= B^2(r^2 - {x_0}^2 - {y_0}^2) \\ (A^2 + B^2)x^2 + (2AC - 2B^2 x_0)x + (2AB y_0)x &= -C^2 - 2BCy_0 + B^2(r^2 - {x_0}^2 - {y_0}^2) \end{align*}

Therefore, we have a quadratic equation $$ax^2 + bx + c = 0$$ with:

$$\begin{cases} a = A^2 + B^2 \\ b = 2AC + 2AB y_0 - 2B^2 x_0 \\ c = C^2 + 2BC y_0 - B^2 (r^2 - {x_0}^2 - {y_0}^2) \end{cases}$$

where

$$\begin{cases} A = y_2 - y_1 \\ B = x_1 - x_2 \\ C = x_2 y_1 - x_1 y_2 \end{cases}$$

Now you can use $$\{ x = \frac{-b \pm \sqrt{\Delta}}{2a}, y= -\frac{Ax+C}{B} \}$$ to solve for the points.

The above equation doesn't work for $$B=0$$. Besides this, if $$|B|$$ is too small, then floating point computation gets inaccurate.

If $$B=0$$, or is too small, then we let $$x = -\frac{By+C}{A}$$, and sub it into the circle's formula:

\begin{align*} \left( - \frac{By+C}{A} - x_0 \right)^2 + (y - y_0)^2 &= r^2 \\ \frac{B^2 y^2 + 2BCy + C^2}{A^2} + 2x_0 \cdot \frac{By+C}{A} + y^2 - 2y_0 y &= r^2 - {x_0}^2 - {y_0}^2 \\ B^2 y^2 + 2BCy + C^2 + 2Ax_0 (By+C) + A^2y^2 - 2A^2 y_0 y &= A^2(r^2 - {x_0}^2 - {y_0}^2) \\ (A^2 + B^2)y^2 + (2BC + 2ABx_0 - 2A^2 y_0)y &= -C^2 - 2ACx_0 + A^2(r^2 - {x_0}^2 - {y_0}^2) \end{align*}

Thus, we have a quadratic equation $$ay^2 + by + c = 0$$ where

$$\begin{cases} a = A^2 + B^2 \\ b = 2BC + 2ABx_0 -2A^2 y_0 \\ c = C^2 + 2ACx_0 -A^2(r^2 - {x_0}^2 - {y_0}^2) \end{cases}$$

Have you checked the general solution on wolframalpha? You've already made your quadratic equation and so, you can get the roots of $x$ as the solutions that can then be used to get the values of $y$.