Skip to content

Instantly share code, notes, and snippets.

@stangirala
Last active December 19, 2015 18:58
Show Gist options
  • Save stangirala/6002117 to your computer and use it in GitHub Desktop.
Save stangirala/6002117 to your computer and use it in GitHub Desktop.
C++ Code that checks if a point lies within a rectangle. The rectangle can be aligned on any axis.
// Check if point is inside the space of the rectangle.
// Note the class defs and the operator overloading.
bool inRect (rectangle rectangle, point point) {
class point length, breadth, point_centre;
bool result;
sort_points(rectangle);
if (point.x <= 0 && point.y <= 0)
point_centre = ((rectangle.points[2] - rectangle.points[0])) - point*2;
else
point_centre = point*2 - ((rectangle.points[2] - rectangle.points[0]));
length = rectangle.points[1] - rectangle.points[0];
breadth = rectangle.points[2] - rectangle.points[1];
if (
((point_centre - length) * length < 0) && ((point_centre + length) * length >= 0) &&
((point_centre - breadth) * breadth < 0) && ((point_centre + breadth) * breadth >= 0)
) result = true;
else result = false;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment