line intersecting rectangle

Started by
1 comment, last by kSquared 18 years, 9 months ago
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
Advertisement
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.
Whoops. The AP was me, sorry.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}

This topic is closed to new replies.

Advertisement