How to tell if a line segment intersects with a circle?

Find the intersections of the line containing the segment and the circle. This amounts to solving a quadratic equation. If there are no intersections (i.e. the solutions of the corresponding equation are non-real), then your segment does not intersect the circle. Now, if there are intersections, see whether they are inside the segment or not.

To implement this, let $P$ and $Q$ be the endpoints of your segment and $C$ and $r$ be the center and the radius of your circle. Then every point of the line through $P$ and $Q$ is given by the formula $$t P + (1-t) Q$$ for exactly one value of $t\in\mathbb R$, and the points in the segment are precisely those for which the corresponding $t$ is in the interval $[0,1]$.

Now the point corresponding to $t\in\mathbb R$ is on the circle if and only if $$\langle t P + (1-t) Q - C,t P + (1-t) Q - C\rangle = r^2,$$ where $\langle\mathord\cdot,\mathord\cdot\rangle$ is the usual inner (dot) product of vectors. If you solve this equation—this is easy, as it is a quadratic equation for $t$—, you find the $t$'s corresponding to the points of intersection of the circle and the line, and if at least one of those $t$'s belong to the interval $[0,1]$, then the segment intersects the circle.

Later: I tried to have Mathematica do the computation for me. Assuming $C=(0,0)$ and $r=1$, as we may up to translation and rescaling, and letting $P=(p1,p2)$, $Q=(q1,q2)$, the following computes the $t$'s:

P = {p1, p2};
Q = {q1, q2};
ts = t /. Solve[Norm[t P + (1 - t) Q]^2 == 1, t];

This ends up with the variable ts having the value

$$\left\{\frac{-p_1 q_1-p_2 q_2-\sqrt{p_1^2 \left(-q_2^2\right)-2 p_1 q_1+2 p_2 p_1 q_1 q_2-p_2^2 q_1^2-2 p_2 q_2+p_1^2+p_2^2+q_1^2+q_2^2}+q_1^2+q_2^2}{-2 p_1 q_1-2 p_2 q_2+p_1^2+p_2^2+q_1^2+q_2^2},\frac{-p_1 q_1-p_2 q_2+\sqrt{p_1^2 \left(-q_2^2\right)-2 p_1 q_1+2 p_2 p_1 q_1 q_2-p_2^2 q_1^2-2 p_2 q_2+p_1^2+p_2^2+q_1^2+q_2^2}+q_1^2+q_2^2}{-2 p_1 q_1-2 p_2 q_2+p_1^2+p_2^2+q_1^2+q_2^2}\right\}$$

(This was a huge formula, which will likely not fit on your browser window...) If the expression inside the square roots is negative, there are no real $t$'s, so the line (hence, a fortiori the segment) and the circle are disjoint. If not, now we need to see if at least one of the roots in in $[0,1]$.

If I now tell Mma to tell me when then first of the roots is in $[0,1]$, by telling her to compute

Reduce[0 <= ts[[1]] <= 1, Reals]

it works for a while, and comes up with a huge answer, presumably equivalent to the original

0 <= ts[[1]] <= 1

Can anyone make sense of the huge answer? (if one asks instead for the more meaningful

Reduce[0 <= ts[[1]] <= 1 || 0 <= ts[[2]] <= 1, t, Reals]

the same thing happens)

PS: Please notice that I have made absolutely no attempt at being fast or particularly smart with this idea: it is just the straightforward was to set up the problem and solve it.


Maybe this is what you are getting at... Given a line through points $(x_1,y_1)$ and $(x_2,y_2)$, and given a single point $p$ not necessarily on the line, we want to find out: If we draw a circle of radius $r$ around $p$, does that circle intersect the line?

If this is what you want then you are looking for the perpendicular distance from the point to the line.

The derivation on MathWorld may not be want you want to look at right now considering your stated familiarity with so many symbols - so let's just get right down to it.

Given that your line passes through the two points $(x_1,y_1)$ and $(x_2,y_2)$, a point $(a,b)$ not necessarily on the line, and some radius $r$ around the point $(a,b)$, in order to see if your point $(a,b)$ is within $r$ from the line, you need to check

if $r \geq \\frac{|(x_1 - x_2)(a-x_1) + (y_2 - y_1)(b-y_1)|}{\sqrt{(x_1-x_2)^2 + (y_2-y_1)^2}}$

then $(a,b)$ is within $r$ of the line (taken as an orthogonal distance).

EDIT: I don't think this will work if you are working with just a line segment... This solution assumes your line is extended infinitely in both directions. (update: there are scenarios where this would fail) I think the case of the segment itself being further away than $r$ can be taken care of in cases before you arrive at the "if" statement above. It should be something along the lines of

if $r \leq \sqrt{(x_1 - a)^2 + (y_1 - b)^2}$ or $r \leq \sqrt{(x_2-a)^2 + (y_2 - b)^2}$

then the segment is too far away to even consider checking the longer inequality above. (again, the previous if/then is insufficient... still thinking)

Final Edit: This approach has been adapted and completed far more satisfactorily by Isaac. I will offer a second approach that may be exactly what Mariano did, but I'll try to keep it short sweet and simple.


If I understood what you want correctly, you need to find conditions on a line segment so that it lies entirely outside a circle; a possible reason for confusion is that mathematicians tend to call "circle" just the one-dimensional boundary of the "disk" and they call "disk" its interior. In this terminology, I think that you want the segment to lie completely outside of the closed disk; this is the question that I will an answer.

First, a geometric description of the answer. Let $P$ and $Q$ be the endpoints of the segment, let $C$ be the center of the circle and let $r$ be the radius of the circle $C$.

1) We rule out the possibility that at least one among $P$ and $Q$ is contained inside the closed disk.

2) If in the triangle $PQC$ one of the two angles on the segment $PQ$ is not acute, then moving along the segment $PQ$ and away from the non-acute angle the distance from the center $C$ increases, so that there is no intersection in this case because of 1). Otherwise, the two angles on the segment $PQ$ are acute, so that the minimum distance between the center $C$ and the points on the segment $PQ$ is achieved inside the segment. In this case (i.e. when the two angles on $PQ$ are acute), we make sure that the distance between the center $C$ and the line joining $P$ and $Q$ is larger than $r$. In view of 1) and the previous discussion this implies that there is no contact between the segment and the disk.

Translation into formulas:

1) $dist(P,C)>r$ and $dist(Q,C)>r$;

2) if the inequalities $PC \cdot PQ > 0$ and $QC \cdot QP > 0$ hold, then also the inequality $||PC \times PQ|| > r ||PQ||$ must hold (in the formulas, we denote by $\cdot$ the scalar product, by $\times$ the cross product, and by $||-||$ the length of a vector).

Explicit equations, assuming that $C$ is the origin (which you can always achieve by translations):

1) $x_P^2+y_P^2 > r^2$ and $x_Q^2+y_Q^2 > r^2$;

2) if both $x_P^2 + y_P^2 \geq x_P x_Q + y_P y_Q$ and $x_Q^2 + y_Q^2 \geq x_P x_Q + y_P y_Q$ hold, then check that also $(x_Py_Q-x_Qy_P)^2 > r^2 ((x_P-x_Q)^2 + (y_P-y_Q)^2)$ holds.


If I understand your edit correctly, you have a point $C=(x_c,y_c)$ and the line segment with endpoints $P=(x_1,y_1)$ and $Q=(x_2,y_2)$ and you want to know whether or not $C$ is within a distance $r$ of any point on the line segment $\overline{PQ}$.

Working from Tom Stephens's answer, let's look at the whole line $\overleftrightarrow{PQ}$ containing the line segment $\overline{PQ}$. The distance between $C$ and $\overleftrightarrow{PQ}$ is $d=\frac{|(x_1 - x_2)(x_c-x_1) + (y_2 - y_1)(y_c-y_1)|}{\sqrt{(x_1-x_2)^2 + (y_2-y_1)^2}}$.

(1) If $d>r$, then $C$ is too far from $\overleftrightarrow{PQ}$ to be within $r$ of any point on $\overline{PQ}$, finished.

If $d\le r$, then $C$ is close enough to $\overleftrightarrow{PQ}$, but may not be close enough to $\overline{PQ}$. $d$ is the perpendicular distance from $C$ to $\overleftrightarrow{PQ}$. Consider two cases—first, that $C$ and $\overline{PQ}$ are arranged such that the perpendicular distance is to a point on $\overline{PQ}$; second, that the perpendicular distance is to a point not on $\overline{PQ}$.

(2) In the first case, $\angle PQC$ and $\angle QPC$ each have measure ≤90°, so the cosine of each angle must be nonnegative so $\frac{(x_1-x_2)^2-(x_1-x_c)^2+(x_2-x_c)^2+(y_1-y_2)^2-(y_1-y_c)^2+(y_2-y_c)^2}{2\sqrt{((x_1-x_2)^2+(y_1-y_2)^2)((x_2-x_c)^2+(y_2-y_c)^2)}}\ge 0$ and $\frac{(x_1-x_2)^2+(x_1-x_c)^2-(x_2-x_c)^2+(y_1-y_2)^2+(y_1-y_c)^2-(y_2-y_c)^2}{2\sqrt{((x_1-x_2)^2+(y_1-y_2)^2)((x_1-x_c)^2+(y_1-y_c)^2)}}\ge 0$ (from the Law of Cosines). [ edit 2: After a more careful reading of damiano's answer, it occurred to me that these are much more hideously complicated than necessary. The denominators do not affect the sign at all and the numerators are somewhat simpler when expanded, so the two inequalities can be replaced by: $x_1x_2-x_1x_c+x_2x_c-x_2^2+y_1y_2-y_1y_c+y_2y_c-y_2^2\ge0$ and $x_1x_2+x_1x_c-x_2x_c-x_1^2+y_1y_2+y_1y_c-y_2y_c-y_1^2\ge0$. These are more clearly equivalent to damiano's vector-dot-product criteria. ] If both of these inequalities hold, then $d\le r$ is the distance from $C$ to $\overline{PQ}$, so $C$ is within $r$ of $\overline{PQ}$.

(3) If those two inequalities from the Law of Cosines do not both hold, then $d$ is not the distance from $C$ to any point on $\overline{PQ}$, so the shortest distance from $C$ to any point on $\overline{PQ}$ is the distance to the closer of $P$ and $Q$: $d'=\min(\sqrt{(x_1-x_c)^2+(y_1-y_c)^2},\sqrt{(x_2-x_c)^2+(y_2-y_c)^2})$. If $d'\le r$, then $C$ is within $r$ of $\overline{PQ}$.

edit (adding the numbers above, also):

Given Corey Ogburn's answer, I thought it might be helpful to add a diagram and discuss what parts of my original answer address which regions. The numbers below discuss the correspondingly-numbered part of my original answer, above.

diagram

(1) If $d>r$, then the point $C$ is in the beige region.

(2) $d\le r$, so if $\angle PQC$ and $\angle QPC$ each have measure ≤90°, then $C$ is in the purple region.

(3) $C$ is not in the beige or purple regions, so we only need to see whether it's in the blue or red regions (based on whether $C$ is close enough to the endpoints of the segment).