# 2d line box intersection

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

## 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 on other sites
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 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 Rectanglebool 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 on other sites
Quote:
 Original post by neweraI 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 Rectanglebool 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!

• ### What is your GameDev Story?

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

• 31
• 16
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
634117
• Total Posts
3015601
×