Line in a rect

Started by
21 comments, last by lone_ranger 21 years, 1 month ago
Which is the fastest way to do a line in a 2d rect check?
Virtus junxit, mors non separabit
Advertisement
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!
Click here to learn more
Its a line from point1(x1,y1) to point2(x2,y2).
Virtus junxit, mors non separabit
Anyone?
Virtus junxit, mors non separabit
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]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
I don''t want a point in a rect function but a line in a rect function
Virtus junxit, mors non separabit
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]
It's not what you're taught, it's what you learn.
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]
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
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]
It's not what you're taught, it's what you learn.
He said 2d, not 3d.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno

This topic is closed to new replies.

Advertisement