Intersection of a line and a circle
Introduction
Given: line parameters a, b and c and circle center $M=\left({x}_{m},{y}_{m}\right)$ 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:
$$\begin{array}{llll}\text{If}& b\ne 0& \text{:}& y=mx+{y}_{0}\\ \text{If}& b=0& \text{:}& x=k\end{array}$$
with:
$$\begin{array}{lll}m=-\frac{a}{b}\text{,}& {y}_{0}=-\frac{c}{b}\text{,}& k=-\frac{c}{a}\end{array}$$
In case b ≠ 0, m is called the slope or gradient of the line and y_{0} is called the y-intercept. If b = 0, the line is a vertical line.
General circle equation:
$${\left(x-{x}_{m}\right)}^{2}+{\left(y-{y}_{m}\right)}^{2}={r}^{2}$$ ⇔ $${x}^{2}-2x{x}_{m}+{{x}_{m}}^{2}+{y}^{2}-2y{y}_{m}+{{y}_{m}}^{2}+-{r}^{2}=0$$
If b ≠ 0:
Substituting line equation $y=mx+{y}_{0}$ in [1] results in an equation of the form:
$$A{x}^{2}+Bx+C=0$$
This equation's solutions for x are the x-coordinates of the intersection points $P=\left({x}_{p},-\frac{a}{b}{x}_{p}-\frac{c}{b}\right)$ :
$${x}_{p}=\frac{-B\pm \sqrt{{B}^{2}-4AC}}{2A}$$ with: $$A=\frac{{a}^{2}}{{b}^{2}}+1$$ $$B=2\left(\frac{ac}{{b}^{2}}-{x}_{m}+\frac{a}{b}{y}_{m}\right)$$ $$C={{x}_{m}}^{2}+{{y}_{m}}^{2}+\frac{2c}{b}{y}_{m}+\frac{{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 [1] 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=\left(-\frac{c}{a},{y}_{p}\right)$ :
$${y}_{p}=\frac{-B\pm \sqrt{{B}^{2}-4C}}{2}$$ with: $$B=-2{y}_{m}$$ $$C={{x}_{m}}^{2}+{{y}_{m}}^{2}+\frac{2c}{a}{x}_{m}+\frac{{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.