Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'class,': 0.07; 'float': 0.07; '[0,': 0.09; 'linear': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'translate': 0.10; 'python': 0.11; 'def': 0.12; 'windows': 0.15; 'biology': 0.16; 'code?': 0.16; 'coordinates': 0.16; 'fly': 0.16; 'introduces': 0.16; 'pygame,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'rotation': 0.16; 'rounding': 0.16; 'skip:[ 30': 0.16; 'weird': 0.16; 'student': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'result.': 0.19; 'header :User-Agent:1': 0.23; 'error': 0.23; 'color,': 0.24; 'math': 0.24; 'right.': 0.26; 'excel': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'points': 0.29; 'program,': 0.31; 'pos': 0.31; 'class': 0.32; 'probably': 0.32; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'display': 0.35; 'problem.': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'method': 0.36; 'wrong': 0.37; 'to:addr:python-list': 0.38; 'planning': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'even': 0.60; 'eventually': 0.60; 'color': 0.61; 'conversion': 0.61; 'new': 0.61; 'simple': 0.61; 'first': 0.61; 'subject:. ': 0.67; 'center.': 0.81; 'low': 0.83; 'all!': 0.84; 'calculations': 0.84; 'results,': 0.84; 'russia,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Beginner. 2d rotation gives unexpected results. Date: Tue, 23 Jul 2013 15:11:43 +0200 Organization: None References: <0a905ff1-199c-4900-81e6-d9b7bb63bb44@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bbd8.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374585095 news.xs4all.nl 15962 [2001:888:2000:d::a6]:53983 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51084 enmce@yandex.ru wrote: > This is my first post, nice to meet you all! Welcome! > 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? > 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 The conversion to int introduces a rounding error that accumulates over time. > 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 One way to keep the error low is to keep the float values in self.pos and do the rounding on the fly when you display the point: class Poly(): def __init__(self, color, pos, rot=m.radians(1)): self.color = color self.pos = pos self.rot = rot def draw(self): x, y = self.pos pygame.draw.circle(screen, self.color, [350+int(x), 250+int(y)], 10, 0) def rotate(self): sin = m.sin(self.rot) cos = m.cos(self.rot) x_rot = self.pos[0]*cos-self.pos[1]*sin y_rot = self.pos[0]*sin+self.pos[1]*cos self.pos = [x_rot, y_rot] a = Poly(white, [100, 100]) b = Poly(green, [0, 100]) c = Poly(blue, [100, 0]) d = Poly(red, [0, 0])