numpy python中的矩阵旋转,不同向量的长度

时间:2016-04-12 22:00:37

标签: python numpy matrix 2d transpose

我想在pygame中制作2D游戏,为此,我想使用数学矩阵。我想是正确的,但我遇到了问题。这是:所以我做一个随机点,我计算它的矢量长度(从0,0开始),然后,通过使用矩阵移植方程(2D),我想移动那个点,为了让&#39 ; s说+1度。我得到了一个结果,但是等等,为什么移植矢量的长度与另一个不同?所有答案将不胜感激,谢谢:)

这是我的代码:

import numpy
import math
import random

#length of wector    
def R(w0):
    #start point coordinates
    x0 = y0 = 0
    #new point coordinates    
    x1 = w0[0][0]
    y1 = w0[1][0]
    r = math.sqrt((x1 - x0)**2 + (y1-y0)**2)
    return r 

#matrix rotation by 1 deg
rad = math.pi/180
Tm = numpy.matrix([[math.acos(rad),-math.asin(rad)], [math.asin(rad), math.acos(rad)]]) 

#w0
x0 = random.randint(1, 5)
y0 = random.randint(1, 5)

w0 = numpy.matrix([[x0],[y0]])
r0 = R(w0)

print "x0: ",w0[0][0], "y0: ",w0[1][0], "r0: ", r0
#w0'

w1 = Tm * w0
r1 = R(w1)
print "x1: ",w1[0][0], "y1: ",w1[1][0], "r1: ", r1

正如你所看到的,r0和r1是不同的:/

1 个答案:

答案 0 :(得分:1)

您在轮播矩阵中使用的是arccosarcsin,而不是cossin。解决这个问题,并且两个向量具有相同的长度。

此外,您可以使用np.linalg.norm作为矢量长度。