# Circle and Line segment intersection

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

## 3 Answers

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.

You can do this analytically:

• write the equation of circle : $(x- x_R)^2 + (y - y_R)^2 = R^2$
• write the equation of the line which support the segment : $ax + by + c = 0$ , a, b, c depending of the extremas of your segment.
• compute (second order polynome) the intersection : there is 2 points maximum.
• check if intersection is in the segment

Enjoy ;-)

• more help please – lacroix Jan 29 '12 at 11:35

Compute the distance of the line passing through $(x_1,y_1)$ and $(x_2,y_2)$ from $(x_3,y_3)$. If it is less than $r$, then the line passing through the two points intersects the circle else not. If it does, you are still not sure if the line segment intersects or not (it can be the line that is intersecting), so just find the distance of the two points from the center. If exactly one is less than $r$, then it is intersecting. The solution won't involve solving any quadratic equation.

• But won't this test would fail if the line-segment is big enough that the line segment points (x_i,y_i) would fall on either sides of the circle but with the line-segment still passing through the circle ? – sinner Jan 5 '16 at 18:27