Jump to content
  • Advertisement
Sign in to follow this  
OOBrad21

2d line box intersection

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, i need some help with line-box intersection Give 2 points - (X1,Y1), (X2,Y2) and a box (X, Y, width, height) the X, Y for the box is the upper left coords. If I was to draw a line between the two points, I need to know if that line goes through the box.....any tips/hints on how to write an efficient function for this? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
clip the line by the infinite lines parallel to the edges, and if there's something left of the line after that, the rect and the line intersect

Share this post


Link to post
Share on other sites
I am sure there is a faster way, but here some code which should do the job:


/** Check to see if a line intersects another */
bool inline LineIntersectLine( Vector2f v1, Vector2f v2, Vector2f v3, Vector2f v4 )
{
float denom = ((v4.y - v3.y) * (v2.x - v1.x)) - ((v4.x - v3.x) * (v2.y - v1.y));
float numerator = ((v4.x - v3.x) * (v1.y - v3.y)) - ((v4.y - v3.y) * (v1.x - v3.x));

float numerator2 = ((v2.x - v1.x) * (v1.y - v3.y)) - ((v2.y - v1.y) * (v1.x - v3.x));

if ( denom == 0.0f )
{
if ( numerator == 0.0f && numerator2 == 0.0f )
{
return false;//COINCIDENT;
}
return false;// PARALLEL;
}
float ua = numerator / denom;
float ub = numerator2/ denom;

return (ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f);
}


// Test to see if a line intersects a Rectangle
bool LineIntersectsRect( const Vector2f &v1, const Vector2f &v2, const Rect &r )
{
Vector2f lowerLeft( r.x, r.y+r.height );
Vector2f upperRight( r.x+r.width, r.y );
Vector2f upperLeft( r.x, r.y );
Vector2f lowerRight( r.x+r.width, r.y+r.height);
// check if it is inside
if (v1.x > lowerLeft.x && v1.x < upperRight.x && v1.y < lowerLeft.y && v1.y > upperRight.y &&
v2.x > lowerLeft.x && v2.x < upperRight.x && v2.y < lowerLeft.y && v2.y > upperRight.y )
{
return true;
}
// check each line for intersection
if (LineIntersectLine(v1,v2, upperLeft, lowerLeft ) ) return true;
if (LineIntersectLine(v1,v2, lowerLeft, lowerRight) ) return true;
if (LineIntersectLine(v1,v2, upperLeft, upperRight) ) return true;
if (LineIntersectLine(v1,v2, upperRight, lowerRight) ) return true;
return false;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by newera
I am sure there is a faster way, but here some code which should do the job:


/** Check to see if a line intersects another */
bool inline LineIntersectLine( Vector2f v1, Vector2f v2, Vector2f v3, Vector2f v4 )
{
float denom = ((v4.y - v3.y) * (v2.x - v1.x)) - ((v4.x - v3.x) * (v2.y - v1.y));
float numerator = ((v4.x - v3.x) * (v1.y - v3.y)) - ((v4.y - v3.y) * (v1.x - v3.x));

float numerator2 = ((v2.x - v1.x) * (v1.y - v3.y)) - ((v2.y - v1.y) * (v1.x - v3.x));

if ( denom == 0.0f )
{
if ( numerator == 0.0f && numerator2 == 0.0f )
{
return false;//COINCIDENT;
}
return false;// PARALLEL;
}
float ua = numerator / denom;
float ub = numerator2/ denom;

return (ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f);
}


// Test to see if a line intersects a Rectangle
bool LineIntersectsRect( const Vector2f &v1, const Vector2f &v2, const Rect &r )
{
Vector2f lowerLeft( r.x, r.y+r.height );
Vector2f upperRight( r.x+r.width, r.y );
Vector2f upperLeft( r.x, r.y );
Vector2f lowerRight( r.x+r.width, r.y+r.height);
// check if it is inside
if (v1.x > lowerLeft.x && v1.x < upperRight.x && v1.y < lowerLeft.y && v1.y > upperRight.y &&
v2.x > lowerLeft.x && v2.x < upperRight.x && v2.y < lowerLeft.y && v2.y > upperRight.y )
{
return true;
}
// check each line for intersection
if (LineIntersectLine(v1,v2, upperLeft, lowerLeft ) ) return true;
if (LineIntersectLine(v1,v2, lowerLeft, lowerRight) ) return true;
if (LineIntersectLine(v1,v2, upperLeft, upperRight) ) return true;
if (LineIntersectLine(v1,v2, upperRight, lowerRight) ) return true;
return false;
}


awesome. I used a modified version of this. thx!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!