Possible Duplicate:
Circle line collision detection
I have a problem. I need to find point A.
How can I do this best way?
Programming languale is Java.
Possible Duplicate:
Circle line collision detection
I have a problem. I need to find point A.
How can I do this best way?
Programming languale is Java.
Given: a circle with center C=[x2,y2] and radius R a line segment from C to B=[x1,y2] calculate their intersection.
This is easy since one of the endpoints is the center of the circle. You have to walk the distance of R from C towards B. The distance guarantees you will end up on the circle and the direction guarantees you will end up on the ray C->B. You still need to check if the intersection lies on the segment.
Here is the pseudocode if you have a vector library
- offset = B-C
- if length_square(offset) < R^2 output "no intersection"
- offset_a = normalize(B-C) * R
- output C + offset_a
Without a library, the code is longer:
- off_x = x1-x2;
- off_y = y1-y2;
- ls = off_x*off_x + off_y*off_y
- if ls < R*R
-- return an empty array, meaning "no intersections"
- scale = R / sqrt(ls)
- res_x = off_x * scale + x2
- res_y = off_y * scale + y2
- return [[off_x, off_y]]