Search code examples
mathvector3dlinear-algebraorthogonal

Given a single arbitrary unit vector, what is the best method to compute an arbitrary orthogonal unit vector?


Essentially the same question was asked here, but in a non-programming context. A suggested solution is take { y, -x, 0 }. This would work for all vectors that have an x or y component, but fails if the vector is equal to + or - { 0, 0, 1 }. In this case we would get { 0, 0, 0 }.

My current solution (in c++):

// floating point comparison utilizing epsilon
bool is_equal(float, float);

// ...

vec3 v = /* some unit length vector */

// ...

// Set as a non-parallel vector which we will use to find the 
//   orthogonal vector. Here we choose either the x or y axis.
vec3 orthog;
if( is_equal(v.x, 1.0f) )
  orthog.set(1.0f, 0.0f, 0.0f);
else
  orthog.set(0.0f, 1.0f, 0.0f);

// Find orthogonal vector
orthog = cross(v, orthog);
orthog.normalize(); 

This method works, but I feel that there may be a better method and my searches turn up nothing more.


[EDIT]

Just for fun I did a quick code up of naive implementations of each of the suggested answers in c++ and verified they all worked (though some don't always return a unit vector naturally, I added a noramlize() call where needed).

My original idea:

vec3 orthog_peter(vec3 const& v)
{
  vec3 arbitrary_non_parallel_vec = v.x != 1.0f ? vec3(1.0, 0.0f, 0.0f) : vec3(0.0f, 1.0f, 0.0f);
  vec3 orthog = cross(v, arbitrary_non_parallel_vec);

  return normalize( orthog );
}

https://stackoverflow.com/a/19650362/2507444

vec3 orthog_robert(vec3 const& v)
{
  vec3 orthog;
  if(v.x == 0.0f && v.y == 0.0f)
    orthog = vec3(1.0f, 1.0f, 0.0f);
  else if(v.x == 0.0f)
    orthog = vec3(1.0f, v.z / v.y, 1.0f);
  else if(v.y == 0.0f)
    orthog = vec3(-v.z / v.x, 1.0f, 1.0f);
  else
    orthog = vec3(-(v.z + v.y) / v.x, 1.0f, 1.0f);

  return normalize(orthog);
}

https://stackoverflow.com/a/19651668/2507444

// NOTE: u and v variable names are swapped from author's example
vec3 orthog_abhishek(vec3 const& v)
{
  vec3 u(1.0f, 0.0f, 0.0f);
  float u_dot_v = dot(u, v);

  if(abs(u_dot_v) != 1.0f)
    return normalize(u + (v * -u_dot_v));
  else
    return vec3(0.0f, 1.0f, 0.0f);
}

https://stackoverflow.com/a/19658055/2507444

vec3 orthog_dmuir(vec3 const& v)
{
  float length = hypotf( v.x, hypotf(v.y, v.z));
  float dir_scalar = (v.x > 0.0) ? length : -length;
  float xt = v.x + dir_scalar;
  float dot = -v.y / (dir_scalar * xt);

  return vec3(
    dot * xt, 
    1.0f + dot * v.y, 
    dot * v.z);
};

Solution

  • Well, here's one way to go about it. Let a vector (a, b, c) be given. Solve the equation (a, b, c) dot (aa, bb, cc) = 0 for aa, bb, and cc (and ensuring that aa, bb, and cc are not all zero), so (aa, bb, cc) is orthogonal to (a, b, c). I've used Maxima (http://maxima.sf.net) to solve it.

    (%i42) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]), a=0, b=0;
    (%o42)                 [[aa = %r19, bb = %r20, cc = 0]]
    (%i43) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]), a=0;
                                            %r21 c
    (%o43)              [[aa = %r22, bb = - ------, cc = %r21]]
                                              b
    (%i44) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]), b=0;
                                 %r23 c
    (%o44)              [[aa = - ------, bb = %r24, cc = %r23]]
                                   a
    (%i45) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]);
                            %r25 c + %r26 b
    (%o45)         [[aa = - ---------------, bb = %r26, cc = %r25]]
                                   a
    

    Note that I've solved special cases first (a = 0 and b = 0, or a = 0, or b = 0) since the solutions found aren't all valid for some components equal to zero. The %r variables which appear are arbitrary constants. I'll set them equal to 1 to get some specific solutions.

    (%i52) subst ([%r19 = 1, %r20 = 1], %o42);
    (%o52)                    [[aa = 1, bb = 1, cc = 0]]
    (%i53) subst ([%r21 = 1, %r22 = 1], %o43);
                                              c
    (%o53)                   [[aa = 1, bb = - -, cc = 1]]
                                              b
    (%i54) subst ([%r23 = 1, %r24 = 1], %o44);
                                      c
    (%o54)                   [[aa = - -, bb = 1, cc = 1]]
                                      a
    (%i55) subst ([%r25 = 1, %r26 = 1], %o45);
                                    c + b
    (%o55)                 [[aa = - -----, bb = 1, cc = 1]]
                                      a
    

    Hope this helps. Good luck & keep up the good work.