Search code examples
3dvector-graphicsradix

Construct an orthonormal base given only one vector in 3d


I'm looking for a simple and efficient way to solve the following problem:

I have one vector in 3d and I want to get an orthonormal base (x, y, z) where one of the base vectors (let's say x) is the given vector. So I'm looking for two vectors, perpendicular to each other, that are also perpendicular to my given vector.

I know that this has infinite many solutions, but I don't care which one I get, as long as it satisfies the above requirements and getting it is simple and efficient.


Solution

  • Let's call x your unit vector. Call u = (1,0,0). If dot(u,x) ~= 0, then take u = (0,1,0). Then, y = x ^ u and z = x ^ y.