Computing two vectors that are perpendicular to third vector in 3D

后端 未结 5 2056
無奈伤痛
無奈伤痛 2021-01-18 03:06

What is the best (fastest) way to compute two vectors that are perpendicular to the third vector(X) and also perpendicular to each other?

This is ho

5条回答
  •  时光取名叫无心
    2021-01-18 03:48

    For a good HELPER vector: find the coordinate of X with the smallest absolute value, and use that coordinate axis:

    absX = abs(X.x); absY = abs(X.y); absZ = abs(X.z);
    if(absX < absY) {
      if(absZ < absX)
        HELPER = vector(0,0,1);
      else // absX <= absZ
        HELPER = vector(1,0,0);
    } else { // absY <= absX
      if(absZ < absY)
        HELPER = vector(0,0,1);
      else // absY <= absZ
        HELPER = vector(0,1,0);
    }
    

    Note: this is effectively very similar to @MBo's answer: taking the cross-product with the smallest coordinate axis is equivalent to setting the smallest coordinate to zero, exchanging the larger two, and negating one.

提交回复
热议问题