How to determine coordinates on two different size rectangles

581 Views Asked by At

Hello community I hope all is well. I was wondering if someone could shine some light on the following problem.

Let's say we have 2 rectangles (A and B) which are different in size. Let's say I have 4 points in rectangle A and now I want to scale those points into Rectangle B. What is the correct approach to this problem?

Thank you kindly.

1

There are 1 best solutions below

0
On BEST ANSWER

Let $A$ and $B$ be two rectangles of the plane located at $(X_A, Y_A)$ and $(X_B, Y_B)$ respectively. Let $w_A$, $h_A$, $w_B$, $h_B$ be the widths and heights of these rectangles.

rectangles

To transform a point $(x, y)$ from $A$ to $B$ you first have to express the point coordinates in rectangle $A$ space.

$$x_A = x - X_A$$ $$y_A = Y_A - y$$

Then, compute the ratio of the point position (in rectangle $A$ space) by the rectangle side.

$$r_x = \frac{x_A}{w_A}$$ $$r_y = \frac{y_A}{h_A}$$

Then, scale it by the side of the destination rectangle $B$ to get the position relative to $(x_B, y_B)$ :

$$x'_B = w_B \times r_x$$ $$y'_B = h_B \times r_y$$

Finally, translate it to the position of the destination rectangle $(x_B, y_B)$ :

$$x' = X_B + x'_B$$ $$y' = Y_B - y'_B$$

The final coordinates of the transformed point are :

$$x' = X_B + w_B\frac{x - X_A}{w_A}$$ $$y' = Y_B - h_B\frac{Y_A - y}{h_A}$$

Hope it helped...