This is my first post, nice to meet you all!
I`m biology student from Russia, trying to learn python to perform some simple simulations.
Here`s my first problem.
I`m trying to perform some simple 2d vector rotations in pygame, in order to learn the basics of linear algebra and 2d transformations. So far i understand matrix multiplication pretty well, and probably all my math is right. Eventually i`m planning to write Poly class, and use it to rotate and translate some simple shapes. But when i try and write it in the program, i get very weird results, like all points of rectangle with coordinates [0,0],[0,100],[100,0],[100,100] start to go spiral and eventually shrink to the center instead of rotating right. Although even Excel calculations with this formulas give me right result.
What is wrong with my code?
- Code: Select all
import math as m
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
pos = [100,100] #x and y coordinates of a point
rot = m.radians(1) #rotation in degrees
def draw(self): #draw point
def rotate(self): # rotation method
sin = m.sin(self.rot) #calculationg sin and cos
cos = m.cos(self.rot)
x_rot = int(self.pos*cos-self.pos*sin) #mulpitplicating vector to rotation matrix
y_rot = int(self.pos*sin+self.pos*cos)
self.pos = x_rot #set new coordinates to a point
self.pos = y_rot
a = Poly() #Some simple sample points giving rectangle
b = Poly()
c = Poly()
d = Poly()
b.pos = [0,100]
c.pos = [100,0]
d.pos = [0,0]
size = [700,500]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
a.rotate() #perform rotation
a.draw() #draw point
P.S. I use Python 3.3. Sorry for my english, bit rusty in that department.