Given two numbers a and b where b < a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not.
Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Given point lies inside the rectangle
Approach: An efficient way is to form 4 line’s equation of a rectangle. Then, if a given point lies in or on the rectangle if and only if, substituting the given point on:
- The right sideline ( x-y-b = 0 ) must give the value less than or equals to zero.
- The left sideline ( x-y+a = 0 ) must give the value greater than or equals to one.
- The upper sideline ( x+y-2*a+b = 0 ) must give the value less than or equals to zero.
- The lower sideline ( x+y-b = 0 ) must give the value greater than or equals to one.
Given point lies inside the rectangle
- Check whether a given point lies inside a rectangle or not
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Check whether a point lies inside a sphere or not
- Find a point that lies inside exactly K given squares
- Find if a point lies inside a Circle
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Check whether the point (x, y) lies on a given line
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- Check if a circle lies inside another circle or not
- Coordinates of rectangle with given points lie inside
- Sum of Area of all possible square inside a rectangle
- Count the number of rhombi possible inside a rectangle of given size
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.