[Math] Algorithm to find a line segment is passing through a circle or not

circlescollision detection

I have a line segment between two points P1 (X1,Y1) and P2 (X2,Y2). And I have a circle at point Q(Xq , Yq) with radius R . Can I have an equation in which I can put these values and the result shows me either that line segment passes through the circle or not.

Any help would be greatly appreciated. Thanks in advance.

Best Answer

Here is another suggestion.

  1. Form the equation of the straight line $P_1P_2$, which is an equation involving $x, y, x_1, y_1, x_2, y_2$.

  2. Calculate $d$, the perpendicular distance of $P_1P_2$ from $Q$, which is an expression involving $x_1, y_1, x_2, y_2, x_q, y_q$

  3. Do $R – d$, and make appropriate conclusion.

Added the following further detection mechanism

If d > R, there will be no intersection at all.

We now concentrate on the case when d < R and discuss the various types of the “could-be” intersections

enter image description here

Type-1 If both $O_1P_1 – R$ and $O_1P_2 – R$ are negative, then the line segment lies completely inside the circle and therefore there is no intersection.

Type-2 If $(O_1P_1 – R)*(O_1P_2 – R)$ is negative, then there is an intersection.

Type3 and Type-4 together

They both have the characteristic of both $O_1P_1 – R$ and $O_1P_2 – R$ are positive. Using the standard result of 2-point survey, we have

$$ \frac {L}{d} = (\frac {1}{\tan \beta} - \frac {1}{\tan \alpha})$$

Re-arranging terms, we have $$ \tan \alpha = (\frac {1}{\tan \beta} - \frac {L}{d})^{-1}$$

Substituting the values in it and if $\tan \alpha$ is positive, we have Type-3 result. That is no intersection.

Otherwise is Type-4 with two intersection points.


Note-1 With the values given, one can always determine which angle is $\beta$.

Note-2 The tangential case can be treated in a similar fashion, and hence skipped.