#### Archived

This topic is now archived and is closed to further replies.

# Line in a rect

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

## Recommended Posts

Which is the fastest way to do a line in a 2d rect check?

##### Share on other sites
Is the line infinite ( a ray ) or is this a line from point a to point b?

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!

##### Share on other sites
Its a line from point1(x1,y1) to point2(x2,y2).

Anyone?

##### Share on other sites
in 2d?

bool PointInRect(POINT point, RECT Rect)
{
if(point.x < Rect.Rightt&&point.x > Rect.Left)
{
if(point.y < Rect.Top&&point.y > Rect.Bottom)
return true;
}

return false;
}

[edited by - RhoneRanger on March 3, 2003 5:49:55 PM]

[edited by - RhoneRanger on March 3, 2003 5:50:49 PM]

##### Share on other sites
I don''t want a point in a rect function but a line in a rect function

##### Share on other sites
If either point of the line segment is inside the rectangle, the line is inside.

If that check fails, then if the line segment intersects any of the rectangle sides, the line is inside.

[edited by - Waverider on March 3, 2003 6:27:20 PM]

##### Share on other sites
JEESH

You can use the same function though.

a line is simply 2 connected points right???

bool LineInRect(POINT LineStart,POINT LineEnd,RECT Rect)
{
if(PointInRect(LineStart,Rect)
{
if(PointInRect(LineEnd,Rect)
return true;
}
return false;

}

[edited by - RhoneRanger on March 3, 2003 6:30:21 PM]

##### Share on other sites
It is possible for both points to be outside the rectangle but still have the line be inside, though. Your function works well for the point check. Another function will have to be written for the intersection with the rectangle sides.

Are we talking about total containment or just crossing the edges?

[edited by - Waverider on March 3, 2003 6:36:29 PM]

##### Share on other sites
He said 2d, not 3d.

##### Share on other sites

\ \---------|  \    ||   \   |---------      \       \

Both endpoints are outside the rectangle.
Is the line considered inside or outside?

##### Share on other sites
hmm, good point there, but I was under the impression the poster meant totally contained.

##### Share on other sites
this is probably way off, just did two testcases in my head but wouldnt this work:

assuming origin is top left:

if(P1.x <= Rect.rightEdgeXValue && P2.x >= Rect.leftEdgeXValue && P1.y <= Rect.TopEdgeValueY && P2.y >= Rect.TopEdgeValueY) {
return true;
}
else {
return false;
}

THe above assumes that P1.x is less than P2, so you could fiddle with that and make P2 become P1 to solve the other case, and also in that case you would take leftEdgeValueX and make it rightEdgeValueX and TopEdgeValueY = bottomEdgeValueY . . . i think

back to work

[edited by - mstein on March 3, 2003 7:20:52 PM]

[edited by - mstein on March 3, 2003 7:35:52 PM]

##### Share on other sites
Yes, but my way is a little faster, because you are testing 2 edges at a time, instead of all 4.

##### Share on other sites
no Rhone, i was going for the problem of a line intersecting a rectangle AND coming out the other end (I realize now i have not accounted for every case), the more i think about it the more i think my way works. Mine will not i think check to see if the line is completely contained in the rectangle. Let me know if i am wrong.

i think my one if statement handles the case drawn in ascii above, but will not handle the case where the line goes right through the two left and right edges.

Note i edited my original post . . . .

[edited by - mstein on March 3, 2003 7:34:40 PM]

[edited by - mstein on March 3, 2003 7:36:27 PM]

##### Share on other sites
Look up "liang / barsky parametric line clipping" in google, there's a lot of pdf's on it, The math is actually very easy and it's very efficent.

[edited by - Ironside on March 3, 2003 8:04:37 PM]

##### Share on other sites
Okay

bool LineCrossesRect(LINE line,RECT rect)
{
if(line.start.x > rect.right)
{
if(line.end.x > rect.right)
return false;

if(line.end.y > rect.top && line.end.y < rect.bottom)
return true;

return false;
}

else if(line.start.x < rect.left)
{
if(line.end.x < rect.left)
return false;

if(line.end.y > rect.top && line.end.y < rect.bottom)
return true;

return true;
}

else if(line.start.y > rect.top)
{
if(line.end.y > rect.top)
return false;

if(line.start.x > rect.left && line.start.x < rect.right)
return true;

return false;
}

else if(line.start.y < rect.bottom)
{
if(line.end.y < rect.bottom)
return false;

if(line.start.x > rect.left && line.start.x < rect.right)
return true;

return false;
}
}

##### Share on other sites
yes, i believe that fills in my other missing cases

##### Share on other sites
You know I gotta do it:

What is true:
line.start.x > rect.left, line.start.x < rect.right
line.start.y > rect.top, line.start.y > rect.bottom
line.end.x > rect.left, line.end.x > rect.right
line.end.y < rect.top, line.end.y > rect.bottom

Would this return false in your LineCrossesRect function, RhoneRanger?

I don't think the LineCrossesRect can depend only on comparisons to detect a crossing. Is some and many cases, yes, but not all. I think the function will also need a calculation of actual intersection, and seeing if the intersection point actually falls within the limits of both segments.

I might be missing something, but I think the above example would return true in your function, when the answer should be false. It enters the third block.

    +     +      +       +------  +|    |   +|    |------

[edited by - Waverider on March 3, 2003 8:27:13 PM]

##### Share on other sites
quote:
Original post by Waverider
You know I gotta do it:

What is true:
line.start.x > rect.left, line.start.x < rect.right
line.start.y > rect.top, line.start.y > rect.bottom
line.end.x > rect.left, line.end.x > rect.right
line.end.y < rect.top, line.end.y > rect.bottom

Would this return false in your LineCrossesRect function, RhoneRanger?

    +     +      +       +------  +|    |   +|    |------

else if(line.start.y > rect.top) //it is
{
if(line.end.y > rect.top) //it isnt
return false;

if(line.start.x > rect.left && line.start.x < rect.right)
return true; //it isnt

return false; //it is false
}

EDIT: Thanks for making me think though! I love it when people help each other !

[edited by - RhoneRanger on March 3, 2003 8:38:31 PM]

##### Share on other sites
You should AT LEAST look at parametric line clipping 8-10 lines of code, it''s really not that tough....

##### Share on other sites

      *       *         *------      |*   |       |  * |    -----*          *            *

##### Share on other sites
if you want to see parametric line clipping in action..

check out my latest little expirement

http://www.rainfallstudios.com/kablesart/katsusjourney.zip

• ### Forum Statistics

• Total Topics
628734
• Total Posts
2984444

• 25
• 11
• 10
• 16
• 14