<div dir="ltr">On 23 July 2013 13:34,  <span dir="ltr"><<a href="mailto:enmce@yandex.ru" target="_blank">enmce@yandex.ru</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

Hello!<br>
This is my first post, nice to meet you all!<br>
I`m biology student from Russia, trying to learn python to perform some<br>
<br>
simple simulations.<br>
<br>
Here`s my first problem.<br>
I`m trying to perform some simple 2d vector rotations in pygame, in order<br>
<br>
to learn the basics of linear algebra and 2d transformations. So far i<br>
<br>
understand matrix multiplication pretty well, and probably all my math is<br>
<br>
right. Eventually i`m planning to write Poly class, and use it to rotate<br>
<br>
and translate some simple shapes. But when i try and write it in the<br>
<br>
program, i get very weird results, like all points of rectangle with<br>
<br>
coordinates [0,0],[0,100],[100,0],[100,100] start to go spiral and<br>
<br>
eventually shrink to the center. Although even Excel calculations with<br>
<br>
this formulas give me right result.<br>
I use Python 3.3 on Windows Xp.<br>
What is wrong with my code?<br>
<br>
[code]import pygame<br>
import math as m<br></blockquote><div><br></div><div>GAH!</div><div><br></div><div>Why on earth would you do such a thing?</div><div>Just "import math", there's no need to obfuscate your code.</div><div> </div>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
black = ( 0, 0, 0)<br>
white = ( 255, 255, 255)<br>
green = ( 0, 255, 0)<br>
red = ( 255, 0, 0)<br></blockquote><div><br></div><div><div>It's probably better to do:</div><div><br></div><div>black = pygame.Color("Black")</div><div>white = pygame.Color("white")</div><div>green = pygame.Color("green")</div>

<div>red   = pygame.Color("red")</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


class Poly():<br>
    pos = [100,100] #x and y coordinates of a point </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


    rot = m.radians(1) #rotation in degrees<br></blockquote><div><br></div><div>*Do not do this*</div><div><br></div><div>This is because classes have shared values -- these "pos" and "rot" values are shared within the class.</div>

<div><br></div><div><div>>>> class P:</div><div>...     n = []</div><div>...     def more_n(self):</div><div>...         self.n.append(len(self.n))</div><div>...         </div><div>...     </div><div>... </div><div>

>>> one_P = P()</div><div>>>> two_P = P()</div><div>>>> </div><div>>>> one_P.more_n()</div><div>>>> one_P.more_n()</div><div>>>> one_P.more_n()</div><div>>>> </div>

<div>>>> two_P.n</div><div>[0, 1, 2]</div></div><div><br></div><div><br></div><div>Normally you want to set these at initialisation:</div><div><br></div><div>class Poly():</div><div>    def __init__(self, pos=None, rot=math.radians(1)):</div>

<div>        self.pos = [100, 100] if pos is None else pos</div><div>        self.rot = rot</div><div>        super().__init__(self)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


    def draw(self): #draw point<br>
        pygame.draw.circle(screen,white,self.pos,10,0)<br></blockquote><div><br></div><div>Add some spaces, dude.</div><div><br></div><div>I was going to say:</div><div><br></div><div>> Also, use keyword arguments instead of throwing around "10" and "0" with no context:</div>

<div>>     def draw(self):</div><div>>         pygame.draw.circle(screen, white, self.pos, radius=10, width=0)</div><div>> Pygame-ists will know that "width" means border_width, by now. Pygame isn't known for it's clean design ;).</div>

<div><br></div><div>But pygame, being brilliant (not) decided that it won't let you.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


    def rotate(self): # rotation method<br>
        sin = m.sin(self.rot) #calculationg sin and cos<br>
        cos = m.cos(self.rot)<br>
        x_rot = int(self.pos[0]*cos-self.pos[1]*sin) #mulpitplicating</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


vector to rotation matrix<br>
        y_rot = int(self.pos[0]*sin+self.pos[1]*cos)<br>
<br>
        self.pos[0] = x_rot #set new coordinates to a point<br>
        self.pos[1] = y_rot<br></blockquote><div><br></div><div><div><br class="">A lot of your comments are ridiculous. This one is particularly so: #mulpitplicating vector to rotation matrix. Don't add comments that talk about lines. Here is a quick guide for when to use comments:</div>

<div><br></div><div>1) API usage, when docstrings aren't usable</div><div><br></div><div>2) When something funny or unexpected occurs in the code, such as:</div><div><br></div><div>    # Goes down to 0 (does not include end-point)</div>

<div>    for i in range(foo, -1, -1): ...</div><div><br></div><div>3) To explain large-scale methodologies and algorithms</div><div><br></div><div>Other than this, are you trying to obfuscate this line? HINT: ADD SPACES ;).</div>

<div><br></div><div><br></div><div>A big problem here (this solves your problem) is your int(...) conversions. Do *not* store important information less accurately than it deserves. This is one very important instance. Only convert to int when you need to.</div>

<div><br></div></div><div>Another thing: "sin = m.sin(self.rot)"??? Really? Write "sin_1deg = ..." instead, at least.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


a = Poly() #Some simple sample points giving rectangle<br>
b = Poly()<br>
c = Poly()<br>
d = Poly()<br>
<br>
b.pos = [0,100]<br>
c.pos = [100,0]<br>
d.pos = [0,0]<br></blockquote><div><br></div><div>Use:</div><div><br></div><div>a = Poly()</div><div>b = Poly([0, 100])</div><div>c = Poly([100, 0])</div><div>d = Poly([0, 0])</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


pygame.init()<br>
size = [700,500]<br>
screen = pygame.display.set_mode(size)<br>
done = False<br>
clock = pygame.time.Clock()<br>
while done == False:<br></blockquote><div><br></div><div>"while not done"</div><div><br></div><div>Also, just use "while True" and a "break". I know some C-people or what not think this is "evil" or something, but they're wrong.</div>

<div><br></div><div>Personally, I actually like using:</div><div><br></div><div>while "rotating the squares":</div><div>    ...</div><div><br></div><div>instead of "while True". It's no slower and it's free documentation.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
    for event in pygame.event.get():<br>
        if event.type == pygame.QUIT:<br>
            done = True<br>
<br>
    a.rotate() #perform rotation<br>
    b.rotate()<br>
    c.rotate()<br>
    d.rotate()<br>
<br>
    screen.fill(black)<br>
<br>
    a.draw() #draw point<br>
    b.draw()<br>
    c.draw()<br>
    d.draw()<br>
    pygame.display.flip()<br>
    clock.tick(30)<br>
<br>
pygame.quit()[/code]<br></blockquote><div><br></div><div>You don't need pygame.quit(). You *only* want pygame.quit() if you're quitting Pygame *before* the program is finished.</div></div></div></div>