How to rotate, shift, and scale one line to another Given any two arbitrary lines and their endpoints how do you scale, shift and rotate one line to be the same position as the other (meaning one line directly on top of the other)?
I thought this would be fairly straight forward but apparently it doesn't seem to be.
The example I have come up with is:
line1 = [0,0], [100,500]
line2 - [10,23], [30,75]
*These are arbitrary values
I thought I would start by scaling line2 to be the same size as line1 via:
(When I refer to line1.x I mean all the x belonging to line1. When I refer to line1.x1 I am referring to the first x in line1. I am not familiar with this site and do not know the best way to represent these things.)
line2.x * line1_magnitude/line2_magnitude and line2.x * line1_magnitude/line2_magnitude
This did seem to scale it well. I also aligned the first points of both lines by adding the first point of line1 from the two points of line2.
line2.x + line1.x1 and
line2.y + line2.y1
My current issue is I do not know how to rotate the lines to be on top of each other. I also feel like this is not a generic way of moving one line to be on top of the second line. If anyone has a better method, I would greatly appreciate seeing it.
Example Plot
 A: You have two line segments.  One line segment is between $(x_1, y_1)$ and $(x_2, y_2)$, and the other is between $(x_3, y_3)$ and $(x_4, y_4)$.
You want a translation that brings $(x_1, y_1)$ to origin, followed by rotation and scaling, followed by a translation that brings origin to $(x_3, y_3)$.
If we have two vectors $\vec{u}$ and $\vec{v}$ that represent the new unit $x$ axis vector and unit $y$ axis vectors, i.e. are the new basis, the corresponding transformation matrix is $\left[\begin{matrix}\vec{u} & \vec{v} \end{matrix}\right]$. Here, we have just one line segment each, but we can consider them as our "$x$ axis unit vectors" from a default coordinate system.
Fortunately, in two dimensions, rotating a vector $(x, y)$ 90° counterclockwise yields $(-y, x)$.  This means that since we are not skewing the coordinate system, only translating, rotating, and uniformly scaling it, in 2D we only need that one vector. Nice!
The 2D transformation matrix (rotate and scale) that transforms line segment $(0,0)-(1,0)$ to line segment $(0,0)-(x_2-x_1, y_2-y_1)$ (i.e., the first line segment translated so it starts at origin) is thus
$$\mathbf{T}_1 = \left[\begin{matrix}
x_2 - x_1 & y_1 - y_2 \\
y_2 - y_1 & x_2 - x_1 \\
\end{matrix}\right] \tag{1}\label{1}$$
followed by a translation by $(x_1, y_1)$; and, similarly for the second line,
$$\mathbf{T}_2 = \left[\begin{matrix}
x_4 - x_3 & y_3 - y_4 \\
y_4 - y_3 & y_4 - y_3 \\
\end{matrix}\right] \tag{2}\label{2}$$
followed by a translation by $(x_3, y_3)$.
To transform the first to the second, we need the inverse of the first matrix.  (You could think of this as "reverting 'current' coordinate system back to default coordinate system"; we already have the transformation from the default coordinate system to the second one.)
The inverse of a 2×2 matrix is
$$\mathbf{M} = \left[ \begin{matrix}
m_{11} & m_{12} \\
m_{21} & m_{22} \\
\end{matrix} \right] \quad \iff \quad \mathbf{M}^{-1} = \frac{1}{m_{11} m_{22} - m_{12} m_{21}} \left[ \begin{matrix}
m_{22} & -m_{12} \\
-m_{21} & m_{11} \\
\end{matrix} \right]$$
and therefore
$$\mathbf{T}_1^{-1} = \frac{1}{(x_2 - x_1)^2 + (y_2 - y_1)^2} \left[ \begin{matrix}
x_2 - x_1 & y_2 - y_1 \\
y_1 - y_2 & x_2 - x_1 \\
\end{matrix} \right] \tag{3}\label{3}$$
and the rotation and scaling needed is
$$\mathbf{T} = \mathbf{T}_2 \mathbf{T}_1^{-1} = \left[ \begin{matrix}
T_1 & T_2 \\
T_3 & T_4 \\
\end{matrix} \right] \tag{4}\label{4}$$
where
$$\left\lbrace ~ \begin{aligned}
T_1 &= \frac{(x_2 - x_1)(x_4 - x_3) + (y_2 - y_1)(y_4 - y_3)}{(x_2 - x_1)^2 + (y_2 - y_1)^2} \\
T_2 &= \frac{(x_4 - x_3)(y_2 - y_1) - (x_2 - x_1)(y_4 - y_3)}{(x_2 - x_1)^2 + (y_2 - y_1)^2} \\
T_3 &= \frac{(x_2 - x_1)(y_4 - y_3) - (x_4 - x_3)(y_2 - y_1)}{(x_2 - x_1)^2 + (y_2 - y_1)^2} \\
T_4 &= \frac{(x_2 - x_1)(x_4 - x_3) + (y_2 - y_1)(y_4 - y_3)}{(x_2 - x_1)^2 + (y_2 - y_1)^2} \\
\end{aligned} \right . \tag{5}\label{5}$$
To apply $\mathbf{T}$, we must first translate the first line to origin, then apply $\mathbf{T}$, and finally translate the second line from origin.
Let's call $\vec{b} = (x_1, y_1)$ and $\vec{a} = (x_3, y_3)$.
Point $\vec{p}$ in the first coordinate system is then transformed to point $\vec{p}^\prime$ in the second coordinate system via
$$\vec{p}^\prime = \vec{a} + \mathbf{T}(\vec{p} - \vec{b}) \tag{6}\label{6}$$
or, using Cartesian coordinates,
$$\left\lbrace ~ \begin{aligned}
x^\prime &= x_3 + T_1 (x - x_1) + T_2 (y - y_1) \\
y^\prime &= y_3 + T_3 (x - x_1) + T_4 (y - y_1) \\
\end{aligned} \right . \tag{7}\label{7}$$
