Matrix representation of affine transformation that maps a given line segment to another given line segment Given two line segments $(x_1,y_1),(x_2,y_2)$ and $(x_3,y_3),(x_4,y_4)$, I am looking for a matrix representation of (one, as I understand there can be many?) affine transformation that takes the first line segment to the second. (Also, just to be clear, I want it to map $(x_1,y_1)$ to $(x_3,y_3)$, not the/any other mapping that flips the order.)
(I realize that I can work out the translation of the first segment back to origin, then rotation through the difference of angles, scaling by the ratio of the lengths, and then translating up to the second segment's location.  I'm just thinking that this might be a standard thing people want to be able to do (I need it for a computer program I am working on) and the general form of this matrix is already worked out, but I haven't found it due to not being 100% sure of what terms to google.  I also have a vague recollection that there is a clever shortcut that prevents having to go through all the steps I described, but my matrix-fu is quite weak, unfortunately.)
Update: I am going to see what I can do using the examples at https://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations showing the method of augmenting the vector with a 1 and the $n$x$n$ with a zero row and the translation vector as a column.  I will post whatever I come up with as an answer if nothing else shows up here--I just want to let people know I'm making an attempt, in case that changes their desire to work on it either way.
 A: We need to find a transformation that maps the segments. It is sufficient to restrict the search to affine maps and to map only the end points
$$
z_1=\left[\matrix{x_1\\y_1}\right]\ \mapsto w_1=\left[\matrix{x_3\\y_3}\right],\quad\text{and}\quad 
z_2=\left[\matrix{x_2\\y_2}\right]\ \mapsto \ w_2=\left[\matrix{x_4\\y_4}\right].
$$
Case I: $\text{rank}\,[z_1\ z_2]=2$. Then the linear transformation
$$
w=Az\qquad\text{where}\qquad A=[w_1\ w_2]\cdot [z_1\ z_2]^{-1}
$$
does the job.
Case II: $z_1\parallel z_2$, but $z_1\ne z_2$. Then we need to consider an affine map $w=Az+z_0$ to shift the $w$-vectors. Let $\lambda z_1+\mu z_2=0$. Then
$$
\left\{
\begin{array}{lcl}
\lambda Az_1+\lambda z_0&=&\lambda w_1,\\
\mu Az_2+\mu z_0&=&\mu w_2,\\
\end{array}
\right.\quad\Rightarrow\quad \underbrace{A(\lambda z_1+\mu z_2)}_{=0}+(\lambda+\mu)z_0=\lambda w_1+\mu w_2.
$$
It gives $z_0=\frac{\lambda w_1+\mu w_2}{\lambda+\mu}$. Note that $\lambda+\mu\ne 0$ by the assumption that $z_1\ne z_2$. With this $z_0$ any $A$ that maps only one end point, i.e. $Az_1=w_1-z_0$ would suffice (not unique).
Case III: $z_1=z_2$ is trivial. If $w_1=w_2$ (point-to-point) then any $A$ with $Az_1=w_1$ works. If $w_1\ne w_2$ (point-to-segment) then no such transformation (even non-linear) exists.
A: Let $T_1$ be the translation by $(x_1, y_1)$ and $T_3$ the translation by $(x_3, y_3)$. Then, for any linear transformation $S$, the affine transformation $$P := T_3 \circ S \circ T_1^{-1}$$ maps $(x_1, y_1)$ to $(x_3, y_3)$. Thus, to find an affine transformation $P$ that maps the first segment to the second, it's enough to choose $S$ so that $S(x_2', y_2') = (x_4', y_4')$, where $$(x_2', y_2') := T_1^{-1}(x_2, y_2) = (x_2 - x_1, y_2 - y_1)$$ and $$(x_4', y_4') := T_3^{-1}(x_4, y_4) = (x_4 - x_3, y_4 - y_3).$$
If we want $P$ to be a composition of translations, dilations, and rotations---so an (oriented) conformal affine transformation---we need only choose $S$ to be a composition of rotations and dilations, that is an (oriented) conformal transformation. It's not hard to show that any oriented conformal transformation of the plane has matrix transformation $$\pmatrix{a & -b \\ b & a}$$ for some $a, b$, and that this is the unique such transformation that maps $(1, 0)$ to $(a, b)$. (If we identify the plane $\Bbb R^2$ with the complex numbers $\Bbb C$ in the usual way, this map is nothing more than multiplication by $a + ib$.) So, by construction, the composition
$$S := \pmatrix{x_4' & -y_4' \\ y_4' & x_4'} \pmatrix{x_2' & -y_2' \\ y_2' & x_2'}^{-1}$$ is the unique oriented conformal transformation that maps $(x_2', y_2')$ to $(x_4', y_4')$. Note that the matrices of such transformations have particularly nice inverses:
$$\pmatrix{x_2' & -y_2' \\ y_2' & x_2'}^{-1} = \frac{1}{(x_2')^2 + (y_2')^2}\pmatrix{x_2' & y_2' \\ -y_2' & x_2'}.$$
One can of course use the above formulas to compute an explicit formula for the desired affine transformation $P$, but the result is at best no more enlightening the above ingredients taken together.
A: You're definitly right -- there are many affine transformations that map one of the line segments into another. Since segments remain segments under affine transformation the only concern is correct mapping of their endpoints, thus we actually want to retrieve affine transfromation from its action on 2 points. But to retrieve 2D affine transformation you need exactly 3 points and they should not lie on one line. For N-dimensional space there is a simple rule: to unambiguously recover affine transformation you should know images of N+1 points that form a simplex --- triangle for 2D, pyramid for 3D, etc. A good explanation of why it's the way it should be, you may find in the "Beginner's guide to mapping simplexes affinely".
Now consider the problem of finding any affine transformation that maps one segment into another. Since I need 3 points, let me assume (arbitrarily) that point $(0;0)$ remains the same, e.g. $(0;0) \mapsto (0;0)$. Now I can use equation from the guide I mentioned above
$$
\vec{f}(x; y) = (-1)
\frac{
    \det
        \begin{pmatrix}
            0   & x_3\vec{i} + y_3\vec{j} & x_4\vec{i} + y_4\vec{j} & \vec{0} \\
            x   & x_1       & x_2       & 0     \\
            y   & y_1       & y_2       & 0     \\
            1   & 1         & 1         & 1         \\
        \end{pmatrix}
}{
    \det
        \begin{pmatrix}
            x_1       & x_2       & 0     \\
            y_1       & y_2       & 0     \\
            1         & 1         & 1         \\
        \end{pmatrix}
},
$$
where $x$ and $y$ are coordinates of the point you are mapping and $\vec{i}$, $\vec{j}$ are unitary vectors along coordinate axes. Now we can perform simplification
$$
\vec{f}(x; y) =
   x \frac{(x_3 y_2 - x_4 y_1) \vec{i} + (y_3 y_2 - y_4 y_1) \vec{j}}
          {x_1 y_2 - x_2 y_1} +
   y \frac{(x_4 x_1 - x_3 x_2) \vec{i} + (y_4 x_1 - y_3 x_2) \vec{j}}
          {x_1 y_2 - x_2 y_1},
$$
or write it in a matrix form
$$
\vec{f}(x; y) =
    \frac{1}{x_1 y_2 - x_2 y_1}
    \begin{pmatrix}
    (x_3 y_2 - x_4 y_1) & (x_4 x_1 - x_3 x_2) \\
    (y_3 y_2 - y_4 y_1) & (y_4 x_1 - y_3 x_2)
    \end{pmatrix}
    \begin{pmatrix}
    x \\
    y
    \end{pmatrix}.
$$
To see how examples of this kind are solved you may check "Workbook on mapping simplexes affinely", where authors of the equation above demonstrate many more examples of its usage. Besides, you may want to check their description of the theory behind the scenes "Beginner's guide to mapping simplexes affinely".
