# Intersection of a line and a circle

## Introduction

In this article it will be explained how to calculate the coordinates of the points ($P=\left({x}_{p},{y}_{p}\right)$) where a line and a circle intersect. The required equations will be derived and an interactive application is provided to calculate the intersection points of a given line and circle. A line and a circle have no intersection points, one intersection point (line is tangent to circle) or two intersection points 🙄.

## JavaScript application

Input mode:
Line parameters:

Circle parameters:

Circle with center M = (xm , ym) and radius r:

P1 =

P2 =

## Line equation

Let a, b and c be the line parameters in the general linear equation:

$ax+by+c=0$

We can rewrite this general form of the equation to the so called slope-intercept form, which is generally the form for representing an equation of a straight line in the Euclidean plane:

$\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}$ 

Here 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 ($x=k$).

## Circle equation

Let $M=\left({x}_{m},{y}_{m}\right)$ be the circle center and r be the circle radius in the general circle equation:

$\begin{array}{c}{\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\end{array}$ 

## Intersection points

Given that $y=-\frac{a}{b}x-\frac{c}{b}$ (equation ), means that the coordinates of the intersection points of a non-vertical line and a circle are $P=\left({x}_{p},-\frac{a}{b}{x}_{p}-\frac{c}{b}\right)$ (if any exists).

When the line is vertical ($x=-\frac{c}{a}$), the coordinates of the intersection points of a line and a circle are $P=\left(-\frac{c}{a},{y}_{p}\right)$ (if any exists).

### Intersection with non-vertical line

A point where a line and circle intersect (or touch) must be a point belonging to both the line and the circle. Substituting line equation  ($y=mx+{y}_{0}$) in circle equation  yields an equation in which x represents the x-coordinates of the intersection points (if any exists). Substitution results in an equation of the form:

$A{x}^{2}+Bx+C=0$

Solving this equation for x gives us the x-coordinates xp:

${x}_{p}=\frac{-B±\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 xp has no solutions; line and circle do not intersect.
• If D = 0, then xp has exactly one solution; line is tangent to circle.
• If D > 0, then xp has two solutions; line and circle intersect at two points.

### Intersection with vertical line

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

${y}^{2}+By+C=0$

Solving this equation for y gives us the y-coordinates yp:

${y}_{p}=\frac{-B±\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 yp has no solutions; line and circle do not intersect.
• If D = 0, then yp has exactly one solution; line is tangent to circle.
• If D > 0, then yp has two solutions; line and circle intersect at two points.

Calculate the intersection points (if they exist) of $y=3x-2$ and ${\left(x-6\right)}^{2}+{y}^{2}=36$.