469,927 Members | 1,835 Online

2D vector graphics Problem

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
Jul 19 '05 #1
5 2571 > 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
"Karl Max" <ka*****@tiscalinet.it> schrieb im Newsbeitrag
news:42**********************@news.tiscali.it...
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

Jul 19 '05 #2
Karl Max wrote:
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
Sc***********@Acm.Org
Jul 19 '05 #3

"Scott David Daniels" <Sc***********@Acm.Org> ha scritto nel messaggio
news:42********@nntp0.pdx.net...
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.

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
Jul 19 '05 #4
Karl Max wrote:
"Scott David Daniels" <Sc***********@Acm.Org> ha scritto nel messaggio
... 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.

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
Sc***********@Acm.Org
Jul 19 '05 #5
Scott David Daniels wrote:
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_...kinter-complex

</F>

Jul 19 '05 #6

 10 posts views Thread by Andy C | last post: by 1 post views Thread by James-Hughes | last post: by 5 posts views Thread by Markus Elfring | last post: by 3 posts views Thread by Ska | last post: by reply views Thread by Eric | last post: by 1 post views Thread by juvi | last post: by 5 posts views Thread by Bytter | last post: by reply views Thread by =?Utf-8?B?QW5kcmV3?= | last post: by 30 posts views Thread by Xah Lee | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by lanliddd | last post: by reply views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by 9 posts views Thread by anoble1 | last post: by 1 post views Thread by skydivetom | last post: by reply views Thread by MikeCant | last post: by 1 post views Thread by MikeCant | last post: by 2 posts views Thread by Usman55 | last post: by