Section author: Jörg Lehmann <joergl@users.sourceforge.net>
The path module defines several important classes which are documented in the present section.
This class represents a PostScript like path consisting of the path elements pathitems.
All possible path items are described in Sect. Path elements. Note that there are restrictions on the first path element and likewise on each path element after a closepath directive. In both cases, no current point is defined and the path element has to be an instance of one of the following classes: moveto, arc, and arcn.
Instances of the class path provide the following methods (in alphabetic order):
Appends a pathitem to the end of the path.
Returns the total arc length of the path.:math:`^\dagger`
Returns the parameter value(s) corresponding to the arc length(s) lengths.:math:`^\dagger`
Returns the coordinates (as 2-tuple) of the path point(s) corresponding to the parameter value(s) params.:math:`^\ddagger` :math:`^\dagger`
Returns the coordinates (as 2-tuple) of the first point of the path.:math:`^\dagger`
Returns the coordinates (as 2-tuple) of the end point of the path.:math:`^\dagger`
Returns the bounding box of the path. Note that this returned bounding box may be too large, if the path contains any curveto elements, since for these the control box, i.e., the bounding box enclosing the control points of the Bézier curve is returned.
Returns the parameter value (a normpathparam instance) of the first point in the path.
Returns the curvature radius/radii (or None if infinite) at parameter value(s) params.:math:`^\ddagger` This is the inverse of the curvature at this parameter. Note that this radius can be negative or positive, depending on the sign of the curvature.:math:`^\dagger`
Returns the parameter value (a normpathparam instance) of the last point in the path.
Appends the list pathitems to the end of the path.
Returns a tuple consisting of two lists of parameter values corresponding to the intersection points of the path with the other path opath, respectively.:math:`^\dagger` For intersection points which are not farther apart then epsilon points, only one is returned.
Appends opath to the end of the path, thereby merging the last subpath (which must not be closed) of the path with the first sub path of opath and returns the resulting new path.:math:`^\dagger`
Returns the equivalent normpath. For the conversion and for later calculations with this normpath and accuracy of epsilon points is used. If epsilon is None, the global epsilon of the path module is used.
Returns the arc length(s) corresponding to the parameter value(s) params.:math:`^\ddagger` :math:`^\dagger`
Returns the maximal parameter value param that is allowed in the path methods.
Returns the reversed path.:math:`^\dagger`
Returns (a) rotations(s) which (each), which rotate the x-direction to the tangent and the y-direction to the normal at that param.:math:`^\dagger`
Splits the path at the parameter values params, which have to be sorted in ascending order, and returns a corresponding list of normpath instances.:math:`^\dagger`
Return (a) line instance(s) corresponding to the tangent vector(s) to the path at the parameter value(s) params.:math:`^\ddagger` The tangent vector will be scaled to the length length.:math:`^\dagger`
Returns (a) trafo(s) which (each) translate to a point on the path corresponding to the param, rotate the x-direction to the tangent and the y-direction to the normal in that point.:math:`^\dagger`
Returns the path transformed according to the linear transformation trafo. Here, trafo must be an instance of the trafo.trafo class.:math:`^\dagger`
Some notes on the above:
The class pathitem is the superclass of all PostScript path construction primitives. It is never used directly, but only by instantiating its subclasses, which correspond one by one to the PostScript primitives.
Except for the path elements ending in _pt, all coordinates passed to the path elements can be given as number (in which case they are interpreted as user units with the currently set default type) or in PyX lengths.
The following operation move the current point and open a new subpath:
Path element which sets the current point to the absolute coordinates (x, y). This operation opens a new subpath.
Path element which moves the current point by (dx, dy). This operation opens a new subpath.
Drawing a straight line can be accomplished using:
Path element which appends a straight line from the current point to the point with absolute coordinates (x, y), which becomes the new current point.
Path element which appends a straight line from the current point to the a point with relative coordinates (dx, dy), which becomes the new current point.
For the construction of arc segments, the following three operations are available:
Path element which appends an arc segment in counterclockwise direction with absolute coordinates (x, y) of the center and radius r from angle1 to angle2 (in degrees). If before the operation, the current point is defined, a straight line is from the current point to the beginning of the arc segment is prepended. Otherwise, a subpath, which thus is the first one in the path, is opened. After the operation, the current point is at the end of the arc segment.
Path element which appends an arc segment in clockwise direction with absolute coordinates (x, y) of the center and radius r from angle1 to angle2 (in degrees). If before the operation, the current point is defined, a straight line is from the current point to the beginning of the arc segment is prepended. Otherwise, a subpath, which thus is the first one in the path, is opened. After the operation, the current point is at the end of the arc segment.
Path element which appends an arc segment of radius r connecting between (x1, y1) and (x2, y2). —
Bézier curves can be constructed using:
Path element which appends a Bézier curve with the current point as first control point and the other control points (x1, y1), (x2, y2), and (x3, y3).
Path element which appends a Bézier curve with the current point as first control point and the other control points defined relative to the current point by the coordinates (dx1, dy1), (dx2, dy2), and (dx3, dy3).
Note that when calculating the bounding box (see Sect. bbox) of Bézier curves, PyX uses for performance reasons the so-called control box, i.e., the smallest rectangle enclosing the four control points of the Bézier curve. In general, this is not the smallest rectangle enclosing the Bézier curve.
Finally, an open subpath can be closed using:
Path element which closes the current subpath.
For performance reasons, two non-PostScript path elements are defined, which perform multiple identical operations:
Path element which appends straight line segments starting from the current point and going through the list of points given in the points_pt argument. All coordinates have to be given in PostScript points.
Path element which appends Bézier curve segments starting from the current point and going through the list of each three control points given in the points_pt argument. Thus, points_pt must be a sequence of 6-tuples.
The normpath class is used internally for all non-trivial path operations, i.e. the ones marked by a :math:`\dagger` in the description of the path above. It represents a path as a list of subpaths, which are instances of the class normsubpath. These normsubpaths themselves consist of a list of normsubpathitems which are either straight lines (normline) or Bézier curves (normcurve).
A given path can easily be converted to the corresponding normpath using the method with this name:
np = p.normpath()
Additionally, you can specify the accuracy (in points) which is used in all normpath calculations by means of the argument epsilon, which defaults to to :math:`10^{-5}` points. This default value can be changed using the module function path.set().
To construct a normpath from a list of normsubpath instances, you pass them to the normpath constructor:
Construct a normpath consisting of subnormpaths, which is a list of subnormpath instances.
Instances of normpath offers all methods of regular paths, which also have the same semantics. An exception are the methods append() and extend(). While they allow for adding of instances of subnormpath to the normpath instance, they also keep the functionality of a regular path and allow for regular path elements to be appended. The later are converted to the proper normpath representation during addition.
In addition to the path methods, a normpath instance also offers the following methods, which operate on the instance itself, i.e., modify it in place.
Transforms the normpath instance according to the linear transformation trafo.
Finally, we remark that the sum of a normpath and a path always yields a normpath.
Construct a normsubpath consisting of normsubpathitems, which is a list of normsubpathitem instances. If closed is set, the normsubpath will be closed, thereby appending a straight line segment from the first to the last point, if it is not already present. All calculations with the normsubpath are performed with an accuracy of epsilon.
Most normsubpath methods behave like the ones of a path.
Exceptions are:
Append the anormsubpathitem to the end of the normsubpath instance. This is only possible if the normsubpath is not closed, otherwise an exception is raised.
Extend the normsubpath instances by normsubpathitems, which has to be a list of normsubpathitem instances. This is only possible if the normsubpath is not closed, otherwise an exception is raised.
Close the normsubpath instance, thereby appending a straight line segment from the first to the last point, if it is not already present.
For convenience, some oft-used paths are already predefined. All of them are subclasses of the path class.
A straight line from the point (x0, y0) to the point (x1, y1).
A Bézier curve with control points (x0, y0), :math:`\dots`, (x3, y3).
A closed rectangle with lower left point (x, y), width w, and height h.
A closed circle with center (x, y) and radius r.