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);
};
Another way is to use Householder reflectors.
We can find a reflector Q that maps our vector to a multiple of (1,0,0). Applying Q to (0,1,0) will give a vector perpendicular to our vector. One advantage of this method is that it applies to any number of dimensions; another is that we can get the other vector(s) perpendicular to the original and the new: apply Q to (0,0,1). It might sound complicated, but here's the C code for 3d (xp,yp,zp is the required vector, and has length 1; as written everything is a double, but you could use float instead and use hypotf instead of hypot)
Heres a C version which uses the dominant axis to give a more deterministic result.
The caller needs to normalize the result of
ortho_v3_v3
.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.
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.
Hope this helps. Good luck & keep up the good work.
You need to choose a point v that is not equal to zero and not on the line joining origin with the given unit vector u.
As already suggested, you can choose a unit vector on any axis so long as that point satisfies the above condition. If the point u already lies on an axis, then just choose any other axis for point v.
Then you need to solve the equation
(v + tu).u = 0
. (just solve for t)Ofcourse you will need to normalize it to get the orthogonal unit vector.