Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '*not*': 0.07; 'class,': 0.07; '[0,': 0.09; 'arguments': 0.09; 'finished.': 0.09; 'instance.': 0.09; 'large-scale': 0.09; 'linear': 0.09; 'lines.': 0.09; 'spaces': 0.09; 'translate': 0.10; 'api': 0.11; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'windows': 0.15; '"0"': 0.16; '*before*': 0.16; '*do': 0.16; '*only*': 0.16; '-1):': 0.16; '-1,': 0.16; 'biology': 0.16; 'brilliant': 0.16; 'code?': 0.16; 'comments:': 0.16; 'context:': 0.16; 'coordinates': 0.16; 'docstrings': 0.16; 'false:': 0.16; 'hint:': 0.16; 'least.': 0.16; 'personally,': 0.16; 'pygame': 0.16; 'pygame,': 0.16; 'really?': 0.16; 'rotation': 0.16; 'skip:[ 30': 0.16; 'unexpected': 0.16; 'usable': 0.16; 'usage,': 0.16; 'weird': 0.16; 'student': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'normally': 0.19; 'result.': 0.19; 'skip:p 40': 0.19; '>>>': 0.22; 'code,': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; "aren't": 0.24; 'math': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'class.': 0.26; 'right.': 0.26; 'excel': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'skip:p 30': 0.29; '(this': 0.29; 'points': 0.29; 'message-id:@mail.gmail.com': 0.30; 'program,': 0.31; 'comments': 0.31; '255,': 0.31; 'clock': 0.31; 'occurs': 0.31; 'pos': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'skip:# 10': 0.33; 'screen': 0.34; 'skip:_ 10': 0.34; 'problem': 0.35; 'classes': 0.35; 'problem.': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'earth': 0.36; 'false': 0.36; 'keyword': 0.36; 'done': 0.36; 'method': 0.36; 'wrong': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'planning': 0.38; 'explain': 0.39; 'skip:p 20': 0.39; '8bit%:6': 0.40; 'even': 0.60; 'algorithms': 0.60; 'eventually': 0.60; 'free': 0.61; 'black': 0.61; 'new': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'you.': 0.62; 'information': 0.63; 'such': 0.63; 'july': 0.63; 'decided': 0.64; 'within': 0.65; 'here': 0.66; 'subject:. ': 0.67; 'sample': 0.67; 'funny': 0.74; 'as:': 0.81; 'center.': 0.81; 'all!': 0.84; 'calculations': 0.84; 'matrix.': 0.84; 'results,': 0.84; 'white,': 0.84; 'do:': 0.91; 'russia,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=cB+hR+s5YyI4V9uuyWlapoBgGdpq0XUT+TzyyzHbVpo=; b=keyFJtOysg8hg9FN8nclvyu6wyuoRXdFeEMSV8wS0rQPZVqxGFxRU/2bZ+X/iIOkv8 e6NwuJCVHIFwdEcWEUUxkxBzB7RMe/faP9/PrpvJUBrb1s8qI378Q4/SMYh3t2fIdId0 Y9G+NWQgAgIHq2ix0fbLFWqzNdFxeCAC+7nAEIHbvUM+vgeJJfpJyRGjICwR7DcFVMJU oWvajIPNuTqT6s6EQgH/ZSb5mYnnQEl0VMUPT7W8J2jErmZoWMT2dRmf3cHpD1wlguF+ WkP82pHbqYo8IHhS7WapX0gxZbtyDqWppdv98/QHqcGsF2I+IBGlMji8IYb6uBzrR1vG Hi7g== X-Received: by 10.112.170.166 with SMTP id an6mr17431542lbc.22.1374700682718; Wed, 24 Jul 2013 14:18:02 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: <0a905ff1-199c-4900-81e6-d9b7bb63bb44@googlegroups.com> References: <0a905ff1-199c-4900-81e6-d9b7bb63bb44@googlegroups.com> From: Joshua Landau Date: Wed, 24 Jul 2013 22:17:22 +0100 X-Google-Sender-Auth: pDv8AotmZtS3fEPG1I0UUN3blqM Subject: Re: Beginner. 2d rotation gives unexpected results. To: enmce@yandex.ru Content-Type: multipart/alternative; boundary=001a11c37360ddb54304e24871ba Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 427 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374700684 news.xs4all.nl 15936 [2001:888:2000:d::a6]:54935 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51168 --001a11c37360ddb54304e24871ba Content-Type: text/plain; charset=UTF-8 On 23 July 2013 13:34, 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 > GAH! Why on earth would you do such a thing? Just "import math", there's no need to obfuscate your code. > black = ( 0, 0, 0) > white = ( 255, 255, 255) > green = ( 0, 255, 0) > red = ( 255, 0, 0) > It's probably better to do: black = pygame.Color("Black") white = pygame.Color("white") green = pygame.Color("green") red = pygame.Color("red") > class Poly(): > pos = [100,100] #x and y coordinates of a point rot = m.radians(1) #rotation in degrees > *Do not do this* This is because classes have shared values -- these "pos" and "rot" values are shared within the class. >>> class P: ... n = [] ... def more_n(self): ... self.n.append(len(self.n)) ... ... ... >>> one_P = P() >>> two_P = P() >>> >>> one_P.more_n() >>> one_P.more_n() >>> one_P.more_n() >>> >>> two_P.n [0, 1, 2] Normally you want to set these at initialisation: class Poly(): def __init__(self, pos=None, rot=math.radians(1)): self.pos = [100, 100] if pos is None else pos self.rot = rot super().__init__(self) > def draw(self): #draw point > pygame.draw.circle(screen,white,self.pos,10,0) > Add some spaces, dude. I was going to say: > Also, use keyword arguments instead of throwing around "10" and "0" with no context: > def draw(self): > pygame.draw.circle(screen, white, self.pos, radius=10, width=0) > Pygame-ists will know that "width" means border_width, by now. Pygame isn't known for it's clean design ;). But pygame, being brilliant (not) decided that it won't let you. 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 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: 1) API usage, when docstrings aren't usable 2) When something funny or unexpected occurs in the code, such as: # Goes down to 0 (does not include end-point) for i in range(foo, -1, -1): ... 3) To explain large-scale methodologies and algorithms Other than this, are you trying to obfuscate this line? HINT: ADD SPACES ;). 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. Another thing: "sin = m.sin(self.rot)"??? Really? Write "sin_1deg = ..." instead, at least. > 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] > Use: a = Poly() b = Poly([0, 100]) c = Poly([100, 0]) d = Poly([0, 0]) pygame.init() > size = [700,500] > screen = pygame.display.set_mode(size) > done = False > clock = pygame.time.Clock() > while done == False: > "while not done" 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. Personally, I actually like using: while "rotating the squares": ... instead of "while True". It's no slower and it's free documentation. > 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] > You don't need pygame.quit(). You *only* want pygame.quit() if you're quitting Pygame *before* the program is finished. --001a11c37360ddb54304e24871ba Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 23 July 2013 13:34, <enmce@yandex.ru> wro= te:
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

GAH!

Why on earth would you do such a thing?
Just "import= math", there's no need to obfuscate your code.
=C2=A0
black =3D ( 0, 0, 0)
white =3D ( 255, 255, 255)
green =3D ( 0, 255, 0)
red =3D ( 255, 0, 0)

It's prob= ably better to do:

black =3D pygame.Color("Bl= ack")
white =3D pygame.Color("white")
gr= een =3D pygame.Color("green")
red =C2=A0 =3D pygame.Color("red")
=C2=A0
class Poly():
=C2=A0 =C2=A0 pos =3D [100,100] #x and y coordinates of a point=C2=A0
=C2=A0 =C2=A0 rot =3D m.radians(1) #rotation in degrees

*Do not do this*

This is because = classes have shared values -- these "pos" and "rot" val= ues are shared within the class.

>>> class P:
... =C2=A0 =C2=A0 = n =3D []
... =C2=A0 =C2=A0 def more_n(self):
... =C2=A0= =C2=A0 =C2=A0 =C2=A0 self.n.append(len(self.n))
... =C2=A0 =C2= =A0 =C2=A0 =C2=A0=C2=A0
... =C2=A0 =C2=A0=C2=A0
...=C2= =A0
>>> one_P =3D P()
>>> two_P =3D P()
&= gt;>>=C2=A0
>>> one_P.more_n()
>>&= gt; one_P.more_n()
>>> one_P.more_n()
>>= >=C2=A0
>>> two_P.n
[0, 1, 2]


Normally you want to set these at initialisation:

class Poly():
=C2=A0 =C2=A0 def __init__(self, p= os=3DNone, rot=3Dmath.radians(1)):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.pos =3D [100, 100] if pos is None els= e pos
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.rot =3D rot
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(self)
=C2=A0
=C2=A0 =C2=A0 def draw(self): #draw point
=C2=A0 =C2=A0 =C2=A0 =C2=A0 pygame.draw.circle(screen,white,self.pos,10,0)<= br>

Add some spaces, dude.

I was going to say:

> Also, use keywor= d arguments instead of throwing around "10" and "0" wit= h no context:
> =C2=A0 =C2=A0 def draw(self):
> =C2=A0 =C2=A0 =C2=A0= =C2=A0 pygame.draw.circle(screen, white, self.pos, radius=3D10, width=3D0)=
> Pygame-ists will know that "width" means border_w= idth, by now. Pygame isn't known for it's clean design ;).

But pygame, being brilliant (not) decided that it won&#= 39;t let you.

=C2=A0 =C2=A0 def rotate(self): # rotation method
=C2=A0 =C2=A0 =C2=A0 =C2=A0 sin =3D m.sin(self.rot) #calculationg sin and c= os
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cos =3D m.cos(self.rot)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 x_rot =3D int(self.pos[0]*cos-self.pos[1]*sin) = #mulpitplicating
vector to rotation matrix
=C2=A0 =C2=A0 =C2=A0 =C2=A0 y_rot =3D int(self.pos[0]*sin+self.pos[1]*cos)<= br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.pos[0] =3D x_rot #set new coordinates to a= point
=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.pos[1] =3D y_rot

=

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 us= e comments:

1) API usage, when docstrings aren't usable

2) When something funny or unexpected occurs in the code,= such as:

=C2=A0 =C2=A0 # Goes down to 0 (does not= include end-point)
=C2=A0 =C2=A0 for i in range(foo, -1, -1): ...

3) To explain large-scale methodologies and algorithms

Other than this, are you trying to obfuscate this line? HINT: ADD SP= ACES ;).


A big problem here (this solves your pro= blem) is your int(...) conversions. Do *not* store important information le= ss accurately than it deserves. This is one very important instance. Only c= onvert to int when you need to.

Another thing: "sin =3D m.sin(self.rot)"= ;??? Really? Write "sin_1deg =3D ..." instead, at least.
=C2=A0
a =3D Poly() #Some simple sample points giving rectangle
b =3D Poly()
c =3D Poly()
d =3D Poly()

b.pos =3D [0,100]
c.pos =3D [100,0]
d.pos =3D [0,0]

Use:

a =3D Poly()
b =3D Poly([0, 100])
c =3D Poly([10= 0, 0])
d =3D Poly([0, 0])

pygame.init()
size =3D [700,500]
screen =3D pygame.display.set_mode(size)
done =3D False
clock =3D pygame.time.Clock()
while done =3D=3D False:

"while no= t done"

Also, just use "while True"= and a "break". I know some C-people or what not think this is &q= uot;evil" or something, but they're wrong.

Personally, I actually like using:

=
while "rotating the squares":
=C2=A0 =C2=A0 ...

instead of "while True". It's no slowe= r and it's free documentation.
=C2=A0
=C2=A0 =C2=A0 for event in pygame.event.get():
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if event.type =3D=3D pygame.QUIT:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 done =3D True

=C2=A0 =C2=A0 a.rotate() #perform rotation
=C2=A0 =C2=A0 b.rotate()
=C2=A0 =C2=A0 c.rotate()
=C2=A0 =C2=A0 d.rotate()

=C2=A0 =C2=A0 screen.fill(black)

=C2=A0 =C2=A0 a.draw() #draw point
=C2=A0 =C2=A0 b.draw()
=C2=A0 =C2=A0 c.draw()
=C2=A0 =C2=A0 d.draw()
=C2=A0 =C2=A0 pygame.display.flip()
=C2=A0 =C2=A0 clock.tick(30)

pygame.quit()[/code]

You don't need= pygame.quit(). You *only* want pygame.quit() if you're quitting Pygame= *before* the program is finished.
--001a11c37360ddb54304e24871ba--