Question

What is the simplest way to take an input vector, given as (x,y,z), and find some new vector with a different direction than it? Any direction will do, it just has to be a different direction than the input (other than exact opposite direction, which is trivial).

It seems like there should be a simple solution that does not involve branching, but I can't seem to find one, and after some though, I'm interested to know if there actually is one.

Was it helpful?

Solution

I'm not sure how simple this is, but assuming that (x,y,z) has length L (which is not 0) the vector below has length 1 and is at right angles to (x,y,z)

-y * (x + sign(x)*L) / (L*(L+|x|))
1 - y * y / (L*(L+|x|))
-y * z / (L*(L+|x|))

(here |x| is the absolute value of x and sign(x) is -1 if x<0, and 1 if x >= 0)

I derived this formula by computing the householder reflection ( eg http://en.wikipedia.org/wiki/Householder_transformation) that maps (x,y,z) to a multiple of (1,0,0) and then computing the image of (0,1,0) under this matrix; since the matrix is both orthogonal and symmetric this vector will be orthogonal to (x,y,z).

There is no continuous function (x,y,z) -> (x',y',z') (for (x,y,z) != (0,0,0)) such that (x',y',z') is never a multiple of (x,y,z); if there were you could remove from (x',y',z') its component in the (x,y,z) direction and so get a continuous map (x,y,z)->(x'',y'',z'') where (x'',y'',z'') is at right angles to (x,y,z), but by the hairy ball theorem you can't.

In the formula above the occurrence of the discontinuous sign function makes the formula discontinuous. Note that sign needn't involve a branch; in some languages there is a build in function to do it; in C you could use 2*(x>=0)-1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top