Skip to main content
gamedev.net
🔒 Locked

RectInRect()?

Started by SikCiv Mar 21, 2000 at 12:10 AM 3 replies 1.8k views
Original Post
SikCiv
SikCiv
PtInRect, which tests if a POINT is within a RECTs bounds works like a dream, but I need to test if a RECT lies over another RECT. Is there a RectInRect function in vc++? Ive tried to create my own using the rects corners, but ive come to a few problems, what if no rect corner is within the other rect, and vise-versa (like a plus sign), etc.. Im sure I can work it out, but im affraid it will be kinda slow. I would do it in ASM but I dont know ASM. Anyone know of where I can get some source code to do this, prefferably in ASM?
  Downloads:  ZeroOne Realm
m_grinder
m_grinder
I belive this shoud do the trick.

BOOL IntersectRect( LPCRECT lpRect1, LPCRECT lpRect2 );

m_grinder
DerekDay
DerekDay
The function WinAPI function IntersectRect, as the previous post said, will work well. However, you can write a pretty fast test yourself, but don't test the corner points, because that has problems, like you said. Instead, do something similar to this:

bool RectInRect( RECT * r1, RECT * r2 )
{
if( r1->left > r2->right // r1->top > r2->bottom //
r1->bottom < r2->top // r1->right < r2->left )
return false;

return true;
}

That should be correct. IntersectRect has some other overhead, because it figures out which type of intersection is taking place, so you might get better results from the previous code.
-Derek


Edited by - DerekDay on 3/21/00 3:00:44 AM

Edited by - DerekDay on 3/21/00 3:01:35 AM
Xai
Xai
Derek missed a few things...he missed a few things ... his check guarantees that there is NOT an intersection...but when it fails, it does NOT mean there IS an intersection.

I wrote this function about 2 months ago ... but i don''t have it handy ... i will try to post my final solution (which is optimised a little) when i find it. I expect to post it about 18 hours after this post (early March 22nd).
Xai
Xai
I appologize. Upon much more detailed examination, it appears DerekDay way correct. My function went through 4 or 5 revisions as I found new ways to arange the code, until finally, this solution was what was left. Sorry for the confusion.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.