Rotating some rectangles to make them horizontal I am new to math.stackexchange, so pardon me if I made any mistake. My question is - I have some rectangles along some lines inside another rectangle. They are at an angle other than 0 and 180 along horizontal line and I want to rotate them so that they are along the horizontal line. I know that in order to rotate a rectangle I have to rotate the four corner points with respect to a reference point. How can I achive this and what should be my reference point for rotation. Please see the image below to better understand my question.


So far I have tried to rotate individual rectangle using the top left corner point of the rectangle I am working with as a reference, but this didn't work because then the rectangles rotate remaining in their position. So I get a set of rectangles like stairs, not in the same line. I now know that I have to rotate all of them using the same reference point. Which should be the point- the top left of the outer rectangle?
EDIT:
The image for my current approach 


 A: If I understand your problem correctly, the solution would be to find coordinates of all the corners of rectangle in a new other coordinate system where: 


*

*Origin of the coordinate system is at the bottom left corner of the bottom left rectangle.

*The angle between x-axis of new coordinate system with respect to horizontal is $\omega$

As shown in above image you just need to get $(a, b)$ in that new coordinate system.
Now if you assume $(a, b)$ (all the corners of rectangle in new system) in orthogonal Cartesian system ($x'$-axis and $y$-axis with the same origin $(0, 0)$) as shown in figure you get your desired result with width and height preserved.
So the set of coordinates in this system is your final coordinates you want.
Shifting Origin and finding coordinates is left to you as an exercise! 
A: Forget about rotating all of the rectangles about one reference point, since all of them will end up being rotated by different amounts.
What you can do instead is to find the angle $\theta$ of the bottom sides of the rectangles from the horizontal. You can do this by using the coordinates of the corners.
Then, translate the centroids of the rectangles to $(0,0)$. Now you can apply the rotation matrix and rotate by $- \theta$:
$$R = \begin{bmatrix} \cos(-\theta) & -\sin(-\theta) \\ \sin(-\theta) & \cos( -\theta) \end{bmatrix}
= \begin{bmatrix} \cos \theta & \sin \theta \\ -\sin \theta & \cos \theta  \end{bmatrix}$$
using the properties of $\cos$ and $\sin$. Don't forget to translate back to the position you want the rectangles to be in!
