Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Line intersects rectangle?

Hi,

How can I test if a line (defined by two x,y endpoints) intersects a rectangle?

I have a bunch of rectangles draw on a screen, and want to know if a user's mouse intersects any of them when they move the mouse. I only get mouse move messages every so often, so if the user moves the mouse fast enough, most of the mouse location points are missed. So I'm looking to figure out which rectangles could have been intersected between two mouse points.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
http://www.siggraph.org/education/materials/HyperGraph/scanline/clipping/cliplb.htm
says that Liang-Barsky is a 36% speed improvement over  Cohen -Sutherland for 2D lines
...the questioner is not clipping,  just detecting incidence with the rectangular.
that's what line clipping algorithms do
...line clipping gives the clipped line segment. That is different than just detecting if the line crosses the rectangle.