0
$\begingroup$

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

  • 0
    Just for the record, I have found two interesting links on that topic: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html [http://www.visibone.com/inpoly/](http://www.visibone.com/inpoly/) Cheers!2011-07-30

1 Answers 1

6

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

  • 0
    @Matthias, it's a point-location algorithm that works for any convex polygon. See also http://mathforum.org/library/drmath/view/54386.html2011-07-28