How to rotate a rectangle inside a rectangle with different scales

Reverse engineering

This is the original, with $(p,q)=(132,51)$ and $(a,b)=(159,54)$ within the rectangle $(w,h)=(530,686)$.

First rotate counterclockwise over 90 degrees:

The new rectangle is $(W,H)=(787,608)$. So the scaling in x-direction is $\,W/h=787/686\,$ and the scaling in y-direction is $\,H/w=608/530\,$, but upon calculation they turn out to be the same, within the required accuracy (= one pixel).

Perform the scaling and round to integer values: $$ \begin{cases} P = q \times W/h = 59 & ; & Q = (w-a-p) \times H/w = 274 \\ A = b \times W/h = 62 & ; & B = a \times H/w = 182 \end{cases} $$