point in rectangle

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

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 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 on other sites
Quote:
 Original post by popsoftheyearDon'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).

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×