update upload data
[PyX.git] / manual / path.tex
blob20fcfac4ac10d89c77534fee8fc484be5990bbec
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. Thus, \var{points_pt} must be a
282 sequence of 6-tuples.
283 \end{classdesc}
286 \subsection{Class \class{normpath}}
288 \label{path:normpath}
290 The \class{normpath} class is used internally for all non-trivial path
291 operations, i.e. the ones marked by a $\dagger$ in the description of
292 the \class{path} above. It represents a path as a list of subpaths, which are
293 instances of the class \class{normsubpath}. These \class{normsubpath}s
294 themselves consist of a list of \class{normsubpathitems} which are
295 either straight lines (\class{normline}) or B\'ezier curves
296 (\class{normcurve}).
298 A given path can easily be converted to the corresponding \class{normpath}
299 using the method with this name:
300 \begin{verbatim}
301 np = p.normpath()
302 \end{verbatim}
303 Additionally, you can specify the accuracy (in points) which is used
304 in all \class{normpath} calculations by means of the argument
305 \var{epsilon}, which defaults to to $10^{-5}$ points. This default
306 value can be changed using the module function \function{path.set}.
308 To construct a \class{normpath} from a list of \class{normsubpath}
309 instances, you pass them to the \class{normpath} constructor:
311 \begin{classdesc}{normpath}{normsubpaths=[]}
312 Construct a \class{normpath} consisting of \var{subnormpaths}, which
313 is a list of \class{subnormpath} instances.
314 \end{classdesc}
316 Instances of \class{normpath} offers all methods of regular
317 \class{path}s, which also have the same semantics. An exception are
318 the methods \method{append} and \method{extend}. While they allow for
319 adding of instances of \class{subnormpath} to the \class{normpath}
320 instance, they also keep the functionality of a regular path and allow
321 for regular path elements to be appended. The later are converted to
322 the proper normpath representation during addition.
324 In addition to the \class{path} methods, a \class{normpath} instance
325 also offers the following methods, which operate on the instance
326 itself, i.e., modify it in place.
328 \begin{methoddesc}{join}{other}
329 Join \var{other}, which has to be a \class{path} instance, to
330 the \class{normpath} instance.
331 \end{methoddesc}
333 \begin{methoddesc}{reverse}{}
334 Reverses the \class{normpath} instance.
335 \end{methoddesc}
337 \begin{methoddesc}{transform}{trafo}
338 Transforms the \class{normpath} instance according to the linear
339 transformation \var{trafo}.
340 \end{methoddesc}
342 Finally, we remark that the sum of a \class{normpath} and a \class{path}
343 always yields a \class{normpath}.
346 \subsection{Class \class{normsubpath}}
348 \begin{classdesc}{normsubpath}{normsubpathitems=[], closed=0,
349 epsilon=1e-5}
350 Construct a \class{normsubpath} consisting of
351 \var{normsubpathitems}, which is a list of \class{normsubpathitem}
352 instances. If \var{closed} is set, the \class{normsubpath} will be
353 closed, thereby appending a straight line segment from the first to
354 the last point, if it is not already present. All calculations with
355 the \class{normsubpath} are performed with an accuracy of \var{epsilon}.
356 \end{classdesc}
358 Most \class{normsubpath} methods behave like the ones of a
359 \class{path}.
361 Exceptions are:
363 \begin{methoddesc}{append}{anormsubpathitem}
364 Append the \var{anormsubpathitem} to the end of the
365 \class{normsubpath} instance. This is only possible if the
366 \class{normsubpath} is not closed, otherwise an exception is raised.
367 \end{methoddesc}
369 \begin{methoddesc}{extend}{normsubpathitems}
370 Extend the \class{normsubpath} instances by \var{normsubpathitems},
371 which has to be a list of \class{normsubpathitem} instances. This is
372 only possible if the \class{normsubpath} is not closed, otherwise an
373 exception is raised.
374 \end{methoddesc}
376 \begin{methoddesc}{close}{}
377 Close the \class{normsubpath} instance, thereby appending a straight
378 line segment from the first to the last point, if it is not already
379 present.
380 \end{methoddesc}
382 \subsection{Predefined paths}
384 \label{path:predefined}
386 For convenience, some oft-used paths are already predefined. All
387 of them are subclasses of the \class{path} class.
389 \begin{classdesc}{line}{x0, y0, x1, y1}
390 A straight line from the point (\var{x0}, \var{y0}) to the point (\var{x1}, \var{y1}).
391 \end{classdesc}
393 \begin{classdesc}{curve}{x0, y0, x1, y1, x2, y2, x3, y3}
394 A B\'ezier curve with
395 control points (\var{x0}, \var{y0}), $\dots$, (\var{x3}, \var{y3}).\
396 \end{classdesc}
398 \begin{classdesc}{rect}{x, y, w, h}
399 A closed rectangle with lower left point (\var{x}, \var{y}), width \var{w}, and
400 height \var{h}.
401 \end{classdesc}
403 \begin{classdesc}{circle}{x, y, r}
404 A closed circle with center (\var{x}, \var{y}) and radius \var{r}.
405 \end{classdesc}
407 %%% Local Variables:
408 %%% mode: latex
409 %%% TeX-master: "manual.tex"
410 %%% ispell-dictionary: "british"
411 %%% End: