1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
\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}{*pathitems}
This class represents a PostScript like path consisting of the
path elements \var{pathitems}.
All possible path items are described in Sect.~\ref{path:pathitem}.
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}{pathitem}
Appends a \var{pathitem} 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 value(s) corresponding to the arc length(s)
\var{lengths}.$^\dagger$
\end{methoddesc}
\begin{methoddesc}{at}{params}
Returns the coordinates (as 2-tuple) of the path point(s)
corresponding to the parameter value(s) \var{params}.$^\ddagger$
$^\dagger$
\end{methoddesc}
\begin{methoddesc}{atbegin}{}
Returns the coordinates (as 2-tuple) of the first point of the path.$^\dagger$
\end{methoddesc}
\begin{methoddesc}{atend}{}
Returns the coordinates (as 2-tuple) of the end point of the path.$^\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 parameter value (a \class{normpathparam} instance) of
the first point in the path.
\end{methoddesc}
\begin{methoddesc}{curveradius}{param=None, arclen=None}
Returns the curvature radius/radii (or None if infinite) at parameter
value(s) params.$^\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.$^\dagger$
\end{methoddesc}
\begin{methoddesc}{end}{}
Returns the parameter value (a \class{normpathparam} instance) of
the last point in the path.
\end{methoddesc}
\begin{methoddesc}{extend}{pathitems}
Appends the list \var{pathitems} to the end of the path.
\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$ For intersection points which
are not farther apart then \var{epsilon} points, only one is returned.
\end{methoddesc}
\begin{methoddesc}{joined}{opath}
Appends \var{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 \var{opath} and returns the resulting new path.$^\dagger$
\end{methoddesc}
\begin{methoddesc}{normpath}{epsilon=None}
Returns the equivalent \class{normpath}. For the conversion and for later
calculations with this \class{normpath} and accuracy of
\var{epsilon} points is used. If \var{epsilon} is \var{None}, the
global \var{epsilon} of the \module{path} module is used.
\end{methoddesc}
\begin{methoddesc}{paramtoarclen}{params}
Returns the arc length(s) corresponding to the parameter value(s)
\var{params}.$^\ddagger$ $^\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}{rotation}{params}
Returns (a) rotations(s) which (each), which rotate the x-direction
to the tangent and the y-direction to the normal at that param.$^\dagger$
\end{methoddesc}
\begin{methoddesc}{split}{params}
Splits the path at the parameter values \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}{params, length=1}
Return (a) \class{line} instance(s) corresponding to the tangent
vector(s) to the path at the parameter value(s)
\var{params}.$^\ddagger$ The tangent vector will be scaled to the length
\var{length}.$^\dagger$
\end{methoddesc}
\begin{methoddesc}{trafo}{params}
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.$^\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 (using the precision \var{epsilon} set globally
using \method{path.set}). If you need a different \var{epsilon} for
a normpath, you also can perform the conversion manually.
\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 $^\ddagger$ In these methods, \var{params} may either be a
single value or a list. In the latter case, the result of the method
will be a list consisting of the results for every parameter. The
parameter itself may either be a length (or a number which is then
interpreted as a user length) or an instance of the class
\class{normpathparam}. In the former case, the length refers to the
arc length along the path.
\end{itemize}
\subsection{Path elements}
\label{path:pathitem}
The class \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 \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 subpath:
\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 subpath 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_pt}
Path element which appends straight line segments starting from
the current point and going through the list of points given
in the \var{points_pt} argument. All coordinates have to
be given in PostScript points.
\end{classdesc}
\begin{classdesc}{multicurveto_pt}{points_pt}
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_pt} argument.
\end{classdesc}
\subsection{Class \class{normpath}}
\label{path:normpath}
The \class{normpath} class is used internally for all non-trivial path
operations, i.e. the ones marked by a $\dagger$ in the description of
the \class{path} above. It represents a path as a list of subpaths, which are
instances of the class \class{normsubpath}. These \class{normsubpath}s
themselves consist of a list of \class{normsubpathitems} which are
either straight lines (\class{normline}) or B\'ezier curves
(\class{normcurve}).
A given path can easily be converted to the corresponding \class{normpath}
using the method with this name:
\begin{verbatim}
np = p.normpath()
\end{verbatim}
Additionally, you can specify the accuracy (in points) which is used
in all \class{normpath} calculations by means of the argument
\var{epsilon}, which defaults to to $10^{-5}$ points. This default
value can be changed using the module function \function{path.set}.
To construct a \class{normpath} from a list of \class{normsubpath}
instances, you pass them to the \class{normpath} constructor:
\begin{classdesc}{normpath}{normsubpaths=[]}
Construct a \class{normpath} consisting of \var{subnormpaths}, which
is a list of \class{subnormpath} instances.
\end{classdesc}
Instances of \class{normpath} offers all methods of regular
\class{path}s, which also have the same semantics. An exception are
the methods \method{append} and \method{extend}. While they allow for
adding of instances of \class{subnormpath} to the \class{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 \class{path} methods, a \class{normpath} instance
also offers the following methods, which operate on the instance
itself, i.e., modify it in place.
\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}
Finally, we remark that the sum of a \class{normpath} and a \class{path}
always yields a \class{normpath}.
\subsection{Class \class{normsubpath}}
\begin{classdesc}{normsubpath}{normsubpathitems=[], closed=0,
epsilon=1e-5}
Construct a \class{normsubpath} consisting of
\var{normsubpathitems}, which is a list of \class{normsubpathitem}
instances. If \var{closed} is set, the \class{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 \class{normsubpath} are performed with an accuracy of \var{epsilon}.
\end{classdesc}
Most \class{normsubpath} methods behave like the ones of a
\class{path}.
Exceptions are:
\begin{methoddesc}{append}{anormsubpathitem}
Append the \var{anormsubpathitem} to the end of the
\class{normsubpath} instance. This is only possible if the
\class{normsubpath} is not closed, otherwise an exception is raised.
\end{methoddesc}
\begin{methoddesc}{extend}{normsubpathitems}
Extend the \class{normsubpath} instances by \var{normsubpathitems},
which has to be a list of \class{normsubpathitem} instances. This is
only possible if the \class{normsubpath} is not closed, otherwise an
exception is raised.
\end{methoddesc}
\begin{methoddesc}{close}{}
Close the \class{normsubpath} instance, thereby appending a straight
line segment from the first to the last point, if it is not already
present.
\end{methoddesc}
\subsection{Predefined paths}
\label{path:predefined}
For convenience, some oft-used paths are already predefined. All
of them are subclasses of the \class{path} class.
\begin{classdesc}{line}{x0, y0, x1, y1}
A straight line from the point (\var{x0}, \var{y0}) to the point (\var{x1}, \var{y1}).
\end{classdesc}
\begin{classdesc}{curve}{x0, y0, x1, y1, x2, y2, x3, y3}
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:
|