# Rotate 3 dimensional polygon to be parallel to the X/Z plane

I have a set of points in 3 dimensional euclideon space. All points are planar and ordered, so these points form a 3 dimensional polygon $P$. The goal is to rotate this polygon so that it is parallel to the X/Z plane. To achieve this, I calculate an inverse rotation matrix $R_I^{-1}$ using the polygon's normal vector $\vec{N}$ and multiply each point with this matrix. This sometimes works but sometimes it does not and I don't know why. To calculate a rotation matrix, I calculate three orthogonal vectors $\vec{Q} = \vec{N}\times{\vec{X}}$ where $\vec{X}= \langle1,0,0\rangle$ and $\vec{R} = \vec{Q}\times{\vec{N}}$. I combine the vectors $N, Q, R$ to $R_I^{-1} = \begin{bmatrix}Q_{11} & Q_{12} & Q_{13} & 0\\N_{11} & N_{12} & N_{13} & 0\\R_{11} & R_{12} & R_{13} & 0\\0 & 0 & 0 & 1\end{bmatrix}$ and multiply it with each point of $P$.

However, this does not work for example for the polygon $P =(-1, 1, 1), (-0.5, 1, 0.5), (0, 1, 0), (0.5, 0.5, 0), (1, 0, 0), (1, -0.5, 0.5), (1, -1, 1)$ with $\vec{N}= \langle1,1,1\rangle$. I have drawn it in Blender: Polygon P

And the rotated polygon $P_R = {(0, 1, -4) (0.5, 1, -2.5) (1, 1, -1) (0.5, 1, 0.5) (0, 1, 2) (-1, 1, 2) (-2, 1, 2)}$. It is parallel to the X/Z plane since all Y-compnents are 1 but it looks different compared to the original: Rotated Polygon

I have found out that it looks correct when I calculate $\vec{Q} = \vec{N}\times{\vec{X}}$ where $\vec{X}= \langle0,0,1\rangle$ instead. But I don't know why.

• Unless the plane of the polygon includes the origin, it can’t be rotated onto the $x$-$z$ plane. There also has to be a translation. Do you perhaps mean parallel to the $x$-$z$, or are you guaranteed that your polygon’s plane passes through the origin?
– amd
Commented Aug 9, 2016 at 18:28
• You are right, I was a bit too unclear. I have fixed it.
– NMO
Commented Aug 9, 2016 at 22:50
• Did you normalize $N$ and the other two vectors? If not, you’re also introducing dilations into the transformation.
– amd
Commented Aug 10, 2016 at 2:32
• Also, $\vec Q$, $\vec N$ and $\vec R$ as described form a left-handed basis. The vertex coordinates of $P_R$ don’t match what you’d get with $\vec R=\vec N\times\vec Q$, either—their $z$-coordinates are negated. I suspect that you really set $\vec R=\vec Q\times\vec N$.
– amd
Commented Aug 10, 2016 at 7:29
• You are right, I have fixed it.
– NMO
Commented Aug 14, 2016 at 13:21

Based on your writeup, and especially on the values you give for $P_R$, it looks like you’re not normalizing the basis vectors. The plane of the polygon is at a distance of $(1,0,0)\cdot\vec N/\|\vec N\|=1/\sqrt3$ from the origin, so you should see a $y$-coordinate of about $0.577$ after rotation instead of $1$, which happens to be exactly $\|\vec N\|$ times this distance.
By not normalizing, you’re effectively performing a dilation after rotating: the $x$-axis by a factor of $\|\vec N\|\sin\theta$, the $y$-axis by $\|\vec N\|$ and the $z$-axis by $\|\vec N\|^2\sin\theta$, where $\theta$ is the angle between $\vec N$ and the $x$-axis. The $y$-dilation is hard to spot because it just moves the polygon along the $y$-axis, but the dilation in the other two dimensions distorts it. Even if you normalized $\|\vec N\|$, you’d still see the polygon shrink if you didn’t also normalize the two derived vectors. If $\vec N$ is close to the $x$-axis, this shirinkage will be quite obvious.
When you used $\vec Q=\vec N\times(0,0,1)$ instead, you had exactly the same dilation, but it wasn’t as obvious because of the orientation of the transformed polygon. If you compare it side-by-side with the original, however, the distortion will be obvious. The resulting polygon is much too large and its aspect ratio is off.
• @NMO Nothing comes to mind. It might be possible in specific cases, but in general, you’re going to have to match basis vector lengths, find a unit vector for the normal or the rotation axis, or compute sines and cosines of the rotation angle, so having radicals creep into the solution seems unavoidable. For example, you could try to perform the rotation as a sequence of two reflections, but that requires finding the angle bisector of the normal and the $y$-axis.