一种可能的图像来自C_prime = A_prime + R(C - A)
等,其中R
是需要AB
到A_prime B_prime
的旋转矩阵,C是任何其他点.旋转矩阵(通常)可以从这两个向量之间的夹角和它们的叉积形成的轴中找到.除非它们是共线的,因此叉积是0.
请注意,此变换不是唯一的,因为您可以随后应用您喜欢的大约A_prime B_prime
的任何旋转,并且仍然保持距离和相对Angular .
您还必须强制要求AB
具有与A_prime B_prime
相同的长度.
在下面的代码中,点是数组pts[,]
的行.我为糟糕的3-d剧情道歉:这不是我的强项.
import math
import numpy as np
import matplotlib.pyplot as plt
def rotationMatrix( a, theta ): # rotation matrix for active rotation, angle theta about axis a
R = np.zeros( ( 3, 3 ) )
n = a / np.linalg.norm( a )
C = math.cos( theta )
S = math.sin( theta )
R[0,0] = C + n[0] * n[0] * ( 1.0 - C )
R[0,1] = n[0] * n[1] * ( 1.0 - C ) - n[2] * S
R[0,2] = n[0] * n[2] * ( 1.0 - C ) + n[1] * S
R[1,1] = C + n[1] * n[1] * ( 1.0 - C )
R[1,2] = n[1] * n[2] * ( 1.0 - C ) - n[0] * S
R[1,0] = n[1] * n[0] * ( 1.0 - C ) + n[2] * S
R[2,2] = C + n[2] * n[2] * ( 1.0 - C )
R[2,0] = n[2] * n[0] * ( 1.0 - C ) - n[1] * S
R[2,1] = n[2] * n[1] * ( 1.0 - C ) + n[0] * S
return R
pts = np.array( [ [0,0,0], [1,0,0], [1,1,0], [0.5,1.5,0.5], [0,1,0] ], dtype=float )
A = pts[0,:]
B = pts[1,:]
Aprime = np.array( [2,2,0] )
Bprime = np.array( [2,2,-1] )
# Ensure that A'B' has the same length as AB
length = np.linalg.norm( B - A )
Bprime = Aprime + length * ( Bprime - Aprime ) / np.linalg.norm( Bprime - Aprime )
# Rotation matrix
rotate = rotationMatrix( np.cross( B - A, Bprime - Aprime ), math.acos( np.dot( Bprime - Aprime, B - A ) / length ** 2 ) )
# Transform points
ptsprime = np.zeros_like( pts )
for i in range( len( pts[:,0] ) ): ptsprime[i,:] = Aprime + rotate @ ( pts[i,:] - A ).T
# Draw it
ax = plt.figure().add_subplot( projection='3d' )
ax.plot( pts[:,0], pts[:,1], pts[:,2], color='r' )
ax.plot( ptsprime[:,0], ptsprime[:,1], ptsprime[:,2], color='b' )
ax.set_aspect( 'equal' )
plt.show()
Output: