# Thread: Calculate points within a rotated rectangle

1. ## Calculate points within a rotated rectangle

I have an app that I am working on. A bitmap will have a rectangle drawn on it. I need to rotate the rectangle anywhere from 0 - 360 degrees along its center point. After the rectangle is rotated I need to calculate if any given point int the picture falls inside the rectangle. All distances in centimeters. Looking for a good way to calculate using the following inputs:

bitmap width - width of the whole image
bitmap height - height of the whole image
rectangle width - width of the rectangle drawn
rectangle height - height of the rectangle drawn
angle of rotation - rotated clockwise 360 degrees

The point that is given can be expressed as an x,y position from the top left corner of the bitmap or a distance from the center of the rectangle

2. ## Re: Calculate points within a rotated rectangle

Here is a sketch of what I'd do in your case. The first thing I did was solve for the base case, where there is no rotation. I then convinced myself that being within the boundary of a region would be retained with a rotation.
So if I have a clockwise rotated rectangle of $\displaystyle \theta$, I counterclockwise rotate it back that much in my calculations. It's not elegant to show all the work, but I hope this leads you on the right track.
|*| denotes absolute value.

Given O is the origin (0,0)
No rotations : verify P(x,y) |x| < 1/2 w and |y| < 1/2 h. If not, it's not within the boundary.

With a rotation clockwise of an angle $\displaystyle \theta$, use a rotation matrix to rotate counterclockwise, then solve,
https://en.wikipedia.org/wiki/Rotati...Two_dimensions

ie.
Verify that P'(x',y'), the transformed point corresponding to P(x,y) follows the below rules. If it does, then P in within the boundary of your rotated rectangle.

$\displaystyle |x'| = |x\cos(\theta) - y\sin(\theta)| < 1/2 w$
$\displaystyle |y'| = |x\sin(\theta) + y\cos(\theta)| < 1/2 h$