Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

python实现【x1,y1,z1】向量绕[x2,y2,z2]向量 旋转45度

  1. User

    python实现【x1,y1,z1】向量绕[x2,y2,z2]向量 旋转45度

  2. Assistant

    以下是Python实现的代码:

    import math

    定义向量和旋转角度

    v1 = [1, 2, 3] v2 = [4, 5, 6] angle = 45

    计算向量叉积

    cross = [v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1]-v1[1]*v2[0]]

    计算向量模长

    v1_magnitude = math.sqrt(v1[0]**2 + v1[1]**2 + v1[2]**2) v2_magnitude = math.sqrt(v2[0]**2 + v2[1]**2 + v2[2]**2) cross_magnitude = math.sqrt(cross[0]**2 + cross[1]**2 + cross[2]**2)

    计算旋转角弧度值

    theta = math.radians(angle)

    计算旋转矩阵

    cos_theta = math.cos(theta) sin_theta = math.sin(theta) rot_matrix = [[cos_theta+(1-cos_theta)*v2[0]**2, (1-cos_theta)*v2[0]v2[1]-sin_thetav2[2], (1-cos_theta)*v2[0]v2[2]+sin_thetav2[1]], [(1-cos_theta)*v2[1]v2[0]+sin_thetav2[2], cos_theta+(1-cos_theta)*v2[1]**2, (1-cos_theta)*v2[1]v2[2]-sin_thetav2[0]], [(1-cos_theta)*v2[2]v2[0]-sin_thetav2[1], (1-cos_theta)*v2[2]v2[1]+sin_thetav2[0], cos_theta+(1-cos_theta)*v2[2]**2]]

    计算旋转后的向量

    rotated_v = [(v1[0](v1_magnitude-cross_magnitudecos_theta)+cross[0]*sin_theta+(v1[1]*rot_matrix[0][1]+v1[2]rot_matrix[0][2])cos_theta)/v2_magnitude, (v1[1](v1_magnitude-cross_magnitudecos_theta)+cross[1]*sin_theta+(v1[0]*rot_matrix[1][0]+v1[2]rot_matrix[1][2])cos_theta)/v2_magnitude, (v1[2](v1_magnitude-cross_magnitudecos_theta)+cross[2]*sin_theta+(v1[0]*rot_matrix[2][0]+v1[1]*rot_matrix[2][1])*cos_theta)/v2_magnitude]

    print(rotated_v) # 输出旋转后的向量