Sign in to follow this  
ClaF

line intersecting rectangle

Recommended Posts

ClaF    122
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this