Line-Circle Collision Detection

circle points1 Line Circle Collision DetectionLine and circles can be easily defined through equations, so their intersection points should be easy to find, right?
Well compared to the previous collisions it definitely involves more calculations.

First we need to take into account the linear and circle equations, respectively:

y = mx + b
(x – Cx)2 + (y – Cy)2 = r2



Before we can replace the y value on the circle equation we first have to solve it by using this property:

(a – b)2 = a2 -2ab + b2

Which will lead to:

x2 – 2xCx + Cx2 + y2 – 2yCy + Cy2 = r2

And after we replace the y value with the linear equation the result will be:

x2 + (mx)2 – 2xCx + 2mxb -2mxCy + Cx2 + Cy2 -2bCy + b2 = r2

But because this is a quadratic equation we can use this formula:

ax2 + bx + c = 0
x = -b ± √(b2 – 4ac)



Which will result in:

(m2 + 1)x2 + (2(-Cx + m(b – Cy)))x + (Cx2 + Cy2 – 2bCy + b2 – r2) = 0
x = -2(-Cx + m(b – Cy)) ± √((2(-Cx + m(b – Cy)))2 -4(m2 + 1)(Cx2 + Cy2 – 2bCy + b2 – r2))
2(m2 + 1)

tangent Line Circle Collision DetectionNow we’re ready to find the coordinates of both intersection points. But what if there’s only one? Or no intersection at all?

There’s an interesting little section of the quadratic formula that allows us to predict if the line is colliding with the circle or not:

Δ = b2 – 4ac

If the value you want to find the square root of is negative (which would lead to an irrational number) then the collision isn’t occurring. But if the value is 0 then the line is a tangent. And any other positive value means there’s 2 intersection points.

if (Δ < 0) {           // The line isn’t intersecting the circle

if (Δ == 0) {
          // The line is tangent to the circle

if (Δ > 0) {
          // The line intersects the circle

circleVertical Line Circle Collision DetectionAll these steps work perfectly with any type of line… except one. The vertical line!

This is because its slope is a mathematical impossibility.
So in order to determine its intersection points with the circle we need to find a different formula:

x = k
y = Cy ± √(r2 – (k – Cx)2)


As for determining if the collision is occurring or not, the process is pretty much the same. You just need to determine if the value inside the square root is negative or not.

You may also like...