Last active
December 19, 2015 18:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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