Sign in to follow this  
_Kami_

Object inside Rectangle?

Recommended Posts

_Kami_    190
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
grhodes_at_work    1385
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this