How can a find if a point lies within a 2D rectangle given 4 points?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Transform the point to a coordinate frame aligned with the rectangle, then the problem becomes axis-aligned and trivial.
If the rectangle consists of the following 4 points:
Then get the “x-axis” and “y-axis” of the rectangle as:
Then construct a rotation matrix using x and y as columns:
If you’re using 3-d coordinates, we need a z axis:
Your transform matrix from world coordinates to your rectangle’s axis-aligned coordinates becomes:
Here we’ve chosen the lower-left corner c to be the local origin. “r^T” is r transposed. “0^T” is either a 2-d or 3-d row-vector filled with zeros. 1 is just a one. Note that this is just the inverse of the simpler rectangle-to-world transform, which is
We can use T to transform the point to axis-aligned coordinates. Remember to pad p with a trailing 1, since T is a homogeneous matrix.
If you’re using 3d coordinates, note that the above disregards the local z value of transformed point tp. Even if p is out of the plane of the rectangle, the above behaves as if it’s been projected to the rectangle surface. If you want to check for coplanarity, just do the following beforehand: