[Math] Circle and Line segment intersection

circles

I have a line segment (begin $(x_1,y_1)$, end $(x_2,y_2)$, with $D=5$, let’s say) and a circle (radius $R$, center $(x_3,y_3)$)

How can I check that if my line segment intersects my circle?

picture

http://kepfeltoltes.hu/120129/inter_www.kepfeltoltes.hu_.png

Best Answer

The points $(x,y)$ on the line segment that joins $(x_1,y_1)$ and $(x_2,y_2)$ can be represented parametrically by $$x=tx_1+(1-t)x_2, \qquad y=ty_1+(1-t)y_2,$$ where $0\le t\le 1$. Substitute in the equation of the circle, solve the resulting quadratic for $t$. If $0\le t\le 1$ we have an intersection point, otherwise we don't. The value(s) of $t$ between $0$ and $1$ (if any) determine the intersection point(s).

If we want a simple yes/no answer, we can use the coefficients of the quadratic in $t$ to determine the answer without taking any square roots.