Rotate a line about another line

347 Views Asked by At

I have two graphs say A and B in X,Y plane. I have coordinates of each edge of these graphs. I want to connect graph A and Graph B by merging one edge from B to an edge of A. These edges are located at the exterior of the graphs and are identical in length. Say the edge from A is edgeA and from B is edgeB. They are oriented differently. So I have to translate and rotate edgeB to edgeA position. This transformation will be applied to all of the edges of B. How do I apply this transformation mathematically?

Edit: What I'm thinking is, I can figure out how much the edgeB needs to rotated to be parallel to edgeA. And then also calculate how much edgeB needs to be translated to be same as edgeA. This rotation and translation then I will apply to every vertices on graph B. Is this correct way to achieve what I'm trying?

1

There are 1 best solutions below

0
On BEST ANSWER

I’m going to describe the required transformation in terms of vector and matrix operations, but you can expand the final result into a pair of transformation formulas if that’s more convenient.

There are several ways to construct this transformation. I think the most straightforward is to decompose it into the sequence translate-rotate-translate. Let the endpoints of edge A be $\mathbf p$ and $\mathbf q$, and the corresponding endpoints of edge B be $\mathbf p'$ and $\mathbf q'$. The transformation then consists of translating $\mathbf p$ to the origin, rotating, and then translating to $\mathbf p'$.

The two translations are trivial to construct. The rotation could be built by computing the angle between the vectors and constructing the corresponding rotation matrix, but you can avoid computing any trigonometric functions by breaking it down into a pair of simpler rotations. Let $\mathbf v=(v_x,v_y)={\mathbf q-\mathbf p\over\|\mathbf q-\mathbf p\|}$ and $\mathbf v'=(v_x',v_y')={\mathbf q'-\mathbf p'\over\|\mathbf q'-\mathbf p'\|}$. We first rotate to bring $\mathbf v$ to the $x$-axis and then rotate the $x$-axis to align it with $\mathbf v'$. Using the fact that the columns of a transformation matrix are the images of the basis vectors and that the inverse of a rotation matrix is its transpose, the matrix representing this pair of rotations is computed as $$R=\begin{bmatrix}v'_x&-v'_y\\v'_y&v'_x\end{bmatrix}\begin{bmatrix}v_x&v_y\\-v_y&v_x\end{bmatrix}=\begin{bmatrix}v_x v_x'+v_y v_y' & -(v_x v_y'-v_y v_x') \\ v_x v_y'-v_y v_x'& v_x v_x'+v_y v_y' \end{bmatrix}.$$ (You might recognize the entries of this matrix as the differences of sines and cosines.)

Putting this all together, the transformation that maps edge A onto edge B is $\mathbf x'=R(\mathbf x-\mathbf p)+\mathbf p'$. By linearity of $R$, we can rewrite this as $R\mathbf x+(\mathbf p'-R\mathbf p)$. The parenthesized term is constant, so if you have a lot of points to transform you can improve the efficiency of the process by precomputing this fixed translation.