Results 1 to 4 of 4

Math Help - How to check if a point is in a square?

  1. #1
    Newbie
    Joined
    Nov 2007
    Posts
    22

    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!!
    Last edited by inzaghina; November 5th 2010 at 01:06 AM. Reason: more clear
    Follow Math Help Forum on Facebook and Google+

  2. #2
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    5
    Awards
    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.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Nov 2007
    Posts
    22
    Thank you for your answer!
    Meantime, I have discovered the contains() function, in the Polygon class, so I no longer need to find out by myself....interesting, I guess this is how the function is implemented.... thanks
    Follow Math Help Forum on Facebook and Google+

  4. #4
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    5
    Awards
    2
    Hey, using a built-in function, assuming it's doing what you think it is (always check! I'd recommend using it on some obvious examples to make sure its behavior is what you want), is a perfectly good engineering answer.

    You're welcome! Have a good one.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: July 2nd 2011, 08:03 PM
  2. from the point inside the square
    Posted in the Trigonometry Forum
    Replies: 1
    Last Post: December 3rd 2010, 05:04 AM
  3. Proof check Accumulation point
    Posted in the Differential Geometry Forum
    Replies: 3
    Last Post: December 23rd 2009, 07:14 AM
  4. Z value of point in square
    Posted in the Calculus Forum
    Replies: 14
    Last Post: August 23rd 2007, 11:23 PM
  5. Check my answer for least square solution of Ax=b
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: May 10th 2007, 12:36 AM

Search Tags


/mathhelpforum @mathhelpforum