## [PyX-devel] Another geometry version

 [PyX-devel] Another geometry version From: Magnus Lie Hetland - 2004-07-27 17:52:28 Attachments: geom.py ```Luckily, I couldn't quite stop playing around with this... My first thought was to rewrite everything, writing custom stuff to integrate the linear equation solving stuff and my lazy points. I guess that is still an option, but I decided to do a very simple version instead. So, here is a version which uses Andr=E9's linear equation stuff (although I've switched to numarray -- it's what I use; and numarray 1.0 is out now, so... Hooray for that :). A point is simply a list of variables. By using ordinary list notation, such as p[0] one gets at these variables (their values are available through p[0].value and so forth) but by using the syntactic sugar p.x (or p.y or p.z) one gets (or sets) their values. For general dimensions beyond the third, use p.get(dim) or p.set(dim, val) (or, equivalently, p[dim].value and p[dim].value =3D val). The lsys class is a simple wrapper that handles left and right hand sides with more than one element (for multidimensional equations). There is precious little one can do directly with points at the moment (such as addition/multiplication) but adding that should be easy. I've added a single transformation as an example: """ def rotated(point, a): assert len(point) =3D=3D 2 # To keep it simple :) return (point[0]*cos(a)-point[1]*sin(a), point[1]*cos(a)+point[0]*sin(a)) """ Note that this works with the *variables*, not their *values*. Thus, one can use this transform either way in an equation: """ from geom import * from math import pi a =3D pt() b =3D pt() c =3D pt() eqs =3D lsys() a.x =3D 0 a.y =3D 10 eqs.eq(a, rotated(b, pi/2)) eqs.eq(c, rotated(a, pi/2)) eqs.solve() print b.x, b.y # Prints out 10.0 0.0 print c.x, c.y # Prints out -10.0 0.0 """ So, presto, we've got a bidirectional thingy. Note that the angle in the transform is still a constant, though. As Andr=E9 pointed out, if the angle is to be a variable, we'd end up getting equations with sin() and cos(), and that's not exactly pleasant. So, this code is still sort of hackish, but it's a starting point that actually works... And that's always good, isn't it? :) --=20 Magnus Lie Hetland "Canned Bread: The greatest thing since sliced http://hetland.org bread!" [from a can in Spongebob Squarepants] ```