\section{Module \module{path}}

\sectionauthor{J\"org Lehmann}{joergl@users.sourceforge.net} 

\label{path}

The \module{path} module defines several important classes which are
documented in the present section.

\subsection{Class \class{path} --- PostScript-like paths}

\label{path:path}

\declaremodule{}{path}

\begin{classdesc}{path}{*pathels}
This class represents a PostScript like path consisting of the
path elements \var{pathels}.

All possible path elements are described in Sect.~\ref{path:pathel}.
Note that there are restrictions on the first path element and likewise
on each path element after a \class{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: \class{moveto}, \class{arc}, and
\class{arcn}.
\end{classdesc}

Instances of the class \class{path} provide the following
methods (in alphabetic order):

\begin{methoddesc}{append}{pathel}
Appends a \var{pathel} to the end of the path.
\end{methoddesc}

\begin{methoddesc}{arclen}{}
Returns the total arc length of the path.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{arclentoparam}{lengths}
  Returns the parameter values corresponding to the arc lengths
  \var{lengths}.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{at}{param=None, arclen=None}
  Returns the coordinates (as 2-tuple) of the path point corresponding to the
  parameter value \var{param} or, alternatively, the arc length
  \var{arclen}. The parameter value \var{param} (\var{arclen}) has to be smaller
  or equal to \method{self.range()} (\method{self.arclen()}),
  otherwise an exception is raised.  At discontinuities in the path,
  the limit from below is returned.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{bbox}{}
  Returns the bounding box of the path. Note that this returned
  bounding box may be too large, if the path contains any
  \class{curveto} elements, since for these the control box, i.e., the
  bounding box enclosing the control points of the B\'ezier curve is
  returned.
\end{methoddesc}

\begin{methoddesc}{begin}{}
  Returns the coordinates (as 2-tuple) of the first point of the path.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{curvradius}{param=None, arclen=None}
  Returns the curvature radius (or None if infinite) at parameter
  param or, alternatively, arc length \var{arclen}.  This is the
  inverse of the curvature at this parameter Please note that this
  radius can be negative or positive, depending on the sign of the
  curvature.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{end}{}
  Returns the coordinates (as 2-tuple) of the end point of the path.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{intersect}{opath}
  Returns a tuple consisting of two lists of parameter values
  corresponding to the intersection points of the path with the other
  path \var{opath}, respectively.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{joined}{opath}
  Appends \var{opath} to the end of the path, thereby merging the last
  sub path (which must not be closed) of the path with the first sub
  path of \var{opath} and returns the resulting new path.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{range}{}
  Returns the maximal parameter value \var{param} that is allowed in the
  path methods.
\end{methoddesc}

\begin{methoddesc}{reversed}{}
  Returns the reversed path.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{split}{params}
  Splits the path at the parameters \var{params}, which have to be
  sorted in ascending order, and returns a corresponding list of
  \class{normpath} instances.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{tangent}{param=None, arclen=None, length=None}
  Return a \class{line} instance corresponding to the tangent vector
  to the path at the parameter value \var{param} or, alternatively, the arc length
  \var{arclen}. The parameter value \var{param} (\var{arclen}) has to be smaller
  or equal to \method{self.range()} (\method{self.arclen()}),
  otherwise an exception is raised.  At discontinuities in the path,
  the limit from below is returned. If \var{length} is not
  \texttt{None}, the tangent vector will be scaled correspondingly.$^\dagger$
\end{methoddesc}


\begin{methoddesc}{trafo}{param=None, arclen=None}
  Returns a trafo which maps a point $(0, 1)$ to the tangent vector to
  the path at the parameter value \var{param} or, alternatively, the
  arc length \var{arclen}.  The parameter value \var{param} (\var{arclen}) has to
  be smaller or equal to \method{self.range()}
  (\method{self.arclen()}), otherwise an exception is raised.  At
  discontinuities in the path, the limit from below is returned.$^\dagger$
\end{methoddesc}

\begin{methoddesc}{transformed}{trafo}
  Returns the path transformed according to the linear transformation
  \var{trafo}. Here, \texttt{trafo} must be an instance of the
  \class{trafo.trafo} class.$^\dagger$
\end{methoddesc}

Some notes on the above:
\begin{itemize}
\item The $\dagger$ denotes methods which require a prior
  conversion of the path into a \class{normpath} instance. This is
  done automatically, but if you need to call such methods often or
  if you need to change the precision used for this conversion, 
  it is a good idea to manually perform the conversion.
\item Instead of using the \method{joined()} method, you can also join two
paths together with help of the \verb|<<| operator, for instance
\samp{p = p1 << p2}.
\item In the methods accepting both a parameter value \var{param} and
  an arc length \var{arclen}, exactly one of these arguments has to
  provided.
\end{itemize}

\subsection{Path elements}

\label{path:pathel}

The class \class{pathel} 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 \code{_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 sub path:

\begin{classdesc}{moveto}{x, y}
Path element which sets the current point to the absolute coordinates (\var{x},
\var{y}). This operation opens a new subpath.
\end{classdesc}

\begin{classdesc}{rmoveto}{dx, dy}
Path element which moves the current point by (\var{dx}, \var{dy}).
This operation opens a new subpath.
\end{classdesc}

Drawing a straight line can be accomplished using:

\begin{classdesc}{lineto}{x, y}
Path element which appends a straight line from the current point to the
point with absolute coordinates (\var{x}, \var{y}), which becomes
the new current point.
\end{classdesc}

\begin{classdesc}{rlineto}{dx, dy}
Path element which appends a straight line from the current point to the
a point with relative coordinates (\var{dx}, \var{dy}), which becomes
the new current point.
\end{classdesc}

For the construction of arc segments, the following three operations
are available:

\begin{classdesc}{arc}{x, y, r, angle1, angle2}
Path element which appends an arc segment in counterclockwise direction
with absolute coordinates (\var{x}, \var{y}) of the center and 
radius \var{r} from \var{angle1} to \var{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.
\end{classdesc}

\begin{classdesc}{arcn}{x, y, r, angle1, angle2}
Path element which appends an arc segment in clockwise direction
with absolute coordinates (\var{x}, \var{y}) of the center and 
radius \var{r} from \var{angle1} to \var{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.
\end{classdesc}

\begin{classdesc}{arct}{x1, y1, x2, y2, r}
Path element which appends an arc segment of radius \var{r}
connecting between (\var{x1}, \var{y1}) and (\var{x2}, \var{y2}).\\
\end{classdesc}

B\'ezier curves can be constructed using: \

\begin{classdesc}{curveto}{x1, y1, x2, y2, x3, y3}
Path element which appends a B\'ezier curve with
the current point as first control point and the other control points
(\var{x1}, \var{y1}), (\var{x2}, \var{y2}), and (\var{x3}, \var{y3}).
\end{classdesc}

\begin{classdesc}{rcurveto}{dx1, dy1, dx2, dy2, dx3, dy3}
Path element which appends a B\'ezier curve with
the current point as first control point and the other control points
defined relative to the current point by the coordinates
(\var{dx1}, \var{dy1}), (\var{dx2}, \var{dy2}), and (\var{dx3}, \var{dy3}).
\end{classdesc}

Note that when calculating the bounding box (see Sect.~\ref{bbox}) of
B\'ezier curves, \PyX{} uses for performance reasons the so-called
control box, i.e., the smallest rectangle enclosing the four control
points of the B\'ezier curve. In general, this is not the smallest
rectangle enclosing the B\'ezier curve.

Finally, an open sub path can be closed using:

\begin{classdesc}{closepath}{}
Path element which closes the current subpath.
\end{classdesc}

For performance reasons, two non-PostScript path elements are defined, 
which perform multiple identical operations:

\begin{classdesc}{multilineto_pt}{points}
Path element which appends straight line segments starting from
the current point and going through the list of points given 
in the \var{points} argument. All coordinates have to 
be given in PostScript points.
\end{classdesc}

\begin{classdesc}{multicurveto_pt}{points}
Path element which appends B\'ezier curve segments starting from
the current point and going through the list of each three control
points given in the \var{points} argument.
\end{classdesc}


\subsection{Class \class{normpath}}

\label{path:normpath}

The \class{normpath} class represents a specialized form of a
\class{path} containing only the elements \class{moveto},
\class{lineto}, \class{curveto} and \class{closepath}. Such normalized
paths are used for all of the more sophisticated path operations
which are denoted by a $\dagger$ in the description of the \class{path}
class above.

Any path can easily be converted to its normalized form by passing it
as parameter to the \class{normpath} constructor,
\begin{verbatim}
np = normpath(p)
\end{verbatim}
Additionally, you can specify the accuracy (in points) which is used
in all \class{normpath} calculations by means of the keyword argument
\var{epsilon}, which defaults to $10^{-5}$.  Note that the sum of a
\class{normpath} and a \class{path} always yields a \class{normpath}.

\begin{classdesc}{normpath}{arg=[], epsilon=1e-5}
  Construct a normpath from \var{arg}. All numerical calculations will
  be performed using an accuracy of the order of \var{epsilon} points.
  The first argument \var{arg} can be a \class{path} or a another
  \class{normpath} instance. Alternatively, a list of
  \class{normsubpath} instances can be supplied as argument.
\end{classdesc}

In addition to the \class{path} methods, a \class{normpath} instance
also offers the following methods, which operate on the instance itself:

\begin{methoddesc}{join}{other}
  Join \var{other}, which has to be a \class{path} instance, to
  the \class{normpath} instance.
\end{methoddesc}

\begin{methoddesc}{reverse}{}
  Reverses the \class{normpath} instance.
\end{methoddesc}

\begin{methoddesc}{transform}{trafo}
  Transforms the \class{normpath} instance according to the linear
  transformation \var{trafo}.
\end{methoddesc}

\subsection{Predefined paths}

\label{path:predefined}

For your convenience, some oft-used paths are already pre-defined. All
of them are sub classes of the \class{path} class.

\begin{classdesc}{line}{x1, y1, x2, y2, x3, y3}
A straight line from the point (\var{x1}, \var{y1}) to the point (\var{x2}, \var{y2}).
\end{classdesc}

\begin{classdesc}{curve}{x1, y1, x2, y2, x3, y3, x4, y4}
A B\'ezier curve with 
control points  (\var{x0}, \var{y0}), $\dots$, (\var{x3}, \var{y3}).\
\end{classdesc}

\begin{classdesc}{rect}{x, y, w, h}
A closed rectangle with lower left point (\var{x}, \var{y}), width \var{w}, and
  height \var{h}.
\end{classdesc}

\begin{classdesc}{circle}{x, y, r}
A closed circle with center (\var{x}, \var{y}) and radius \var{r}.
\end{classdesc}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "manual.tex"
%%% ispell-dictionary: "british"
%%% End:
