0
101 Aug 08, 2008 at 15:12

Hello, I just came across a code that can find a set of three mutually orthogonal vectors U,V,N such that U X V = N when only one vector N is known. Can some one please tell me how this is working ? Why does he compare x and y coordinates ?

void compute_orthogonal_set(vector *U, vector *V, vector *N)
{
double invlength;

if (fabs(N->x) >= fabs(N->y))
{
invlength = 1 / sqrt(N->x * N->x + N->z * N->z);
U->x = -N->z * invlength;
U->y = 0;
U->z = N->x * invlength;
V->x = N->y * U->z;
V->y = N->z * U->x - N->x * U->z;
V->z = -N->y * U->x;
}
else
{
invlength = 1 / sqrt(N->y * N->y + N->z * N->z);
U->x = 0;
U->y = N->z * invlength;
U->z = -N->y * invlength;
V->x = N->y * U->z - N->z * U->y;
V->y = -N->x * U->z;
V->z = N->x * U->y;
}
}