# line intersecting rectangle

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

## Recommended Posts

Hi I have a rectangle and a line. The line is generated by knowing two vertexes and plotting a line between them. This results in the problem that the line itself will not be detected in any collisions without a special method. This special method is what I need help on. How do I find the intersection points (X and Y axis only since its 2D), of where the line cuts the rectangle. Also, if there are more than one intersection points, how do I find them all? Thanks for any help

##### Share on other sites
Here's a direct method that should be intuitive to you, although it won't be perfectly efficient. Consider a rectangle as a series of lines {l1 ... l4}. The intersection of a line with a line is straightforward; you're solving for two unknowns (the x, y coordinates of the intersection point) with two linear equations (the two lines). If you get more than one solution for a given line, then one of the lines of the rectangle is a line segment of the line you're testing. Otherwise, stop after you have two solutions; a given line can't intersect a given rectangle more than twice in two dimensions.

##### Share on other sites
Whoops. The AP was me, sorry.

• ### What is your GameDev Story?

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

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634075
• Total Posts
3015349
×