A mapping that converts a line segment to another one

analytic geometryfunctionsgeometry

I'm looking for a mapping $f:\mathbb{R}^2\to\mathbb{R}^2$ that converts a line segment $AA'$ with two end points $A=(x_A,y_A)$ and $A=(x_{A'},y_{A'})$ to another line segment $BB'$ with end points $B=(x_B,y_B)$ and $B=(x_{B'},y_{B'})$. Does anyone know closed-form relations for such mappings that preserve the linearity (always convert a line segment to another line segment)?

Best Answer

Let's use $$\vec{p}_0 = \left[\begin{matrix} x_A \\ y_A \end{matrix}\right], \vec{q}_0 = \left[\begin{matrix} x_{A^\prime} \\ y_{A^\prime} \end{matrix}\right]$$ for the endpoints of the line segment in the first coordinate system, and $$\vec{p}_1 = \left[\begin{matrix} x_B \\ y_B \end{matrix}\right], \vec{q}_1 = \left[\begin{matrix} x_{B^\prime} \\ y_{B^\prime} \end{matrix}\right]$$ the corresponding points in the second coordinate system.

Let's define two scalars, corresponding to the lengths of the line segments, and two unit vectors, corresponding to the directions of the line segments: $$\begin{aligned} L_0 &= \left\lVert \vec{q}_0 - \vec{p}_0 \right\rVert \\ L_1 &= \left\lVert \vec{q}_1 - \vec{p}_1 \right\rVert \\ \hat{n}_0 &= \displaystyle \frac{\vec{q}_0 - \vec{p}_0}{L_0} = \left[\begin{matrix} x_0 \\ y_0 \end{matrix}\right] = \frac{1}{L_0} \left[\begin{matrix} x_{A^\prime} - x_A \\ y_{A^\prime} - y_A \end{matrix}\right] \\ \hat{n}_1 &= \displaystyle \frac{\vec{q}_1 - \vec{p}_1}{L_1} = \left[\begin{matrix} x_1 \\ y_1 \end{matrix}\right] = \frac{1}{L_1} \left[\begin{matrix} x_{B^\prime} - x_B \\ y_{B^\prime} - y_B \end{matrix}\right] \\ \end{aligned}$$ so that $\lVert\hat{n}_0\rVert = \lVert\hat{n}_1\rVert = 1$, and $$\begin{aligned} \vec{q}_0 &= \vec{p}_0 + L_0 \hat{n}_0 \\ \vec{q}_1 &= \vec{p}_1 + L_1 \hat{n}_1 \\ \end{aligned}$$

A matrix that rotates positive $x$ axis towards $\hat{n}_0$ is $$\mathbf{R}_0 = \left[ \begin{matrix} x_0 & -y_0 \\ y_0 & x_0 \end{matrix} \right]$$ and the matrix that rotates positive $x$ axis towards $\hat{n}_1$ is $$\mathbf{R}_1 = \left[ \begin{matrix} x_1 & -y_1 \\ y_1 & x_0 \end{matrix} \right]$$ Because $x_0^2 + y_0^2 = 1$ and $x_1^2 + y_1^2 = 1$, the above two matrices are orthogonal, and describe a pure rotation. Because they are orthogonal, their inverse is their transpose.

What we need, is a translation that moves $\vec{p}_0$ to origin, then inverts the rotation by $\mathbf{R}_0$, so that $\vec{p}_1$ will be at $[L_0, 0]^T$. Then, we apply a rotation by $\mathbf{R}_1$, scale by the ratio of the lengths of the line segments, and finally translate by $\vec{p}_1$. Using $\vec{v}_0$ for a point in the old coordinate system, and $\vec{v}_1$ for the corresponding point in the new coordinate system, the transform is described by $$\vec{v}_1 = \vec{p}_1 + \frac{L_1}{L_0}\mathbf{R}_1 \mathbf{R}_0^T ( \vec{v}_0 - \vec{p}_0 )$$ or, grouping the fixed translation rightmost, $$\vec{v}_1 = \left(\frac{L_1}{L_0}\mathbf{R}_1 \mathbf{R}_0^T \vec{v}_0 \right) + \left(\vec{p}_1 - \frac{L_1}{L_0} \mathbf{R}_1 \mathbf{R}_0^T \vec{p}_0 \right)$$ A typical format for expressing rotation, scaling, and translation is $$\vec{v}_1 = \mathbf{R} \vec{v}_0 + \vec{t} \quad \iff \quad \left[\begin{matrix} \chi_1 \\ \gamma_1 \end{matrix}\right] = \left[\begin{matrix} u_x & -u_y \\ u_y & u_x \end{matrix}\right] \left[\begin{matrix} \chi_0 \\ \gamma_0 \end{matrix}\right] + \left[\begin{matrix} t_x \\ t_y \end{matrix}\right]$$ which using homogenous coordinates (which programmers often use) is $$\left[\begin{matrix} \chi_1 \\ \gamma_1 \\ 1 \end{matrix}\right] = \left[\begin{matrix} u_x & -u_y & t_x \\ u_y & u_x & t_y \\ 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} \chi_0 \\ \gamma_0 \\ 1 \end{matrix} \right]$$ where $$\begin{aligned} A_x &= x_{A^\prime} - x_A \\ A_y &= y_{A^\prime} - y_A \\ A^2 &= A_x^2 + A_y^2 \\ B_x &= x_{B^\prime} - x_B \\ B_y &= y_{B^\prime} - y_B \\ u_x &= \displaystyle \frac{A_x B_x + A_y B_y}{A^2} \\ u_y &= \displaystyle \frac{A_x B_y - A_y B_x}{A^2} \\ t_x &= \displaystyle x_B - \frac{x_A ( A_x B_x + A_y B_y ) + y_A ( A_y B_x - A_x B_y )}{A^2} \\ t_y &= \displaystyle y_B - \frac{x_A ( A_x B_y - A_y B_x ) + y_A ( A_x B_x + A_y B_y )}{A^2} \\ \end{aligned}$$

Related Question