# Intersection of a line and a circle

## Introduction

Given: line parameters a, b and c and circle center $M=(xm,ym)$ and circle radius r.

General linear equation with two variables:

$ax + by + c = 0$

We can write this as an equation of a straight line like:

$b≠0: y = mx + y0 and b=0: x = k$ with:
$m = -a / b, y0 = -c / b, k = -c / a$

In case b ≠ 0, m is called the slope or gradient of the line and y0 is called the y-intercept. If b = 0, the line is a vertical line.

General circle equation:

$(x-xm)^2+(y-ym)^2 = r^2$ $x^2 - 2xx_m + x_m^2 + y^2 - 2yy_m + y_m^2 - r^2 = 0$ 

If b ≠ 0:

Substituting line equation $y = mx + y0$ in  results in an equation of the form:

$Ax^2 + Bx + C =0$

This equation's solutions for x are the x-coordinates of the intersection points $P = (xp , -a / b * xp - c / b)$ :

$xp = (-B ± sqrt(B^2 - 4AC))/2A$ with: $A = a^2 / b^2 +1$ $B = 2(ac / b^2 - xm + aym / b)$ $C = x_m^2 + y_m^2 + 2c/b ym + c^2/b^2 - r^2$

Let $D = B^2 - 4AC$ :

• If D < 0, then $x_p$ has no solutions; line does not intersect.
• If D = 0, then $x_p$ has exactly one solution; line is tangent to circle.
• If D > 0, then $x_p$ has two solutions; line intersects at two points.

If b = 0:

Substituting vertical line equation x = k in  results in an equation of the form:

$y^2 + By + C =0$

This equation's solutions for y are the y-coordinates of the intersection points $P = (-c / a , yp)$ :

$yp = (-B ± sqrt(B^2 - 4C))/2$ with: $B = -2 ym$ $C = x_m^2 + y_m^2 + 2c/a xm + c^2/a^2 - r^2$

Let $D = B^2 - 4C$ :

• If D < 0, then $y_p$ has no solutions; line does not intersect.
• If D = 0, then $y_p$ has exactly one solution; line is tangent to circle.
• If D > 0, then $y_p$ has two solutions; line intersects at two points.