Mapping points inside one 2D rectangle into another smaller one

In summary: Then the map that you are looking for is given by\left(\begin{array}{c} x\\ y\\ \end{array}\right) =\left(\begin{array}{cc} \cos(\theta) & -\sin(\theta)\\ \sin
  • #1
xovangam
18
0
in some work I'm doing i have a 2D rectangle that can be rotated and/or translated in any direction in 2D space. for example it might look like this:

Code:
(x=30,y=-10) +-----------+ (x=30,y=2)              +-- +y
                   |                |                              |
                   |    *P         |                              -x
(x=20,y=-10) +-----------+ (x=20,y=2)

i would like for that to map to another smaller 2D rectangle that has its origin fixed at the top left corner. such that if i pick some point (P) that's on the larger rectangle i want to map it to the coordinate space of the smaller rectangle:

Code:
(x=0,y=0) +-------+ (x=5,y=0)                          +-- +x
              |           |                                        |
              |  *P      |                                        +y
(x=0,y=3) +-------+ (x=5,y=3)

not quite sure how to go about setting that up...any thoughts?

TIA
 
Physics news on Phys.org
  • #2
Hi xovangam! :smile:

There are quite few ways in which to achieve this. The most simp way is to translate the rectangle first to the top left corner and then shrinking it accordingly.

Some notation:
Let [itex](x_0,y_0)[/itex] be the top left corner of the original rectangle, let [itex](x_1,y_1)[/itex] be the top right corner and let [itex](x_2,y_2)[/itex] be the bottom left corner.
Let [itex](x_1^\prime,0)[/itex] be the top right corner of the new rectangle and let [itex](0,y_2^\prime)[/itex] be the bottom left corner. In your example, we have

[itex](x_0,y_0)=(30,-10)[/itex]
[itex](x_1,y_1)=(20,-10)[/itex]
[itex](x_2,y_2)=(30,2)[/itex]
[itex](x_1^\prime,0)=(5,0)[/itex]
[itex](0,y_2^\prime)=(0,3)[/itex]

The function you are looking for is

[tex]\left(\begin{array}{c} x\\ y\\ \end{array}\right) =
\left(\begin{array}{cc} \frac{x_1^\prime}{x_1-x_0} & 0\\ 0 & \frac{y_2^\prime}{y_2-y_0}\\ \end{array}\right)
\left(\begin{array}{c} x-x_0\\ y-y_0\\ \end{array}\right)
[/tex]

And indeed, we have here that (30,-10) is mapped to (0,0), that (20,-10) is mapped to (5,0) and that (30,2) is mapped to (0,3).
 
  • #3
thanks for the response micromass!

however, if (x1,y1) is the top right corner of the larger rectangle then it would be (30,2) and not (20,-10) as you state using my example. and that actually is what demonstrates the issue i have in that in this example the larger rectangle has been rotated such that it is oriented +y from the left to the right side on the top and bottom, and the smaller rectangle differs in that it has +x in that same direction. but i still want to project points inside the larger rectangle at that orientation into the smaller rectangle. so it's almost like i need a change of basis or something, but I'm not quite sure how to put that into the transformation matrix.
 
  • #4
xovangam said:
thanks for the response micromass!

however, if (x1,y1) is the top right corner of the larger rectangle then it would be (30,2) and not (20,-10) as you state using my example. and that actually is what demonstrates the issue i have in that in this example the larger rectangle has been rotated such that it is oriented +y from the left to the right side on the top and bottom, and the smaller rectangle differs in that it has +x in that same direction. but i still want to project points inside the larger rectangle at that orientation into the smaller rectangle. so it's almost like i need a change of basis or something, but I'm not quite sure how to put that into the transformation matrix.

Ah, you do want the rotation?? I just thought you messed up the drawing for some reason :biggrin: I'm sorry, I should have asked.

Anyway, you have

(x0,y0)=(30,−10)
(x1,y1)=(30,2)
(x2,y2)=(20,-10)
(x′1,0)=(5,0)
(0,y′2)=(0,3)

Anyway, then the matrix should be

[tex]\left(\begin{array}{c} x\\ y\\ \end{array}\right) =
\left(\begin{array}{cc} 0 & \frac{y^\prime}{y_1-y_0}\\ \frac{x^\prime}{x_2-x_0} & 0\\ \end{array}\right)
\left(\begin{array}{c} x-x_0\\ y-y_0\\ \end{array}\right)
[/tex]
 
  • #5
what if i don't know what the orientation of the larger rectangle is, i.e. it could be any arbitrary rotation. in the example i gave (perhaps a poor choice) it is rotated exactly 90 degrees, but it may also be rotated say, only 10 degrees instead. the orientation of the rectangle is an unknown factor in this case (though i guess i could compute that relative to the standard R2 basis vectors).

anyway so if the larger rectangle is arbitrarily oriented about the X and Y axes in 2D space that would require a different transform correct? again the smaller rectangle that i want to map to is always oriented in the same way with (0,0) in the top left corner, +X to the right and +Y down.

i've attached 2 GIFs to try and better visualize what i would like to do.
 

Attachments

  • large.gif
    large.gif
    3.1 KB · Views: 590
  • small.gif
    small.gif
    1.9 KB · Views: 559
Last edited:
  • #6
If you want a generic rotation about an arbitrary point just compose translation and rotation matrices to achieve your transform. If you need to scale, then again compose them with the other transformations.

Just make sure you do the composition of maps in the right order.
 
  • #7
OK, let's do this in complete generality.

Let's say that your first rectangle is described by (x0,y0),(x1,y1),(x2,y2). And your second rectangle is described by (x0',y0'),(x1',y1'),(x2',y2') And you want to send (xi,yi) to (xi',yi'). Then your transformation should be

[tex]\left(\begin{array}{c} x\\ y\\ \end{array}\right) =
\left(\begin{array}{cc}
\frac{(y_1-y_0)(x_2^\prime-x_0^\prime)-(y_2-y_0)(x_1^\prime-x_0^\prime)}{(x_2-x_0)(y_1-y_0)-(x_1-x_0)(y_2-y_0)} & \frac{(x_1-x_0)(x_2^\prime-x_0^\prime)-(x_2-x_0)(x_1^\prime-x_0^\prime)}{(x_1-x_0)(y_2-y_0)-(x_2-x_0)(y_1-y_0)}\\
\frac{(y_1-y_0)(y_2^\prime-y_0^\prime)-(y_2-y_0)(y_1^\prime-y_0^\prime)}{(x_2-x_0)(y_1-y_0)-(x_1-x_0)(y_2-y_0)} & \frac{(x_1-x_0)(y_2^\prime-y_0^\prime)-(x_2-x_0)(y_1^\prime-y_0^\prime)}{(x_1-x_0)(y_2-y_0)-(x_2-x_0)(y_1-y_0)}\\
\end{array}\right)
\left(\begin{array}{c} x-x_0\\ y-y_0\\ \end{array}\right)
+
\left(\begin{array}{c} x_0^\prime\\ y_0^\prime\\ \end{array}\right)
[/tex]
 
  • #8
isn't this a change of basis though? disregarding scale, i guess I'm not clear on how i could rotate the larger rectangle within the standard R2 space and end up with the coordinate system of the smaller rectangle (where positive Y is flipped to point down with positive X still pointing to the right, but with P still at the top-left of the rectangle). i.e. I'm not sure what that transform (composed or otherwise) would look like.
 
Last edited:
  • #9
that almost works :smile:

if Q in the larger rectangle is (-x,-y) however, it's coming out as (-x',-y'), which is outside the boundary of the smaller rectangle. though the values seem correct, they are just negated to the opposite (-x,-y) quadrant it looks like. maybe i set it up wrong, i will double-check..
 
  • #10
ok, so to summarize the results, the solution proposed by micromass worked (thanks!), but i had to negate the (xi',yi') result for any point (xi, yi) inside the larger rectangle that i transformed. i *think* this is because the coordinate system for the larger rectangle actually does have the y-axis flipped around (see result) such that positive Y points down instead of up (see attached).

does that make sense?
 

Attachments

  • large.gif
    large.gif
    3.1 KB · Views: 589
  • #11
xovangam said:
ok, so to summarize the results, the solution proposed by micromass worked (thanks!), but i had to negate the (xi',yi') result for any point (xi, yi) inside the larger rectangle that i transformed. i *think* this is because the coordinate system for the larger rectangle actually does have the y-axis flipped around (see result) such that positive Y points down instead of up (see attached).

does that make sense?

Yes, that makes sense. If you flip the y-axis, then you'll have to negate the result.
 
  • #12
very cool.

thank you sir you've been more than helpful!
 

FAQ: Mapping points inside one 2D rectangle into another smaller one

How do you map points inside one 2D rectangle into another smaller one?

To map points inside one 2D rectangle into another smaller one, you need to use a scaling factor. This factor is calculated by dividing the length of the smaller rectangle by the length of the larger rectangle. Then, multiply this factor by the x and y coordinates of each point in the original rectangle to get the corresponding coordinates in the smaller rectangle.

What is the purpose of mapping points inside one 2D rectangle into another smaller one?

The purpose of mapping points inside one 2D rectangle into another smaller one is to resize and reposition the points to fit within the smaller rectangle. This can be useful for various applications, such as image resizing and data visualization.

Can you provide an example of mapping points inside one 2D rectangle into another smaller one?

For example, if you have a rectangle with dimensions of 10x8 and you want to map it onto a smaller rectangle with dimensions of 5x4, the scaling factor would be 0.5 (5/10). If a point in the original rectangle has coordinates of (6,4), the corresponding coordinates in the smaller rectangle would be (6*0.5, 4*0.5) which is (3,2).

Is there a specific formula for mapping points inside one 2D rectangle into another smaller one?

Yes, the formula for mapping points inside one 2D rectangle into another smaller one is as follows: newX = originalX * (smallerWidth / originalWidth) and newY = originalY * (smallerHeight / originalHeight). This formula takes into account the scaling factor and calculates the corresponding coordinates in the smaller rectangle.

Are there any limitations or considerations when mapping points inside one 2D rectangle into another smaller one?

One limitation to consider is that the aspect ratio of the original and smaller rectangle must be the same in order for the mapping to be accurate. If the aspect ratio is significantly different, the mapped points may appear distorted. Additionally, the mapping may not work for irregular or non-rectangular shapes.

Similar threads

Replies
1
Views
708
Replies
5
Views
2K
Replies
14
Views
1K
Replies
22
Views
4K
Replies
1
Views
1K
Replies
1
Views
7K
Replies
4
Views
6K
Back
Top