line segment circle intersection
http://astronomy.swin.edu.au/~pbourke/geometry/sphereline/
I have a line segment and a circle in 2D-space and want to determine if they intersect. I use this tutorial and it works fine for me until it comes to deciding whether the intersection point(s) is on the actual line intersection or not.
The tutorial uses this forumla:
line segment from P1(x1, y1) to P2(x2, y2)
circle center P3(x3, y3)
u = (x3 - x1) * (x2 - x1) + (y3 - y1) * (y2 - y1) /
(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)
If u is not between 0 and 1 then the point is not an the line segment, but this doesn''t work for me.
They calculate the point on the line segment that is closest to the center of the circle.
if you want to test if the line segment and the circle are intersecting, use the calculated u and find the point on the line that it corresponds with. (called P in the diagram)
now calculate the distance between that point P and the center of your circle P3. this is the closest distance from your line segment to P3
if (distance > radius) -> no intersection
now there could be the case the line is completely inside the circle. if you want to test against this, too, calculate the distances of P1 and P2 to the center of your circle.
At least one of these distances has to be > than the radius.
if both are > radius (and the first test passed), you intersect twice with the circle
if both are < radius, your line segment is completly inside the circle.
hope this helps.
if you want to test if the line segment and the circle are intersecting, use the calculated u and find the point on the line that it corresponds with. (called P in the diagram)
now calculate the distance between that point P and the center of your circle P3. this is the closest distance from your line segment to P3
if (distance > radius) -> no intersection
now there could be the case the line is completely inside the circle. if you want to test against this, too, calculate the distances of P1 and P2 to the center of your circle.
At least one of these distances has to be > than the radius.
if both are > radius (and the first test passed), you intersect twice with the circle
if both are < radius, your line segment is completly inside the circle.
hope this helps.
-----The scheduled downtime is omitted cause of technical problems.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement