Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

readme

line segment circle intersection

This topic is 5683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[url]http://astronomy.swin.edu.au/~pbourke/geometry/sphereline/[/url] 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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!