# Find 3D rotation matrix for a plane, given surface normal and point lying on the plane

Given two points $P_0(x_0,y_0,z_0)$ and $P_1(x_1,y_1,z_1)$, and being $P_0$ the centroid of a square delimited by the following points: $$(x_0 - 0.25, y_0 - 0.25, z_0)$$ $$(x_0 + 0.25, y_0 - 0.25, z_0)$$ $$(x_0 + 0.25, y_0 + 0.25, z_0)$$ $$(x_0 - 0.25, y_0 + 0.25, z_0)$$ we know that vector $\vec{P_0P_1} = (x_1 - x_0, y_1 - y_0, z_1 - z_0)$ should be a normal of the previous plane. I would like to find the rotation matrix for the plane that solves the issue.

• Not exactly clear what you are asking. Do you want to tilt the square in such a way that it is perpendicular to $\vec{P_0 P_1}$? Or do you need to rotate $P_1$? – Andrei Feb 28 '17 at 15:55
• @Andrei Exactly, that's the point (the first one), and $P_0$ must be the center of rotation. Sorry if I formulated the question in a bad way... – Finfa811 Feb 28 '17 at 15:58

The normal to the original square is $\vec{n}=(0,0,1)$. To calculate the rotation of $\vec{n}$ onto $\vec{p}=\vec{P_0P_1}$, the axis of rotation is given by $\vec{p}\times\vec{n}$. Yo can calculate the angle from the dot product. You can get the rotation matrix using https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle If you want to rotate the square instead, it's the same rotation axis, with the opposite sign angle
• Sorry, one more question. Now I can rotate $\vec{n}$ to make $\vec{p}$ the new normal of my square, so the rotation matrix $R$ that I have calculated is right. Is it possible to use $R$ to calculate the new position of the four points delimiting the square, or do I need another method for that purpose? – Finfa811 Mar 1 '17 at 9:18
• You can use $R$ for the square as well – Andrei Mar 1 '17 at 13:28