• ### What is your GameDev Story?

#### Archived

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

# line segment circle intersection

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

## 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 on other sites
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 on other sites
thank you, i send you an email with further questions

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015564
×