monkey-patch for mathpazo bug
[PyX/mjg.git] / manual / path.tex
blobdb8a444f7482ca55b5e3859fff5267f73c189b15
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}{*pathels}
17 This class represents a PostScript like path consisting of the
18 path elements \var{pathels}.
20 All possible path elements are described in Sect.~\ref{path:pathel}.
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}{pathel}
32 Appends a \var{pathel} 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 values corresponding to the arc lengths
41 \var{lengths}.$^\dagger$
42 \end{methoddesc}
44 \begin{methoddesc}{at}{param=None, arclen=None}
45 Returns the coordinates (as 2-tuple) of the path point corresponding to the
46 parameter value \var{param} or, alternatively, the arc length
47 \var{arclen}. The parameter value \var{param} (\var{arclen}) has to be smaller
48 or equal to \method{self.range()} (\method{self.arclen()}),
49 otherwise an exception is raised. At discontinuities in the path,
50 the limit from below is returned.$^\dagger$
51 \end{methoddesc}
53 \begin{methoddesc}{bbox}{}
54 Returns the bounding box of the path. Note that this returned
55 bounding box may be too large, if the path contains any
56 \class{curveto} elements, since for these the control box, i.e., the
57 bounding box enclosing the control points of the B\'ezier curve is
58 returned.
59 \end{methoddesc}
61 \begin{methoddesc}{begin}{}
62 Returns the coordinates (as 2-tuple) of the first point of the path.$^\dagger$
63 \end{methoddesc}
65 \begin{methoddesc}{curvradius}{param=None, arclen=None}
66 Returns the curvature radius (or None if infinite) at parameter
67 param or, alternatively, arc length \var{arclen}. This is the
68 inverse of the curvature at this parameter Please note that this
69 radius can be negative or positive, depending on the sign of the
70 curvature.$^\dagger$
71 \end{methoddesc}
73 \begin{methoddesc}{end}{}
74 Returns the coordinates (as 2-tuple) of the end point of the path.$^\dagger$
75 \end{methoddesc}
77 \begin{methoddesc}{intersect}{opath}
78 Returns a tuple consisting of two lists of parameter values
79 corresponding to the intersection points of the path with the other
80 path \var{opath}, respectively.$^\dagger$
81 \end{methoddesc}
83 \begin{methoddesc}{joined}{opath}
84 Appends \var{opath} to the end of the path, thereby merging the last
85 sub path (which must not be closed) of the path with the first sub
86 path of \var{opath} and returns the resulting new path.$^\dagger$
87 \end{methoddesc}
89 \begin{methoddesc}{range}{}
90 Returns the maximal parameter value \var{param} that is allowed in the
91 path methods.
92 \end{methoddesc}
94 \begin{methoddesc}{reversed}{}
95 Returns the reversed path.$^\dagger$
96 \end{methoddesc}
98 \begin{methoddesc}{split}{params}
99 Splits the path at the parameters \var{params}, which have to be
100 sorted in ascending order, and returns a corresponding list of
101 \class{normpath} instances.$^\dagger$
102 \end{methoddesc}
104 \begin{methoddesc}{tangent}{param=None, arclen=None, length=None}
105 Return a \class{line} instance corresponding to the tangent vector
106 to the path at the parameter value \var{param} or, alternatively, the arc length
107 \var{arclen}. The parameter value \var{param} (\var{arclen}) has to be smaller
108 or equal to \method{self.range()} (\method{self.arclen()}),
109 otherwise an exception is raised. At discontinuities in the path,
110 the limit from below is returned. If \var{length} is not
111 \texttt{None}, the tangent vector will be scaled correspondingly.$^\dagger$
112 \end{methoddesc}
115 \begin{methoddesc}{trafo}{param=None, arclen=None}
116 Returns a trafo which maps a point $(0, 1)$ to the tangent vector to
117 the path at the parameter value \var{param} or, alternatively, the
118 arc length \var{arclen}. The parameter value \var{param} (\var{arclen}) has to
119 be smaller or equal to \method{self.range()}
120 (\method{self.arclen()}), otherwise an exception is raised. At
121 discontinuities in the path, the limit from below is returned.$^\dagger$
122 \end{methoddesc}
124 \begin{methoddesc}{transformed}{trafo}
125 Returns the path transformed according to the linear transformation
126 \var{trafo}. Here, \texttt{trafo} must be an instance of the
127 \class{trafo.trafo} class.$^\dagger$
128 \end{methoddesc}
130 Some notes on the above:
131 \begin{itemize}
132 \item The $\dagger$ denotes methods which require a prior
133 conversion of the path into a \class{normpath} instance. This is
134 done automatically, but if you need to call such methods often or
135 if you need to change the precision used for this conversion,
136 it is a good idea to manually perform the conversion.
137 \item Instead of using the \method{joined()} method, you can also join two
138 paths together with help of the \verb|<<| operator, for instance
139 \samp{p = p1 << p2}.
140 \item In the methods accepting both a parameter value \var{param} and
141 an arc length \var{arclen}, exactly one of these arguments has to
142 provided.
143 \end{itemize}
145 \subsection{Path elements}
147 \label{path:pathel}
149 The class \class{pathel} is the superclass of all PostScript path
150 construction primitives. It is never used directly, but only by
151 instantiating its subclasses, which correspond one by one to the
152 PostScript primitives.
154 Except for the path elements ending in \code{_pt}, all coordinates
155 passed to the path elements can be given as number (in which case they
156 are interpreted as user units with the currently set default type) or in
157 \PyX\ lengths.
159 The following operation move the current point and open a new sub path:
161 \begin{classdesc}{moveto}{x, y}
162 Path element which sets the current point to the absolute coordinates (\var{x},
163 \var{y}). This operation opens a new subpath.
164 \end{classdesc}
166 \begin{classdesc}{rmoveto}{dx, dy}
167 Path element which moves the current point by (\var{dx}, \var{dy}).
168 This operation opens a new subpath.
169 \end{classdesc}
171 Drawing a straight line can be accomplished using:
173 \begin{classdesc}{lineto}{x, y}
174 Path element which appends a straight line from the current point to the
175 point with absolute coordinates (\var{x}, \var{y}), which becomes
176 the new current point.
177 \end{classdesc}
179 \begin{classdesc}{rlineto}{dx, dy}
180 Path element which appends a straight line from the current point to the
181 a point with relative coordinates (\var{dx}, \var{dy}), which becomes
182 the new current point.
183 \end{classdesc}
185 For the construction of arc segments, the following three operations
186 are available:
188 \begin{classdesc}{arc}{x, y, r, angle1, angle2}
189 Path element which appends an arc segment in counterclockwise direction
190 with absolute coordinates (\var{x}, \var{y}) of the center and
191 radius \var{r} from \var{angle1} to \var{angle2} (in degrees).
192 If before the operation, the current point is defined, a straight line
193 is from the current point to the beginning of the arc segment is
194 prepended. Otherwise, a subpath, which thus is the first one in the
195 path, is opened. After the operation, the current point is at the end
196 of the arc segment.
197 \end{classdesc}
199 \begin{classdesc}{arcn}{x, y, r, angle1, angle2}
200 Path element which appends an arc segment in clockwise direction
201 with absolute coordinates (\var{x}, \var{y}) of the center and
202 radius \var{r} from \var{angle1} to \var{angle2} (in degrees).
203 If before the operation, the current point is defined, a straight line
204 is from the current point to the beginning of the arc segment is
205 prepended. Otherwise, a subpath, which thus is the first one in the
206 path, is opened. After the operation, the current point is at the end
207 of the arc segment.
208 \end{classdesc}
210 \begin{classdesc}{arct}{x1, y1, x2, y2, r}
211 Path element which appends an arc segment of radius \var{r}
212 connecting between (\var{x1}, \var{y1}) and (\var{x2}, \var{y2}).\\
213 \end{classdesc}
215 B\'ezier curves can be constructed using: \
217 \begin{classdesc}{curveto}{x1, y1, x2, y2, x3, y3}
218 Path element which appends a B\'ezier curve with
219 the current point as first control point and the other control points
220 (\var{x1}, \var{y1}), (\var{x2}, \var{y2}), and (\var{x3}, \var{y3}).
221 \end{classdesc}
223 \begin{classdesc}{rcurveto}{dx1, dy1, dx2, dy2, dx3, dy3}
224 Path element which appends a B\'ezier curve with
225 the current point as first control point and the other control points
226 defined relative to the current point by the coordinates
227 (\var{dx1}, \var{dy1}), (\var{dx2}, \var{dy2}), and (\var{dx3}, \var{dy3}).
228 \end{classdesc}
230 Note that when calculating the bounding box (see Sect.~\ref{bbox}) of
231 B\'ezier curves, \PyX{} uses for performance reasons the so-called
232 control box, i.e., the smallest rectangle enclosing the four control
233 points of the B\'ezier curve. In general, this is not the smallest
234 rectangle enclosing the B\'ezier curve.
236 Finally, an open sub path can be closed using:
238 \begin{classdesc}{closepath}{}
239 Path element which closes the current subpath.
240 \end{classdesc}
242 For performance reasons, two non-PostScript path elements are defined,
243 which perform multiple identical operations:
245 \begin{classdesc}{multilineto_pt}{points}
246 Path element which appends straight line segments starting from
247 the current point and going through the list of points given
248 in the \var{points} argument. All coordinates have to
249 be given in PostScript points.
250 \end{classdesc}
252 \begin{classdesc}{multicurveto_pt}{points}
253 Path element which appends B\'ezier curve segments starting from
254 the current point and going through the list of each three control
255 points given in the \var{points} argument.
256 \end{classdesc}
259 \subsection{Class \class{normpath}}
261 \label{path:normpath}
263 The \class{normpath} class represents a specialized form of a
264 \class{path} containing only the elements \class{moveto},
265 \class{lineto}, \class{curveto} and \class{closepath}. Such normalized
266 paths are used for all of the more sophisticated path operations
267 which are denoted by a $\dagger$ in the description of the \class{path}
268 class above.
270 Any path can easily be converted to its normalized form by passing it
271 as parameter to the \class{normpath} constructor,
272 \begin{verbatim}
273 np = normpath(p)
274 \end{verbatim}
275 Additionally, you can specify the accuracy (in points) which is used
276 in all \class{normpath} calculations by means of the keyword argument
277 \var{epsilon}, which defaults to $10^{-5}$. Note that the sum of a
278 \class{normpath} and a \class{path} always yields a \class{normpath}.
280 \begin{classdesc}{normpath}{arg=[], epsilon=1e-5}
281 Construct a normpath from \var{arg}. All numerical calculations will
282 be performed using an accuracy of the order of \var{epsilon} points.
283 The first argument \var{arg} can be a \class{path} or a another
284 \class{normpath} instance. Alternatively, a list of
285 \class{normsubpath} instances can be supplied as argument.
286 \end{classdesc}
288 In addition to the \class{path} methods, a \class{normpath} instance
289 also offers the following methods, which operate on the instance itself:
291 \begin{methoddesc}{join}{other}
292 Join \var{other}, which has to be a \class{path} instance, to
293 the \class{normpath} instance.
294 \end{methoddesc}
296 \begin{methoddesc}{reverse}{}
297 Reverses the \class{normpath} instance.
298 \end{methoddesc}
300 \begin{methoddesc}{transform}{trafo}
301 Transforms the \class{normpath} instance according to the linear
302 transformation \var{trafo}.
303 \end{methoddesc}
305 \subsection{Predefined paths}
307 \label{path:predefined}
309 For your convenience, some oft-used paths are already pre-defined. All
310 of them are sub classes of the \class{path} class.
312 \begin{classdesc}{line}{x1, y1, x2, y2, x3, y3}
313 A straight line from the point (\var{x1}, \var{y1}) to the point (\var{x2}, \var{y2}).
314 \end{classdesc}
316 \begin{classdesc}{curve}{x1, y1, x2, y2, x3, y3, x4, y4}
317 A B\'ezier curve with
318 control points (\var{x0}, \var{y0}), $\dots$, (\var{x3}, \var{y3}).\
319 \end{classdesc}
321 \begin{classdesc}{rect}{x, y, w, h}
322 A closed rectangle with lower left point (\var{x}, \var{y}), width \var{w}, and
323 height \var{h}.
324 \end{classdesc}
326 \begin{classdesc}{circle}{x, y, r}
327 A closed circle with center (\var{x}, \var{y}) and radius \var{r}.
328 \end{classdesc}
330 %%% Local Variables:
331 %%% mode: latex
332 %%% TeX-master: "manual.tex"
333 %%% ispell-dictionary: "british"
334 %%% End: