How to calculate coordinates of rectangle using its rotation angle and extreme corners

491 Views Asked by At

I know the coordinates of 2 points A(xA,yA) and C(xC,yC) and a angle alpha. How to calculate coordinates of points B and D so as ABCD is a rectangle rotated of alpha

Sample with homemade ascii art :-)

      A -   -   -   -   -   -   -   -
     |   -------           ) alpha
    |           ------
   |                   -------- B
  D                            |
   -------                    |
          ------             |
                ----------- 
                            C
1

There are 1 best solutions below

4
On

The general algorithm is to rotate the rectangle to be axis-aligned with a rotation matrix, then calculate the area. I'll let you translate it into the language of your choice. I've added comments with the matrix/ vector versions of what's going on. Note that the signs of sin(theta) terms may all be backwards depending on your sign convention.

// d = A - C // all vectors
dx = xC - xA;
dy = yC - yA;

// let R(theta) be the counter clockwise rotation matrix by theta
// axisAligned = R(theta) * [dx; dy]
axisAlignedX = cos(theta) * dx + sin(theta) * dy;
axisAlignedY = -sin(theta) * dx + cos(theta) * dy;

// B = R(theta) * [axisAlignedX; 0] + A
xB = cos(theta) * axisAlignedX + xA;
yB = -sin(theta) * axisAlignedX + yA;

// D = R(theta) * [0; axisAlignedY] + A
xD = sin(theta) * axisAlignedY + xA;
yD = cos(theta) * axisAlignedY + yA;

area = abs(axisAlignedX * axisAlignedY);