Sign in to follow this  

point in rectangle

This topic is 3862 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

hello, i have a 2D-coordinate system and i simply want to check if a point lies inside a rectangle. the rectangle may have a rotation. sounds like a triviality but i'm somehow stuck. this is the function from the rectangle class. i try to rotate the testing point (vx,vy) into the local coordinate system of the rectangle.
public boolean isInside(float vx, float vy) {
	
	float rx = (float) (Math.cos(rotate) * x - Math.sin(rotate) * y);
	float ry = (float) (Math.sin(rotate) * x + Math.cos(rotate) * y); 
		
	return (vx >= rx && vx <= rx + width && vy >= ry && vy <= ry + height);
}


Share this post


Link to post
Share on other sites
You have the right idea...but instead of rotating the testing point into the current rectangle rotation...you should reverse the rotation of the rect AND do the same to the point to do a simple Left < X < Right and Top < Y < Bottom Test.

Don't forget to translate the point first (subtract the center of the rectangle from the point or whatever you do to rotate the rect in the first place).

There are also some other completely different methods to check for point-is-inside, but this method is probably the simplest. I would probably keep a copy of the original *unrotated* rectangle coords and do a reverse of the current rotation to the point you are testing, and test it against those original coords.

Share this post


Link to post
Share on other sites
Quote:
Original post by popsoftheyear
Don't forget to translate the point first (subtract the center of the rectangle from the point or whatever you do to rotate the rect in the first place).

This isn't actually necessary. Rotations preserve global angles and lengths, so it will work regardless of the rotational origin (provided it's the same for each object).

Admiral

Share this post


Link to post
Share on other sites

This topic is 3862 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.

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