I need some maths help for a 2D game I am programming. In this game I have a rectangle, specified by its centers' X and Y coordinates, and its width and height. I then rotate this rectangle via

halfWidth = width / 2;
halfHeight = height / 2;

rad = angle * TO_RADIANS;
cos = FloatMath.cos(rad);
sin = FloatMath.sin(rad);

x1 = -halfWidth * cos - (-halfHeight) * sin;
y1 = -halfWidth * sin + (-halfHeight) * cos;
x2 =  halfWidth * cos - (-halfHeight) * sin;
y2 =  halfWidth * sin + (-halfHeight) * cos;
x3 =  halfWidth * cos - halfHeight * sin;
y3 =  halfWidth * sin + halfHeight * cos;
x4 = -halfWidth * cos - halfHeight * sin;
y4 = -halfWidth * sin + halfHeight * cos;

x1 += x;
y1 += y;
x2 += x;
y2 += y;
x3 += x;
y3 += y;
x4 += x;
y4 += y;

That is working fine.

Additionally I have a point in the same Cartesian coordinate space, specified by pointX and pointY. Now I want check whether or not this point lies within the boudaries of the rotated rectangle -- but unfortunately I do not have a clue how to achieve this :)

Can you help me with this question?
Thanks in advance!

Kind regards, Matthias


1 Answer 1


Rotate the point back and test it against the unrotated rectangle.

  • 3
    $\begingroup$ If you can't bring the rectangle to the point, bring the point to the rectangle. :) $\endgroup$ Commented Jul 28, 2011 at 16:20
  • 1
    $\begingroup$ He needs to rotate the test point around the center of the rectangle by -angle, but this is a good plan. $\endgroup$ Commented Jul 28, 2011 at 16:24
  • $\begingroup$ Thanks, that sounds like a plan! But is this also the fastest (in terms of computation time) solution? It seems the rotating all four X/Y pairs of the rect plus the X/Y coords. of the point is a bit much work for the computer (within a loop of a game) -- or isn't there any faster (mathematical) solution? $\endgroup$
    – Matthias
    Commented Jul 28, 2011 at 20:03
  • $\begingroup$ @Matthias, there is another solution based on half-spaces: the point must be to the left of all edges, when the rotated rectangle is oriented counter-clockwise. Each test reduces to computing a 2x2 determinant. $\endgroup$
    – lhf
    Commented Jul 28, 2011 at 20:47
  • $\begingroup$ @lhf, sorry but I do not really understand your second approach. Could you please be so kind and explain that a bit? Thanks a lot in advance for all your help! $\endgroup$
    – Matthias
    Commented Jul 28, 2011 at 21:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .