Jump to content
• Advertisement

#### Archived

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

# Line in a rect

This topic is 5616 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

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

#### Share this post

##### Share on other sites
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

#### Share this post

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

Anyone?

#### Share this post

##### 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 this post

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

#### Share this post

##### 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 this post

##### 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 this post

##### 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 this post

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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5
• Advertisement

• 10
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002067
×

## 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!