Date: 2010mar1
Updated: 2017nov7
Platform: win32
Language: C/C++
Q. How can I check if a point is inside a rectangle?
A. Here's a function that does that:
// Make sure both are in the same co-ordinates
BOOL IsInside(const RECT &rect, const POINT point)
{
return !(point.x < rect.left || point.x > rect.right || point.y < rect.top || point.y > rect.bottom);
}
BOOL IsInsideAlternate(const RECT &rect, const POINT point)
{
return point.x >= rect.left && point.x <= rect.right && point.y >= rect.top && point.y <= rect.bottom);
}
ExampleUse()
{
RECT rect;
POINT point;
// ...
if (IsInside(rect, point))
{
printf("Inside\n");
}
else
{
printf("Outside\n");
}
}
In MFC you can use CRect::PtInRect()