# Circle Line segment intersection

I have a circle with radius r and center $(c_x, c_y)$. I have a line segment $(x_1, y_1)$ and $(x_2, y_2)$ given $(x_2, y_2)$ is always a point inside the circle.

I am trying to find the intersection between the circle and the line segment. I have tried the tangent formula that mentioned here i am not sure if it works in my case. • I'm assuming (x1,y1) is outside of the circle? Aug 12, 2014 at 0:38
• @3.141592653589793 Yes... Aug 12, 2014 at 15:10

The equation of the circle is:

$$(x-c_x)^2+(y-c_y)^2=r^2$$

The equation of the line is:

$$y-y_1=\frac{y_2-y_1}{x_2-x_1} (x-x_1) \ \ \ (*)$$

To find the intersection solve at the $(*)$ for $y$ and replace it at the equation of the circle.

Use parametrization of segment. This function describes each point on segment for $t \in [0,1]$

$$f(t)=(x_1,y_1)+t((x_2,y_2)-(x_1,y_1))=(t_1,t_2)$$

Now you now how to calculate distance $(x,y)$ from $(c_x,c_y)$, it's:

$$dist(x,y)=\sqrt{(c_x-x)^2+(c_y-y)^2}$$

So you are looking for such a $t$, for which

$$dist(f(t))=r$$

Where $r$-radius. If you substitute $f(t)=(x_1,y_1)+t((x_2,y_2)-(x_1,y_1))=(t_1,t_2)$ you get quadratic equation, next you calculate $t$ and use $t$ to get intersection (you can get 0,1 or two solutions).

WLOG, the center of the circle is the origin (otherwise, translate all points to make it such).

And WLOG, the length of the segment is $1$ (otherwise, divide all coordinates by that length).

Plug the parametric equation of the line segment in the implicit equation of the circle,

$$x^2+y^2=(x_1+t\delta x)^2+(y_1+t\delta y)^2=r^2.$$

$$t=-(x_1\delta x+y_1\delta y)\pm\sqrt{(x_1\delta x+y_1\delta y)^2-(x_1^2+y_1^2-r^2)}.$$

The rest is easy. (don't forget to undo the transformations.)

WLOG, the center of the circle is the origin (otherwise, translate all points to make it such).

Now scale the circle so that the radius is $1$ (not the line segment as Yves Daoust did!).

Let the line by given with $y=mx+c$ where $m=(y_2-y_1)/(x_2-x_1)$ and $c=y_1 - mx_1$ then if \begin{align}m^2 + 1 - c^2 > 0\end{align} the line intersects the circle, if \begin{align}m^2 + 1 - c^2 < 0\end{align} it does not intersect and if \begin{align}m^2 + 1 - c^2 = 0\end{align} it touches it in one point.

This is a special case of the problem of intersecting a line with an ellipse.

As is given on that page the coordinates of intersection (if they exist) are \begin{align}x_{1,2}=&\frac{-mc\pm\sqrt{m^2+1-c^2}}{m^2+1},&y_{1,2}=&\frac{c\pm m\sqrt{m^2+1-c^2}}{m^2+1}\\\end{align}