<p>Just use linear algebra.</p>

<p>Since parallel lines have remained parallel (i.e. meaning we can probably assume the <em>w</em> component of homogeneous coordinates is always 1), we can just pick 3 starting points and their destinations, e.g.</p>

<pre><code>(0,0,1)-&gt;(3,2,1),  (0,1,1)-&gt;(1,3,1), and  (1,0,1)-&gt;(5,3,1),
</code></pre>

<p>define M to be your (homogeneous) transformation matrix as </p>

<p><span class="math-container">$$ M =\begin{bmatrix}
a &amp; b &amp; c \\
d &amp; e &amp; f \\
0 &amp; 0 &amp; 1\\
\end{bmatrix}  $$</span>
and then we have 
<span class="math-container">$$ M.\begin{bmatrix}
0 &amp; 0 &amp; 1 \\
0 &amp; 1 &amp; 0 \\
1 &amp; 1 &amp; 1\\
\end{bmatrix} = 
\begin{bmatrix}
3 &amp; 1 &amp; 5 \\
2 &amp; 3 &amp; 3 \\
1 &amp; 1 &amp; 1\\
\end{bmatrix}
 $$</span></p>

<p>You just need to find the inverse of the 'starting point' matrix (<em>which is left as an exercise for the reader</em> ) and rearrange to compute...
<span class="math-container">$$ M= 
\begin{bmatrix}
3 &amp; 1 &amp; 5 \\
2 &amp; 3 &amp; 3 \\
1 &amp; 1 &amp; 1\\
\end{bmatrix}
.\begin{bmatrix}
0 &amp; 0 &amp; 1 \\
0 &amp; 1 &amp; 0 \\
1 &amp; 1 &amp; 1\\
\end{bmatrix}^{-1}$$</span></p>

<p>To be really thorough, you could also then check that all the remaining points are transformed correctly by your result.</p>

<p>If you don't want to work through the maths yourself, the resulting matrix is</p>

<blockquote class="spoiler">
  <p> <span class="math-container">$ M= \begin{bmatrix} 2 &amp; -2 &amp; 3 \\ 1 &amp; 1 &amp; 2 \\ 0 &amp; 0 &amp; 1\\ \end{bmatrix}$</span></p>
</blockquote>

CC BY-SA 4.0
2019-02-12T08:59:03.130Z
2019-02-12T15:08:34.640Z
2019-02-12T15:08:34.640Z
Simon F
<p>Since the only answer requires homogeneous coordinates, a <span class="math-container">$3\times3$</span> matrix inversion, as well as a <span class="math-container">$3\times3$</span> matrix multiplication, here's something simpler, not as tedious to compute, and also more geometrically intuitive (it also gives a better idea what a linear map can do, and what it cannot).</p>

<p>A linear <span class="math-container">$2\times2$</span> transformation obviously cannot translate the origin, for this we will need a translation. We can find the translation from the difference between the lower left vertices of the house (since both should lie at the origin of their coordinate systems). We see that <span class="math-container">$(0,0)\rightarrow (3,2)$</span>, then the translation is <span class="math-container">$(3,2)$</span>. We can move the stretched and rotated house back to the origin: so that the lower left vertex coincides with <span class="math-container">$(0,0)$</span> (the initial position of it) by translating with <span class="math-container">$-(3,2)$</span>. We'll pick two 'nice' points (in the sense that they have <span class="math-container">$0$</span> for one of their components and <span class="math-container">$1$</span> for the other): <span class="math-container">$(1,0)$</span> and <span class="math-container">$(0,1)$</span>. We can see from the image that <span class="math-container">$(1,0) \rightarrow (5,3) - (3,2) = (2,1)$</span>  where the subtraction is necessary to move the transformed house back to the origin as already mentioned. Similarly: <span class="math-container">$(0,1) \rightarrow (1,3) - (3,2) = (-2,1)$</span>. We want to find a matrix:
<span class="math-container">$$ M =\begin{bmatrix}
    a &amp; b\\
    c &amp; d\\
    \end{bmatrix}  $$</span>
such that <span class="math-container">$M(1,0)^T = (2,1)^T$</span> and <span class="math-container">$M(0,1)^T = (-2,1)^T$</span>. But this is trivial, since we get <span class="math-container">$(a,c) = (2,1)$</span> and <span class="math-container">$(b,d) = (-2,1)$</span>. Thus you have: <span class="math-container">$v'=Mv+(3,2)$</span>, where <span class="math-container">$v$</span> is a point from the original house, and <span class="math-container">$v'$</span> is the transformed such. Note that it is easy to see that this is simply scaling + rotation (45 degrees) + scaling:
<span class="math-container">$$ M =\begin{bmatrix} 2 &amp; -2\\1 &amp; 1\\ \end{bmatrix}   
= \begin{bmatrix} 2 &amp; 0\\0 &amp; 1\\ \end{bmatrix} \begin{bmatrix} 1 &amp; -1\\1 &amp; 1\\ \end{bmatrix} \\=  \begin{bmatrix} 2 &amp; 0\\ 0 &amp; 1\\ \end{bmatrix}
\begin{bmatrix} \frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\ \end{bmatrix}
\begin{bmatrix} \sqrt{2} &amp; 0\\ 0 &amp; \sqrt{2}\\ \end{bmatrix}$$</span></p>

<p>The right scaling may be moved inside the left one (since the right is uniform scaling, hence commutative, or just a multiplication by the scalar <span class="math-container">$\sqrt{2}$</span>, I just thought it's geometrically more intuitive this way).</p>

CC BY-SA 4.0
2019-02-12T11:44:33.470Z
2019-02-12T12:43:30.687Z
2019-02-12T12:43:30.687Z
lightxbulb
<p>I try to solve this,</p>

<p><a href="https://i.sstatic.net/bWhcz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bWhcz.png" alt="enter image description here"></a></p>

<p>First, I used scaling: <span class="math-container">$Scale(S_x,S_y)=S(\sqrt 5,\sqrt 5)$</span></p>

<p>So, I enlarge the bottom of the house and the sides, to the same size of B image.</p>

<p>Later, I used rotation: <span class="math-container">$R(\theta)=R(cos^{-1}(2/\sqrt 5))$</span>
the vertex that was <span class="math-container">$[\sqrt 5, 0, 1]$</span> moved <span class="math-container">$[2, 1, 1]$</span> I do that because I thought to translate it to <span class="math-container">$[5,3,1]$</span></p>

<p>But I stuck with that.</p>

<p>What is the idea of this question?</p>

<p>Thanks.</p>

CC BY-SA 4.0
2019-02-11T05:53:23.887Z
Could there be [shear in the matrix](https://en.wikipedia.org/wiki/Shear_matrix)?
user1118321
CC BY-SA 4.0
2019-02-11T08:54:47.067Z
There definitely is a shear involved. Seems like a shear in the X-coordinate.
gallickgunner
CC BY-SA 4.0
2019-02-11T10:13:20.960Z
Yes, one of the matrices is Shear, but I don't understand when to use it.
Asaf
CC BY-SA 4.0
2019-02-11T11:25:18.843Z
Try Shearing > Scaling > Rotating > Translatng. As long as the scaling is uniform it doesnt matter if you do shearing or scaling first. Translation wouldn't affect shearing though, not quite sure about rotation. Why don't you try shearing before and after rotation. And post the results. Would be a nice find :)
gallickgunner
CC BY-SA 4.0
2019-02-11T12:35:09.053Z
You can find the complete transform matrix T in one step minimizing a linear least squares error.
Mauricio Cele Lopez Belon
CC BY-SA 4.0
2019-02-11T16:51:05.000Z
You don't need least squares. The mapping is obviously linear, so it can be computed from 2 different points.
lightxbulb
CC BY-SA 4.0
2019-02-12T09:09:24.613Z
@lightxbulb You'll need 3 pairs of points because there will be 6 unknowns in the transformation matrix (4 for rotation/scaling +2 for translation)
Simon F
CC BY-SA 4.0
2019-02-12T11:28:24.867Z
@SimonF You can see the translation from the image directly. If anything your solution is needlessly complex.
lightxbulb
<transformations>
Using transformations to get from A to B image
Asaf
# Using transformations to get from A to B image

(Asked by: username_0 on 2019-02-10)

I try to solve this,

[![enter image description here](https://i.sstatic.net/bWhcz.png)](https://i.sstatic.net/bWhcz.png)

First, I used scaling: $Scale(S\_x,S\_y)=S(\sqrt 5,\sqrt 5)$

So, I enlarge the bottom of the house and the sides, to the same size of B image.

Later, I used rotation: $R(\theta)=R(cos^{-1}(2/\sqrt 5))$
the vertex that was $[\sqrt 5, 0, 1]$ moved $[2, 1, 1]$ I do that because I thought to translate it to $[5,3,1]$

But I stuck with that.

What is the idea of this question?

Thanks.

**Tags:** transformations

> **Comments:**
> **username_1**: Could there be [shear in the matrix](https://en.wikipedia.org/wiki/Shear_matrix)?  
> **username_2**: There definitely is a shear involved. Seems like a shear in the X-coordinate.  
> **username_0**: Yes, one of the matrices is Shear, but I don't understand when to use it.  
> **username_2**: Try Shearing > Scaling > Rotating > Translatng. As long as the scaling is uniform it doesnt matter if you do shearing or scaling first. Translation wouldn't affect shearing though, not quite sure about rotation. Why don't you try shearing before and after rotation. And post the results. Would be a nice find :)  
> **username_3**: You can find the complete transform matrix T in one step minimizing a linear least squares error.  
> **username_4**: You don't need least squares. The mapping is obviously linear, so it can be computed from 2 different points.  
> **username_5**: @username_4 You'll need 3 pairs of points because there will be 6 unknowns in the transformation matrix (4 for rotation/scaling +2 for translation)  
> **username_4**: @username_5 You can see the translation from the image directly. If anything your solution is needlessly complex.  

---

## 2 answers

### Answer by username_5 on 2019-02-12. Score: 1

Just use linear algebra.

Since parallel lines have remained parallel (i.e. meaning we can probably assume the *w* component of homogeneous coordinates is always 1), we can just pick 3 starting points and their destinations, e.g.

```
(0,0,1)->(3,2,1),  (0,1,1)->(1,3,1), and  (1,0,1)->(5,3,1),

```

define M to be your (homogeneous) transformation matrix as

$$ M =\begin{bmatrix}
a & b & c \\
d & e & f \\
0 & 0 & 1\\
\end{bmatrix} $$
and then we have
$$ M.\begin{bmatrix}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 1 & 1\\
\end{bmatrix} =
\begin{bmatrix}
3 & 1 & 5 \\
2 & 3 & 3 \\
1 & 1 & 1\\
\end{bmatrix}
$$

You just need to find the inverse of the 'starting point' matrix (*which is left as an exercise for the reader* ) and rearrange to compute...
$$ M=
\begin{bmatrix}
3 & 1 & 5 \\
2 & 3 & 3 \\
1 & 1 & 1\\
\end{bmatrix}
.\begin{bmatrix}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 1 & 1\\
\end{bmatrix}^{-1}$$

To be really thorough, you could also then check that all the remaining points are transformed correctly by your result.

If you don't want to work through the maths yourself, the resulting matrix is

> $ M= \begin{bmatrix} 2 & -2 & 3 \\ 1 & 1 & 2 \\ 0 & 0 & 1\\ \end{bmatrix}$


---

### Answer by username_4 on 2019-02-12. Score: 0

Since the only answer requires homogeneous coordinates, a $3\times3$ matrix inversion, as well as a $3\times3$ matrix multiplication, here's something simpler, not as tedious to compute, and also more geometrically intuitive (it also gives a better idea what a linear map can do, and what it cannot).

A linear $2\times2$ transformation obviously cannot translate the origin, for this we will need a translation. We can find the translation from the difference between the lower left vertices of the house (since both should lie at the origin of their coordinate systems). We see that $(0,0)\rightarrow (3,2)$, then the translation is $(3,2)$. We can move the stretched and rotated house back to the origin: so that the lower left vertex coincides with $(0,0)$ (the initial position of it) by translating with $-(3,2)$. We'll pick two 'nice' points (in the sense that they have $0$ for one of their components and $1$ for the other): $(1,0)$ and $(0,1)$. We can see from the image that $(1,0) \rightarrow (5,3) - (3,2) = (2,1)$ where the subtraction is necessary to move the transformed house back to the origin as already mentioned. Similarly: $(0,1) \rightarrow (1,3) - (3,2) = (-2,1)$. We want to find a matrix:
$$ M =\begin{bmatrix}
a & b\\
c & d\\
\end{bmatrix} $$
such that $M(1,0)^T = (2,1)^T$ and $M(0,1)^T = (-2,1)^T$. But this is trivial, since we get $(a,c) = (2,1)$ and $(b,d) = (-2,1)$. Thus you have: $v'=Mv+(3,2)$, where $v$ is a point from the original house, and $v'$ is the transformed such. Note that it is easy to see that this is simply scaling + rotation (45 degrees) + scaling:
$$ M =\begin{bmatrix} 2 & -2\\1 & 1\\ \end{bmatrix}
= \begin{bmatrix} 2 & 0\\0 & 1\\ \end{bmatrix} \begin{bmatrix} 1 & -1\\1 & 1\\ \end{bmatrix} \\= \begin{bmatrix} 2 & 0\\ 0 & 1\\ \end{bmatrix}
\begin{bmatrix} \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}}\\ \end{bmatrix}
\begin{bmatrix} \sqrt{2} & 0\\ 0 & \sqrt{2}\\ \end{bmatrix}$$

The right scaling may be moved inside the left one (since the right is uniform scaling, hence commutative, or just a multiplication by the scalar $\sqrt{2}$, I just thought it's geometrically more intuitive this way).


---

