这是一个处理矩阵的解决方案(这对于这种类型的计算是有意义的,最终,2D 坐标是具有 1 列的矩阵!),
缩放非常简单,只需将矩阵的每个元素乘以缩放因子即可:
scaled = copy.deepcopy(original)
for i in range(len(scaled[0])):
scaled[0][i]=scaled[0][i]*scaleFactor
scaled[1][i]=scaled[1][i]*scaleFactor
移动非常容易,您所要做的就是将偏移量添加到矩阵的每个元素,这是使用矩阵乘法的方法:
import numpy as np
# Matrix multiplication
def mult(matrix1,matrix2):
# Matrix multiplication
if len(matrix1[0]) != len(matrix2):
# Check matrix dimensions
print 'Matrices must be m*n and n*p to multiply!'
else:
# Multiply if correct dimensions
new_matrix = np.zeros(len(matrix1),len(matrix2[0]))
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
return new_matrix
然后创建你的翻译矩阵
import numpy as np
TranMatrix = np.zeros((3,3))
TranMatrix[0][0]=1
TranMatrix[0][2]=Tx
TranMatrix[1][1]=1
TranMatrix[1][2]=Ty
TranMatrix[2][2]=1
translated=mult(TranMatrix, original)
最后,旋转有点棘手(你知道你的旋转角度吗?):
import numpy as np
RotMatrix = np.zeros((3,3))
RotMatrix[0][0]=cos(Theta)
RotMatrix[0][1]=-1*sin(Theta)
RotMatrix[1][0]=sin(Theta)
RotMatrix[1][1]=cos(Theta)
RotMatrix[2][2]=1
rotated=mult(RotMatrix, original)
关于我所做的一些进一步阅读:
- http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations
- http://en.wikipedia.org/wiki/Homogeneous_coordinates http://en.wikipedia.org/wiki/Homogeneous_coordinates
-
http://www.essentialmath.com/tutorial.htm http://www.essentialmath.com/tutorial.htm(关于所有代数变换)
所以基本上,如果您在代码中插入这些操作,将向量乘以旋转/平移矩阵,它应该可以工作
EDIT
我刚刚发现这个 Python 库似乎提供了所有类型的转换:http://toblerity.org/shapely/index.html http://toblerity.org/shapely/index.html