2016-03-10 160 views
0

我有這種方法,逆時針旋轉一個點。python逆時針旋轉一個角度的角度

def rotate(self, rad): 
    self.x = math.cos(rad)*self.x - math.sin(rad)*self.y 
    self.y = math.sin(rad)*self.x + math.cos(rad)*self.y 

但是當我通過它一個點旋轉,只有x座標旋轉正確。例如,我試着將點(0,25)旋轉π/ 3。我應該得到(-22,13),因爲我四捨五入了答案。相反,我得到(-22,-6)。

回答

0

這裏的問題是,你保存新的價值爲self.x和使用相同的值作爲輸入的self.y

計算試試這個:在第一

def rotate(self, rad): 
    x = math.cos(rad)*self.x - math.sin(rad)*self.y 
    self.y = math.sin(rad)*self.x + math.cos(rad)*self.y 
    self.x = x 
+0

非常感謝你 – cleacrij

0

是的,你已經改變了X方程。因此

self.x = math.cos(rad)*self.x - math.sin(rad)*self.y 

self.x = 0 - 6 

所以第二個公式是

self.y = math.sin(rad)*self.x + math.cos(rad)*self.y 
self.y = math.sin(math.pi/3)*(-6) + math.cos(math.pi/3)*25 

>>> def rotate(x, y, rad): 
     print x, y, rad 
     xx = math.cos(rad)*x - math.sin(rad)*y 
     yy = math.sin(rad)*x + math.cos(rad)*y 
     print xx, yy 
     return(xx, yy) 

>>> rotate(0, 25, math.pi/3) 
0 25 1.0471975512 
-21.6506350946 12.5 
(-21.650635094610966, 12.500000000000004)