• Advertisement
Sign in to follow this  

Object inside Rectangle?

This topic is 4617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is 2d. How can I check if an object is inside a rectangle? The rectangle can be rotated. Ex: Person A is looking at person B. I want to find all persons 2 meters to left/right and 3 meters behind B, forming a rectangle of 4x3 meters. What would the formula be to check if person C is within that rectangle?

Share this post


Link to post
Share on other sites
Advertisement
One generalized way is to do a point-in-polygon test. Look at the Forum FAQ for "point-in-polygon". There is a link to a nice page that describes approaches.

Another way is to project the test point (location of person C) into a local coordinate frame that is located at the center of the rectangle and has an x axis parallel to one edge (width edge) and the y axis parallel to another edge (length edge). Then, just do this:


C_proj = projection of C into the local rectangle frame
if (-width < C_proj.x && width > C_proj.x &&
-length < C_proj.y && length > C_proj.y)
{
C is inside
}
else
{
C is outside
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement