# Rotate 1 plane in 3d space to be parallel to another

I'm not sure if this is the right forum.

I'm actually trying to solve this for a computer program

given shape A and shape B i'd like to rotate shape A so a given face is parallel to the nearest face of shape B

http://postimage.org/image/shb6kyv6d/

attached image but i cant see it. the link
http://postimage.org/image/shb6kyv6d/

i've honestly been working on this problem for hours.
When I run the program The issue i have is it keep keeps alternating between 4 rotations or I can only get the same face everytime to rotate towards it like.
cross Shape A's right with shape B's normal and it produces a plane perpindicular to shape B's plane but the same side of shape A always faces it, not just the nearest.

If i try and change it I sometimes end up with the shape shifting between 4 different rotations.
Presumably the 4 quadrants I think because i'm giving it euler angles and they can represent one of 4 valid rotations but i've no idea how to convert that angle into a single quaternion rotation.

I have the quaternion of each shape but I dont understand quaternions or how to change them properly.

Any help would be much appreciated on what the general function should look like.
I think part of the problem is not knowing what information exactly i need to perform the rotation.

The quaternion of each shape? The quaternions describing their orientation with respect to a reference direction?

You know the two normal vectors, so this is not a difficult computation. You can find the angle between the vectors by either the dot or cross product.

Find the unit vector result of the cross product, and this is the "imaginary" that you use to build the quaternion.

That is to say, if your normal vectors are $a,b$, then the quaternion that transforms $a$ to $b$ is

$$q = \cos \frac{\theta}{2} + \frac{a \times b}{|a \times b|} \sin \frac{\theta}{2}$$

where $\sin \theta = \frac{a \times b}{|a||b|}$.