473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2692
> 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andy C | last post by:
Where can I get a python package to draw such figures? I googled and found PyX, I guess it outputs PostScript. I guess I can get a PostScript to BMP converter or something. Is there any other...
1
by: James-Hughes | last post by:
I am currently working on a .Net assembly (C#) to dynamically generate an SVG in the form of a bar chart. I am having a problem drawing the legend based upon dynamic data being passed to my class....
5
by: Markus Elfring | last post by:
Hello, A lot of clipart libraries offer free raster images. I am looking for a similar collection for vector graphics. Do you know a gallery or archive for the following formats? - Scalable...
3
by: Ska | last post by:
I've just finished to write the 0.1b release of amanith ( www.amanith.org) a library to handle vector graphics fully written in ANSI C++ with STL, it's crossplatform (win, linux, bsd, mac, irix for...
0
by: Eric | last post by:
I'm also interested in finding out more about using vector graphics when designing the UI in Windows forms applications. I know a lot about GDI+ and raster graphics, but vector graphics is a new...
1
by: juvi | last post by:
Hi, is there a tool for Vector Graphics Maps creation? (maybe a free version?) If yes: Can I use the created map with Microsoft MapPoint? Would it be possible to create a map of a building and...
5
by: Bytter | last post by:
Hi ppl, I've already posted this message through the mailing-list, but it seems it never arrived here. Strange... Anyway: I need to render high-quality vector graphics with Python. I was...
0
by: =?Utf-8?B?QW5kcmV3?= | last post by:
I have a borderless form that has a transparent background. I only wanted the color that I painted the background with to be transparent and not the controls on the form or the box I draw in the...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.