Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51168
| 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 | <joshua.landau.ws@gmail.com> |
| 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 <joshua@landau.ws> |
| 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 <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5066.1374700684.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On 23 July 2013 13:34, <enmce@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
>
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Beginner. 2d rotation gives unexpected results. enmce@yandex.ru - 2013-07-23 05:34 -0700
Re: Beginner. 2d rotation gives unexpected results. Peter Otten <__peter__@web.de> - 2013-07-23 15:11 +0200
Re: Beginner. 2d rotation gives unexpected results. Nobody <nobody@nowhere.com> - 2013-07-23 16:20 +0100
Re: Beginner. 2d rotation gives unexpected results. David Hutto <dwightdhutto@gmail.com> - 2013-07-23 09:04 -0400
Re: Beginner. 2d rotation gives unexpected results. Joshua Landau <joshua@landau.ws> - 2013-07-24 22:17 +0100
Re: Beginner. 2d rotation gives unexpected results. Terry Reedy <tjreedy@udel.edu> - 2013-07-24 19:55 -0400
csiph-web