Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check a point if it lies inside a rectangle

I have a rectangle Rect (x,y,w,h) and a point P (px, py).
x and y are the top left coordinate of the rectangle.
w is it’s width.
h is it’s height.
px and py are the coordinate of the point P.
Does anyone knows the algorithm to check if P lies inside Rect.
Surprisingly, I couldn’t find any correct answer for my question on the internet.
Much appreciate!

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

use this piece of code:

#include <iostream>

int main()
{
  int x, y, w, h;
  std::cin >> x >> y >> w >> h;
      
  int px, py;
  std::cin >> px >> py;
  
  if(px >= x && px <= (x + w) && py <= y && py >= (y - h))
    std::cout << "point is in the rectangle";
  else
    std::cout << "point is out of the rectangle";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading