Beginner. 2d rotation gives unexpected results.

David Hutto dwightdhutto at gmail.com
Tue Jul 23 09:04:27 EDT 2013


haven't used pygame that much, but it sounds like you drew Z. You have
[0,0],[0,100],[100,0],[100,
100]
0,0 is the top left, if I recall 0, 100 would be the lower left, then you
move to100, 0 which would go diagonal to the top right, and then 100,100 to
the lower right, this is assuming 0,0 is the upper left. for a square you
would go,[0,0],[0,100],[100,100],[100,0]then back to [0,0] to complete the
square. This is assuming that 0,0 is the upper left, the coords are x,y in
the brackets, and the increase in x takes you the right, and the increase
in y takes you down.

If that doesn't work,I'll download it later, and try it out.


On Tue, Jul 23, 2013 at 8:34 AM, <enmce at yandex.ru> wrote:

> Hello!
> 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. Although even Excel calculations with
>
> this formulas give me right result.
> I use Python 3.3 on Windows Xp.
> What is wrong with my code?
>
> [code]import pygame
> import math as m
>
> black = ( 0, 0, 0)
> white = ( 255, 255, 255)
> green = ( 0, 255, 0)
> red = ( 255, 0, 0)
>
> class Poly():
>     pos = [100,100] #x and y coordinates of a point
>     rot = m.radians(1) #rotation in degrees
>     def draw(self): #draw point
>         pygame.draw.circle(screen,white,self.pos,10,0)
>     def rotate(self): # rotation method
>         sin = m.sin(self.rot) #calculationg sin and cos
>         cos = m.cos(self.rot)
>         x_rot = int(self.pos[0]*cos-self.pos[1]*sin) #mulpitplicating
>
> vector to rotation matrix
>         y_rot = int(self.pos[0]*sin+self.pos[1]*cos)
>
>         self.pos[0] = x_rot #set new coordinates to a point
>         self.pos[1] = 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]
>
> pygame.init()
> 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
>     b.rotate()
>     c.rotate()
>     d.rotate()
>
>     screen.fill(black)
>
>     a.draw() #draw point
>     b.draw()
>     c.draw()
>     d.draw()
>     pygame.display.flip()
>     clock.tick(30)
>
> pygame.quit()[/code]
>
> P.S. Sorry for my english, bit rusty in that department.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130723/00ba3048/attachment.html>


More information about the Python-list mailing list