【问题标题】:Matrix rotation in numpy python, diffrent vector's lengthnumpy python中的矩阵旋转,不同的向量长度
【发布时间】:2016-04-12 22:00:37
【问题描述】:

我想在 pygame 中制作一个 2D 游戏,为此,我想使用数学矩阵。以为我的想法是对的,但我遇到了问题。这里是:所以我创建一个随机点,并计算它的向量长度(从 0,0 开始),然后,通过使用矩阵平移方程(2D)我想移动那个点,比如说 +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 是不同的:/

【问题讨论】:

    标签: python numpy matrix 2d transpose


    【解决方案1】:

    您在旋转矩阵中使用arccosarcsin 而不是cossin。解决这个问题,两个向量应该具有相同的长度。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多