Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51084
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Beginner. 2d rotation gives unexpected results. |
| Date | 2013-07-23 15:11 +0200 |
| Organization | None |
| References | <0a905ff1-199c-4900-81e6-d9b7bb63bb44@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5004.1374585095.3114.python-list@python.org> (permalink) |
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])
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