I have a line from A to B and a circle positioned at C with the radius R.

What is a good algorithm to use to check whether the line intersects the circle? And at what coordinate along the circles edge it occurred?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Taking
Compute:
d = L – E ( Direction vector of ray, from start to end )
f = E – C ( Vector from center sphere to ray start )
Then the intersection is found by..
Plugging:
P = E + t * d
This is a parametric equation:
Px = Ex + tdx
Py = Ey + tdy
into
(x – h)2 + (y – k)2 = r2
(h,k) = center of circle.
to get:
x2 – 2xh + h2 + y2 – 2yk + k2 – r2 = 0
x = ex + tdx
y = ey + tdy
( ex + tdx )2 – 2( ex + tdx )h + h2 +
( ey + tdy )2 – 2( ey + tdy )k + k2 – r2 = 0
ex2 + 2extdx + t2dx2 – 2exh – 2tdxh + h2 +
ey2 + 2eytdy + t2dy2 – 2eyk – 2tdyk + k2 – r2 = 0
t2( dx2 + dy2 ) +
2t( exdx + eydy – dxh – dyk ) +
ex2 + ey2 –
2exh – 2eyk + h2 + k2 – r2 = 0
t2( d · d ) + 2t( e · d – d · c ) + e · e – 2( e · c ) + c · c – r2 = 0
Where d is the vector d and · is the dot product.
t2( d · d ) + 2t( d · ( e – c ) ) + ( e – c ) · ( e – c ) – r2 = 0
t2( d · d ) + 2t( d · f ) + f · f – r2 = 0
So we get:
t2 * (d · d) + 2t*( f · d ) + ( f · f – r2 ) = 0
So solving the quadratic equation: