# Thread: How to check if a point is in a square?

1. ## How to check if a point is in a square?

Hello!

If I have a square given by the coordinates of its peaks, for instance (x1,y1), (x2,y2), (x3,y3) and (x4,y4) in this order , how do I check whether a point P(x,y) is within this square? The square can be either aligned to the xOy system or not (it might be rotated, etc).
I am not interested in any geometrical demonstration, I only want to know if there's any relation between the x-s and the y-s, because I want to implement this relation in a Java program...

Thank you and I am waiting for your opinions!!

2. Here's how I would go about solving the problem: P is within the square (call is S) if and only if P' is within a new square S', where S' has sides aligned with the coordinate system. This new problem is easy to solve, because all you have to check is that x' and y' are both within the limits. So, here's the method:

1. Find a rotation matrix that takes S to an aligned square S'.
2. Using the same translation matrix, take P to P'.
3. Check if the new P' is in the new square S'.

Make sense? There might be other ways of doing it, but this is the method that comes to mind as the easiest.