Reputation: 45
I have this method that rotates a point counter clockwise.
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
but when i pass it a point to rotate, only the x coordinate is rotated correctly. For example I tried to rotate the point (0,25) by π/3 . I should be getting (-22,13) since i am rounding the answers. Instead I am getting (-22,-6).
Upvotes: 1
Views: 4975
Reputation: 4425
Yes you have changed x in the first equation. Thus
self.x = math.cos(rad)*self.x - math.sin(rad)*self.y
self.x = 0 - 6
so the second equation is
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)
Upvotes: 0
Reputation: 6369
The Problem here is that you save the new value for self.x
and use that same value as input for the calculation of self.y
Try this:
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
Upvotes: 2