Dave's Brain

Browse - Programming Tips - How can I check if a point is inside a rectangle?

Date: 2010mar1
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); } ExampleUse() { RECT rect; POINT point; // ... if (IsInside(rect, point)) { printf("Inside\n"); } else { printf("Outside\n"); } }
In MFC you can use CRect::PtInRect()

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.