make the process with processmethod local to the page (still getattr, but at least...
[PyX/mjg.git] / examples / drawing / ellipse.txt
blob6a3ebf9d088794ea75f11cd21ae11b985e1ba051
1 Applying transformations when drawing a path: Creating an ellipse
3 PyX does not directly provide a path corresponding to an ellipse. This example
4 shows how to draw an ellipse using affine transformations. ...
6 In order to create an ellipse, we best start from a unit circle centered around
7 the point of origin of the coordinate system. When stroking this circle on the
8 canvas, we tell PyX to apply a couple of affine transformations first. These
9 affine transformations are contained in the `trafo` module. We first use
10 `trafo.scale` to apply a non-uniform scaling, namely by a factor of 2 in
11 x-direction and a factor of 1.5 in y-direction. Doing so, we define the two
12 principle axes of the ellipse. In a next step, we rotate with `trafo.rotate`
13 the ellipse by an angle of 45 degrees in the mathematical positive direction,
14 i.e. counter-clockwise. Last, we shift the origin of the ellipse to the desired
15 point by applying a `trafo.translate` operation.
17 ! Note that the order of the transformations matters. If you, for instance, would
18 first translate the ellipse, the later scaling would also affect the distance
19 by which you have shifted the ellipse. PyX applies the transformations one after
20 the other, from left to right, so the example shown above does the correct thing.
22 ! You can also treat transformations as mathematical objects (they
23 are represented by two-dimensional matrices together with an offset vector) and 
24 multiply them using the `*` operator. Note, however, that mathematically, transformations
25 are applied from right to left, such that the above example would need to be written as
27     c.stroke(path.circle(0, 0, 1), [trafo.translate(1,0) * trafo.rotate(45) * trafo.scale(sx=2, sy=1.5)])
29 ! PyX also provides some convenience methods for applying certain
30 transformations with a given point as the origin. These allow one to write the
31 example in yet another form
33     c.stroke(path.circle(1, 0, 1), [trafo.scale(sx=2, sy=1.5, x=1, y=0), trafo.rotate(45, x=1, y=0)])
35 where we have started already from a circle centered around the desired point 1,0.