some updates to the webpage
[PyX.git] / manual / path.tex
blob776d050d339e9d26d2e2fe87255000677b3c9609
1 \section{Module \module{path}}
3 \sectionauthor{J\"org Lehmann}{joergl@users.sourceforge.net}
5 \label{path}
7 The \module{path} module defines several important classes which are
8 documented in the present section.
10 \subsection{Class \class{path} --- PostScript-like paths}
12 \label{path:path}
14 \declaremodule{}{path}
16 \begin{classdesc}{path}{*pathitems}
17 This class represents a PostScript like path consisting of the
18 path elements \var{pathitems}.
20 All possible path items are described in Sect.~\ref{path:pathitem}.
21 Note that there are restrictions on the first path element and likewise
22 on each path element after a \class{closepath} directive. In both cases,
23 no current point is defined and the path element has to be an instance
24 of one of the following classes: \class{moveto}, \class{arc}, and
25 \class{arcn}.
26 \end{classdesc}
28 Instances of the class \class{path} provide the following
29 methods (in alphabetic order):
31 \begin{methoddesc}{append}{pathitem}
32 Appends a \var{pathitem} to the end of the path.
33 \end{methoddesc}
35 \begin{methoddesc}{arclen}{}
36 Returns the total arc length of the path.$^\dagger$
37 \end{methoddesc}
39 \begin{methoddesc}{arclentoparam}{lengths}
40 Returns the parameter value(s) corresponding to the arc length(s)
41 \var{lengths}.$^\dagger$
42 \end{methoddesc}
44 \begin{methoddesc}{at}{params}
45 Returns the coordinates (as 2-tuple) of the path point(s)
46 corresponding to the parameter value(s) \var{params}.$^\ddagger$
47 $^\dagger$
48 \end{methoddesc}
50 \begin{methoddesc}{atbegin}{}
51 Returns the coordinates (as 2-tuple) of the first point of the path.$^\dagger$
52 \end{methoddesc}
54 \begin{methoddesc}{atend}{}
55 Returns the coordinates (as 2-tuple) of the end point of the path.$^\dagger$
56 \end{methoddesc}
58 \begin{methoddesc}{bbox}{}
59 Returns the bounding box of the path. Note that this returned
60 bounding box may be too large, if the path contains any
61 \class{curveto} elements, since for these the control box, i.e., the
62 bounding box enclosing the control points of the B\'ezier curve is
63 returned.
64 \end{methoddesc}
66 \begin{methoddesc}{begin}{}
67 Returns the parameter value (a \class{normpathparam} instance) of
68 the first point in the path.
69 \end{methoddesc}
71 \begin{methoddesc}{curveradius}{param=None, arclen=None}
72 Returns the curvature radius/radii (or None if infinite) at parameter
73 value(s) params.$^\ddagger$ This is the inverse of the curvature at
74 this parameter. Note that this radius can be negative or positive,
75 depending on the sign of the curvature.$^\dagger$
76 \end{methoddesc}
78 \begin{methoddesc}{end}{}
79 Returns the parameter value (a \class{normpathparam} instance) of
80 the last point in the path.
81 \end{methoddesc}
83 \begin{methoddesc}{extend}{pathitems}
84 Appends the list \var{pathitems} to the end of the path.
85 \end{methoddesc}
87 \begin{methoddesc}{intersect}{opath}
88 Returns a tuple consisting of two lists of parameter values
89 corresponding to the intersection points of the path with the other
90 path \var{opath}, respectively.$^\dagger$ For intersection points which
91 are not farther apart then \var{epsilon} points, only one is returned.
92 \end{methoddesc}
94 \begin{methoddesc}{joined}{opath}
95 Appends \var{opath} to the end of the path, thereby merging the last
96 subpath (which must not be closed) of the path with the first sub
97 path of \var{opath} and returns the resulting new path.$^\dagger$
98 \end{methoddesc}
100 \begin{methoddesc}{normpath}{epsilon=None}
101 Returns the equivalent \class{normpath}. For the conversion and for later
102 calculations with this \class{normpath} and accuracy of
103 \var{epsilon} points is used. If \var{epsilon} is \var{None}, the
104 global \var{epsilon} of the \module{path} module is used.
105 \end{methoddesc}
107 \begin{methoddesc}{paramtoarclen}{params}
108 Returns the arc length(s) corresponding to the parameter value(s)
109 \var{params}.$^\ddagger$ $^\dagger$
110 \end{methoddesc}
113 \begin{methoddesc}{range}{}
114 Returns the maximal parameter value \var{param} that is allowed in the
115 path methods.
116 \end{methoddesc}
118 \begin{methoddesc}{reversed}{}
119 Returns the reversed path.$^\dagger$
120 \end{methoddesc}
122 \begin{methoddesc}{rotation}{params}
123 Returns (a) rotations(s) which (each), which rotate the x-direction
124 to the tangent and the y-direction to the normal at that param.$^\dagger$
125 \end{methoddesc}
127 \begin{methoddesc}{split}{params}
128 Splits the path at the parameter values \var{params}, which have to
129 be sorted in ascending order, and returns a corresponding list of
130 \class{normpath} instances.$^\dagger$
131 \end{methoddesc}
133 \begin{methoddesc}{tangent}{params, length=1}
134 Return (a) \class{line} instance(s) corresponding to the tangent
135 vector(s) to the path at the parameter value(s)
136 \var{params}.$^\ddagger$ The tangent vector will be scaled to the length
137 \var{length}.$^\dagger$
138 \end{methoddesc}
140 \begin{methoddesc}{trafo}{params}
141 Returns (a) trafo(s) which (each) translate to a point on the path
142 corresponding to the param, rotate the x-direction to the tangent
143 and the y-direction to the normal in that point.$^\dagger$
144 \end{methoddesc}
146 \begin{methoddesc}{transformed}{trafo}
147 Returns the path transformed according to the linear transformation
148 \var{trafo}. Here, \texttt{trafo} must be an instance of the
149 \class{trafo.trafo} class.$^\dagger$
150 \end{methoddesc}
152 Some notes on the above:
153 \begin{itemize}
154 \item The $\dagger$ denotes methods which require a prior
155 conversion of the path into a \class{normpath} instance. This is
156 done automatically (using the precision \var{epsilon} set globally
157 using \method{path.set}). If you need a different \var{epsilon} for
158 a normpath, you also can perform the conversion manually.
159 \item Instead of using the \method{joined()} method, you can also join two
160 paths together with help of the \verb|<<| operator, for instance
161 \samp{p = p1 << p2}.
162 \item $^\ddagger$ In these methods, \var{params} may either be a
163 single value or a list. In the latter case, the result of the method
164 will be a list consisting of the results for every parameter. The
165 parameter itself may either be a length (or a number which is then
166 interpreted as a user length) or an instance of the class
167 \class{normpathparam}. In the former case, the length refers to the
168 arc length along the path.
169 \end{itemize}
171 \subsection{Path elements}
173 \label{path:pathitem}
175 The class \class{pathitem} is the superclass of all PostScript path
176 construction primitives. It is never used directly, but only by
177 instantiating its subclasses, which correspond one by one to the
178 PostScript primitives.
180 Except for the path elements ending in \code{_pt}, all coordinates
181 passed to the path elements can be given as number (in which case they
182 are interpreted as user units with the currently set default type) or in
183 \PyX\ lengths.
185 The following operation move the current point and open a new subpath:
187 \begin{classdesc}{moveto}{x, y}
188 Path element which sets the current point to the absolute coordinates (\var{x},
189 \var{y}). This operation opens a new subpath.
190 \end{classdesc}
192 \begin{classdesc}{rmoveto}{dx, dy}
193 Path element which moves the current point by (\var{dx}, \var{dy}).
194 This operation opens a new subpath.
195 \end{classdesc}
197 Drawing a straight line can be accomplished using:
199 \begin{classdesc}{lineto}{x, y}
200 Path element which appends a straight line from the current point to the
201 point with absolute coordinates (\var{x}, \var{y}), which becomes
202 the new current point.
203 \end{classdesc}
205 \begin{classdesc}{rlineto}{dx, dy}
206 Path element which appends a straight line from the current point to the
207 a point with relative coordinates (\var{dx}, \var{dy}), which becomes
208 the new current point.
209 \end{classdesc}
211 For the construction of arc segments, the following three operations
212 are available:
214 \begin{classdesc}{arc}{x, y, r, angle1, angle2}
215 Path element which appends an arc segment in counterclockwise direction
216 with absolute coordinates (\var{x}, \var{y}) of the center and
217 radius \var{r} from \var{angle1} to \var{angle2} (in degrees).
218 If before the operation, the current point is defined, a straight line
219 is from the current point to the beginning of the arc segment is
220 prepended. Otherwise, a subpath, which thus is the first one in the
221 path, is opened. After the operation, the current point is at the end
222 of the arc segment.
223 \end{classdesc}
225 \begin{classdesc}{arcn}{x, y, r, angle1, angle2}
226 Path element which appends an arc segment in clockwise direction
227 with absolute coordinates (\var{x}, \var{y}) of the center and
228 radius \var{r} from \var{angle1} to \var{angle2} (in degrees).
229 If before the operation, the current point is defined, a straight line
230 is from the current point to the beginning of the arc segment is
231 prepended. Otherwise, a subpath, which thus is the first one in the
232 path, is opened. After the operation, the current point is at the end
233 of the arc segment.
234 \end{classdesc}
236 \begin{classdesc}{arct}{x1, y1, x2, y2, r}
237 Path element which appends an arc segment of radius \var{r}
238 connecting between (\var{x1}, \var{y1}) and (\var{x2}, \var{y2}).\\
239 \end{classdesc}
241 B\'ezier curves can be constructed using: \
243 \begin{classdesc}{curveto}{x1, y1, x2, y2, x3, y3}
244 Path element which appends a B\'ezier curve with
245 the current point as first control point and the other control points
246 (\var{x1}, \var{y1}), (\var{x2}, \var{y2}), and (\var{x3}, \var{y3}).
247 \end{classdesc}
249 \begin{classdesc}{rcurveto}{dx1, dy1, dx2, dy2, dx3, dy3}
250 Path element which appends a B\'ezier curve with
251 the current point as first control point and the other control points
252 defined relative to the current point by the coordinates
253 (\var{dx1}, \var{dy1}), (\var{dx2}, \var{dy2}), and (\var{dx3}, \var{dy3}).
254 \end{classdesc}
256 Note that when calculating the bounding box (see Sect.~\ref{bbox}) of
257 B\'ezier curves, \PyX{} uses for performance reasons the so-called
258 control box, i.e., the smallest rectangle enclosing the four control
259 points of the B\'ezier curve. In general, this is not the smallest
260 rectangle enclosing the B\'ezier curve.
262 Finally, an open subpath can be closed using:
264 \begin{classdesc}{closepath}{}
265 Path element which closes the current subpath.
266 \end{classdesc}
268 For performance reasons, two non-PostScript path elements are defined,
269 which perform multiple identical operations:
271 \begin{classdesc}{multilineto_pt}{points_pt}
272 Path element which appends straight line segments starting from
273 the current point and going through the list of points given
274 in the \var{points_pt} argument. All coordinates have to
275 be given in PostScript points.
276 \end{classdesc}
278 \begin{classdesc}{multicurveto_pt}{points_pt}
279 Path element which appends B\'ezier curve segments starting from
280 the current point and going through the list of each three control
281 points given in the \var{points_pt} argument.
282 \end{classdesc}
285 \subsection{Class \class{normpath}}
287 \label{path:normpath}
289 The \class{normpath} class is used internally for all non-trivial path
290 operations, i.e. the ones marked by a $\dagger$ in the description of
291 the \class{path} above. It represents a path as a list of subpaths, which are
292 instances of the class \class{normsubpath}. These \class{normsubpath}s
293 themselves consist of a list of \class{normsubpathitems} which are
294 either straight lines (\class{normline}) or B\'ezier curves
295 (\class{normcurve}).
297 A given path can easily be converted to the corresponding \class{normpath}
298 using the method with this name:
299 \begin{verbatim}
300 np = p.normpath()
301 \end{verbatim}
302 Additionally, you can specify the accuracy (in points) which is used
303 in all \class{normpath} calculations by means of the argument
304 \var{epsilon}, which defaults to to $10^{-5}$ points. This default
305 value can be changed using the module function \function{path.set}.
307 To construct a \class{normpath} from a list of \class{normsubpath}
308 instances, you pass them to the \class{normpath} constructor:
310 \begin{classdesc}{normpath}{normsubpaths=[]}
311 Construct a \class{normpath} consisting of \var{subnormpaths}, which
312 is a list of \class{subnormpath} instances.
313 \end{classdesc}
315 Instances of \class{normpath} offers all methods of regular
316 \class{path}s, which also have the same semantics. An exception are
317 the methods \method{append} and \method{extend}. While they allow for
318 adding of instances of \class{subnormpath} to the \class{normpath}
319 instance, they also keep the functionality of a regular path and allow
320 for regular path elements to be appended. The later are converted to
321 the proper normpath representation during addition.
323 In addition to the \class{path} methods, a \class{normpath} instance
324 also offers the following methods, which operate on the instance
325 itself, i.e., modify it in place.
327 \begin{methoddesc}{join}{other}
328 Join \var{other}, which has to be a \class{path} instance, to
329 the \class{normpath} instance.
330 \end{methoddesc}
332 \begin{methoddesc}{reverse}{}
333 Reverses the \class{normpath} instance.
334 \end{methoddesc}
336 \begin{methoddesc}{transform}{trafo}
337 Transforms the \class{normpath} instance according to the linear
338 transformation \var{trafo}.
339 \end{methoddesc}
341 Finally, we remark that the sum of a \class{normpath} and a \class{path}
342 always yields a \class{normpath}.
345 \subsection{Class \class{normsubpath}}
347 \begin{classdesc}{normsubpath}{normsubpathitems=[], closed=0,
348 epsilon=1e-5}
349 Construct a \class{normsubpath} consisting of
350 \var{normsubpathitems}, which is a list of \class{normsubpathitem}
351 instances. If \var{closed} is set, the \class{normsubpath} will be
352 closed, thereby appending a straight line segment from the first to
353 the last point, if it is not already present. All calculations with
354 the \class{normsubpath} are performed with an accuracy of \var{epsilon}.
355 \end{classdesc}
357 Most \class{normsubpath} methods behave like the ones of a
358 \class{path}.
360 Exceptions are:
362 \begin{methoddesc}{append}{anormsubpathitem}
363 Append the \var{anormsubpathitem} to the end of the
364 \class{normsubpath} instance. This is only possible if the
365 \class{normsubpath} is not closed, otherwise an exception is raised.
366 \end{methoddesc}
368 \begin{methoddesc}{extend}{normsubpathitems}
369 Extend the \class{normsubpath} instances by \var{normsubpathitems},
370 which has to be a list of \class{normsubpathitem} instances. This is
371 only possible if the \class{normsubpath} is not closed, otherwise an
372 exception is raised.
373 \end{methoddesc}
375 \begin{methoddesc}{close}{}
376 Close the \class{normsubpath} instance, thereby appending a straight
377 line segment from the first to the last point, if it is not already
378 present.
379 \end{methoddesc}
381 \subsection{Predefined paths}
383 \label{path:predefined}
385 For convenience, some oft-used paths are already predefined. All
386 of them are subclasses of the \class{path} class.
388 \begin{classdesc}{line}{x0, y0, x1, y1}
389 A straight line from the point (\var{x0}, \var{y0}) to the point (\var{x1}, \var{y1}).
390 \end{classdesc}
392 \begin{classdesc}{curve}{x0, y0, x1, y1, x2, y2, x3, y3}
393 A B\'ezier curve with
394 control points (\var{x0}, \var{y0}), $\dots$, (\var{x3}, \var{y3}).\
395 \end{classdesc}
397 \begin{classdesc}{rect}{x, y, w, h}
398 A closed rectangle with lower left point (\var{x}, \var{y}), width \var{w}, and
399 height \var{h}.
400 \end{classdesc}
402 \begin{classdesc}{circle}{x, y, r}
403 A closed circle with center (\var{x}, \var{y}) and radius \var{r}.
404 \end{classdesc}
406 %%% Local Variables:
407 %%% mode: latex
408 %%% TeX-master: "manual.tex"
409 %%% ispell-dictionary: "british"
410 %%% End: