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);
}
point in rectangle
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.
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.
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.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement