# how can I rotate a tetrahedron's vertices so that it's pointing straight up?

I am trying to render an Equilateral triangle in 3D, and I came across this answer: https://math.stackexchange.com/a/562895/15497

So based off of that, I made my vertex points:

(1, 1, 1),
(1, -1, -1),
(-1, -1, 1),
(-1, 1, -1),


This looks great when I draw lines across these points, however the triangle is not pointing straight up as I would like it to. I am trying to understand how to calculate the right amount to rotate the X, and Z axis so that it is pointing upwards?

I messed around with rotating the X by PI / 5 and Z by -PI / 5 and got something close:

But that is not right...

Let's designate the point $$\(1, 1, 1\$$) as the one you want to rotate to point straight up.

Normalized, that's $$\\frac 1 {\sqrt 3} (1, 1, 1)\$$. So its dot product with the unit vector pointing straight up is just $$\\frac 1 {\sqrt 3}\$$, meaning the angle between them is $$\\cos^{-1}( \frac 1 {\sqrt 3})\$$. Rotate by this angle along the axis $$\x = z\$$.

Or even better, we can just build the tetrahedron point up by construction. Let's say we want it to have an edge length of 1. Then we can draw the base of the tetrahedron in the $$\xz\$$ plane like so:

That gives us the x and z coordinates of the three base vertices, and we can place the apex vertex somewhere above the cross at the center $$\(x, z) = (0, 0)\$$. Now we just need to know what (positive) height $$\h\$$ will put it exactly 1 unit away from the others.

\begin{align} (h - 0)^2 + (0 - \frac {\sqrt 3} 3)^2 &= 1^2\\ h^2 + \frac 3 9 &= 1\\ h^2 &= 1 - \frac 1 3\\ h &= \sqrt \frac 2 3 \end{align}

So, you can place the other three vertices on the plane $$\y = 0\$$, and place the apex vertex at $$\y = \sqrt \frac 2 3\$$. Or, if you'd like the origin to sit at the centroid, you can subtract the centroid height $$\c\$$ from all those $$\y\$$ coordinates.

\begin{align} (c - 0)^2 + (0 - \frac {\sqrt 3} 3)^2 &= (h - c)^2\\ c^2 + \frac 3 9 &= h^2 - 2 hc + c^2\\ \frac 1 3 &= \frac 2 3 - 2\sqrt \frac 2 3 c\\ 2\sqrt \frac 2 3 c &= \frac 2 3 - \frac 1 3\\ c &= \frac 1 3 \cdot \frac 1 2 \sqrt \frac 3 2\\ c &= \frac {\sqrt 3} {6 \sqrt 2}\\ c &= \frac 3 {6 \sqrt 6}\\ c &= \frac 1 {2 \sqrt 6} \end{align}

You basically want the vector <1, 1, 1> to be rotated to point up <0, 1, 0> - or rather <0, k, 0> where k is some constant.

Copying the matrix I use in domain-rotating noise functions the opposite direction, and transposing to invert (works because rotation matrix <=> orthonormal matrix), you can use this to transform each vertex.

\begin{align} \begin{bmatrix} 0.788675134594813 & -0.577350269189626 & -0.211324865405187\\ 0.577350269189626 & 0.577350269189626 & 0.577350269189626\\ -0.211324865405187 & -0.577350269189626 & 0.788675134594813 \end{bmatrix} \end{align}

note that -0.211... = G2 = (SQRT3-3)/6, 0.788... = G2+1, and 0.577... = SQRT3/3