PyX — Example: drawing2/

0.3 KB
14.1 KB
2.2 KB
0.9 KB

Inserting transformed canvases into canvases

from math import sin, cos, radians
from pyx import *

angle = 10
factor = 1.0 / (cos(radians(angle)) + sin(radians(angle)))

cc = canvas.canvas()
cc.stroke(path.rect(-2, -2, 4, 4))

c = canvas.canvas()
for i in range(10):
    c.insert(cc, [trafo.rotate(i*angle), trafo.scale(factor**i)])


Similar to the drawing of paths onto canvases, it is also possible to insert one canvas into another. You may specify additional transformations which are applied to the canvas which is inserted.

The example code shows how a canvas cc containing a square can be inserted several times into another canvas c. The applied transformation first rotates the square around the origin and then performs a scaling. Note that not only the size of the square gets scaled, but everything that is drawn on the canvas, also the linewidth of the square.

In geometrical operations, it is often necessary to perform calculations. This is conveniently done by importing the mathematics capability of Python. In the example, we calculate the scaling factor from the rotation angle such that the corners of the rotated smaller square reside on the sides of the larger square.