What is a formula to get a vector perpendicular to another vector?

后端 未结 6 1574
轻奢々
轻奢々 2021-01-02 11:53

What is a formula to get a three dimensional vector B lying on the plane perpendicular to a vector A?

That is, given a vector A, what is a formula f(angle,modulus) w

6条回答
  •  梦毁少年i
    2021-01-02 12:04

    One way would be to find a rotation transform from the positive z-axis (or any other axis) to your given vector. Then transform using this transform.

    def getPerpendicular(v1,modulus,angle):
        v2 = vector(0,0,1)
        v1_len = v2.length()
    
        axis = v1.cross_product(v2)
        sinAngle = axis.length() / v1_len       # |u x v| = |u| * |v| * sin(angle)
        cosAngle = v1.dot_product(v2) / v1_len  # u . v = |u| * |v| * cos(angle)
        axis = axis.normalize()
        # atan2(sin(a), cos(a)) = a, -pi < a < pi
        angle = math.atan2(sinAngle, cosAngle)
    
        rotationMatrix = fromAxisAngle(axis, angle)
    
        # perpendicular to v2
        v3 = vector(modulus*cos(angle),modulus*sin(angle),0)
    
        return rotationMatrix.multiply(v3);
    

    To calculate the rotation matrix, see this article: WP: Rotation matrix from axis and angle

    Another method would be to use quaternion rotation. It's a little more to wrap your head around, but it's less numbers to keep track of.

提交回复
热议问题