2D vector graphics Problem

K

Karl Max

Hi all,

I'm new here. Name's Max from tuscany, and I don't speak english very well
:) I am not veteran at coding, I am most like an "artisan coder" since
commodore 64 times.

Now the problem:
I would like to have a little 2d engine to calculate and trace segments and
poly's and their collisions, rotations and trasformations. I didn't
understand too much in tutorials (you're all high school coders, well
informed in maths and programming theory!!! :))) so I took my old school
books on Cartesian space and had a refresh. Proceeding in this not too
optimized way of coding my needs, I took down some generic classes to
describe primitive objects in space (vertexes, segments, poly's etc...).

Now I try to make a point rotate around another point.
Here's my piece of code:

def rotate(self, w):
# This method belongs to the class Vertex
# w = angle expressed in radiants
x, y = self.coords
xRel, yRel = self.relPoint # The rotation should be relative to this
sin, cos = math.sin(w), math.cos(w)
x = x * cos - y * sin - xRel * cos + yRel * sin + xRel
y = x * sin + y * cos - xRel * sin - yRel * cos + yRel
self.coords = (x,y)

I know the style isn't professional, and if you should have some piece of
advice, you're welcome... but that's not the question.
When I render it graphically, using pygame 1.6, the point tends to fall
towards the center of rotation round after round. I mean if I have a loop
rotating the point by a fixed angle, the point is like "attracted" by the
center every round it does, like in a vortex.
I think it depends from float representation inside python, but I have no
clear idea about how to resolve it. Maybe should I use Decimal module? Or is
there any trick on this subject (2D vector graphics?) that I can't even
imagine?
Thanks you all,
Max
 
F

F. GEIGER

xRel, yRel = self.relPoint # The rotation should be relative to this

Shouldn't it be

x -= xRel
y -= yRel
xR = x * cos(phi) - y * sin(phi)
yR = x * sin(phi) + y * cos(phi)

then?

Regards
Franz GEIGER
 
S

Scott David Daniels

Karl said:
def rotate(self, w):
# This method belongs to the class Vertex
# w = angle expressed in radiants
x, y = self.coords
xRel, yRel = self.relPoint # The rotation should be relative to this
sin, cos = math.sin(w), math.cos(w)
x = x * cos - y * sin - xRel * cos + yRel * sin + xRel
y = x * sin + y * cos - xRel * sin - yRel * cos + yRel
self.coords = (x,y)

Your equation for y uses the new x, not the old x. Be more free with
names. Here's one way to write it:

class ...
def rotate(self, angle):
'''Rotate point angle radians around relPoint'''
x, y = self.coords
xRel, yRel = self.relPoint
sin, cos = math.sin(angle), math.cos(angle)
newx = x * cos - y * sin - xRel * cos + yRel * sin + xRel
newy = x * sin + y * cos - xRel * sin - yRel * cos + yRel
self.coords = newx, newy

If you define a testcase or two, you can catch things like this early.


test = Point(1, 1)
test.rotate(math.pi / 2)
x, y = test.coords
assert (x - -1) ** 2 + (y - 1) ** 2 < .00001


--Scott David Daniels
(e-mail address removed)
 
K

Karl Max

Scott David Daniels said:
Your equation for y uses the new x, not the old x.

De hi hi ho. I must sleep some more hours at night... ;-)
Be more free with names.

Well, I often report book formulas, and forget renaming variables to more
comprehensive language. Thank you for your help.

Max
 
S

Scott David Daniels

Karl said:
De hi hi ho. I must sleep some more hours at night... ;-)

Well, I often report book formulas, and forget renaming variables to more
comprehensive language. Thank you for your help.

I was trying to give you a couple of ways to avoid similar problems in
the future. The way to get better at programming is to:
Figure out why you made the mistake.
See if you can change the way you work to prevent such problems.
If you can't prevent the problems:
Try to make the problem rare.
Try to catch the problems early.

It's not so much that I want you to do it my way; I was just suggesting
a couple of ways to improve your own process.

--Scott David Daniels
(e-mail address removed)
 
F

Fredrik Lundh

Scott said:
Your equation for y uses the new x, not the old x. Be more free with
names. Here's one way to write it:

class ...
def rotate(self, angle):
'''Rotate point angle radians around relPoint'''
x, y = self.coords
xRel, yRel = self.relPoint
sin, cos = math.sin(angle), math.cos(angle)
newx = x * cos - y * sin - xRel * cos + yRel * sin + xRel
newy = x * sin + y * cos - xRel * sin - yRel * cos + yRel
self.coords = newx, newy

and here's another one:

http://online.effbot.org/2004_09_01_archive.htm#tkinter-complex

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top