[英]Matrix rotation in numpy python, diffrent vector's length
我想在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是不同的:/
您在旋轉矩陣中使用的是arccos
和arcsin
而不是cos
和sin
。 修正該問題,兩個向量的長度應相同。
您也可以使用np.linalg.norm
作為vectorlength。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.