Re: 2D vector graphics Problem
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_archive.htm#tkinter-complex /F -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: 2D vector graphics Problem
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 .1 --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: 2D vector graphics Problem
Scott David Daniels [EMAIL PROTECTED] ha scritto nel messaggio news:[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: 2D vector graphics Problem
Karl Max wrote: Scott David Daniels [EMAIL PROTECTED] 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 [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list