test trafo application in draw
[PyX/mjg.git] / manual / path.tex
blob74bc38fe5542bc14247324428a023e67a897db17
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 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
46 to the parameter value \var{param} or, alternatively, the arc length
47 \var{arclen}.$^\ddagger$ At discontinuities in the path, the limit
48 from below is returned.$^\dagger$
49 \end{methoddesc}
51 \begin{methoddesc}{bbox}{}
52 Returns the bounding box of the path. Note that this returned
53 bounding box may be too large, if the path contains any
54 \class{curveto} elements, since for these the control box, i.e., the
55 bounding box enclosing the control points of the B\'ezier curve is
56 returned.
57 \end{methoddesc}
59 \begin{methoddesc}{begin}{}
60 Returns the coordinates (as 2-tuple) of the first point of the path.$^\dagger$
61 \end{methoddesc}
63 \begin{methoddesc}{curvradius}{param=None, arclen=None}
64 Returns the curvature radius (or None if infinite) at parameter
65 param or, alternatively, arc length \var{arclen}.$^\ddagger$ This is
66 the inverse of the curvature at this parameter. Note that this
67 radius can be negative or positive, depending on the sign of the
68 curvature.$^\dagger$
69 \end{methoddesc}
71 \begin{methoddesc}{end}{}
72 Returns the coordinates (as 2-tuple) of the end point of the path.$^\dagger$
73 \end{methoddesc}
75 \begin{methoddesc}{intersect}{opath}
76 Returns a tuple consisting of two lists of parameter values
77 corresponding to the intersection points of the path with the other
78 path \var{opath}, respectively.$^\dagger$ For intersection points which
79 are not farther apart then \var{epsilon} points, only one is returned.
80 \end{methoddesc}
82 \begin{methoddesc}{joined}{opath}
83 Appends \var{opath} to the end of the path, thereby merging the last
84 subpath (which must not be closed) of the path with the first sub
85 path of \var{opath} and returns the resulting new path.$^\dagger$
86 \end{methoddesc}
88 \begin{methoddesc}{normpath}{epsilon=None}
89 Returns the equivalent \class{normpath}. For the conversion and for later
90 calculations with this \class{normpath} and accuracy of
91 \var{epsilon} points is used. If \var{epsilon} is \var{None}, the
92 global \var{epsilon} of the \module{path} module is used.
93 \end{methoddesc}
95 \begin{methoddesc}{range}{}
96 Returns the maximal parameter value \var{param} that is allowed in the
97 path methods.
98 \end{methoddesc}
100 \begin{methoddesc}{reversed}{}
101 Returns the reversed path.$^\dagger$
102 \end{methoddesc}
104 \begin{methoddesc}{split}{params}
105 Splits the path at the parameters \var{params}, which have to be
106 sorted in ascending order, and returns a corresponding list of
107 \class{normpath} instances.$^\dagger$
108 \end{methoddesc}
110 \begin{methoddesc}{tangent}{param=None, arclen=None, length=None}
111 Return a \class{line} instance corresponding to the tangent vector
112 to the path at the parameter value \var{param} or, alternatively,
113 the arc length \var{arclen}.$^\ddagger$ At discontinuities in the
114 path, the limit from below is returned. If \var{length} is not
115 \texttt{None}, the tangent vector will be scaled
116 correspondingly.$^\dagger$
117 \end{methoddesc}
120 \begin{methoddesc}{trafo}{param=None, arclen=None}
121 Returns a trafo which maps a point $(0, 1)$ to the tangent vector to
122 the path at the parameter value \var{param} or, alternatively, the
123 arc length \var{arclen}.$^\ddagger$ At discontinuities in the path,
124 the limit from below is returned.$^\dagger$
125 \end{methoddesc}
127 \begin{methoddesc}{transformed}{trafo}
128 Returns the path transformed according to the linear transformation
129 \var{trafo}. Here, \texttt{trafo} must be an instance of the
130 \class{trafo.trafo} class.$^\dagger$
131 \end{methoddesc}
133 Some notes on the above:
134 \begin{itemize}
135 \item The $\dagger$ denotes methods which require a prior
136 conversion of the path into a \class{normpath} instance. This is
137 done automatically (using the precision \var{epsilon} set globally
138 using \method{path.set}), but if you need to call such methods often or
139 if you need to change the precision used for this conversion,
140 it is a good idea to perform the conversion manually.
141 \item Instead of using the \method{joined()} method, you can also join two
142 paths together with help of the \verb|<<| operator, for instance
143 \samp{p = p1 << p2}.
144 \item $^\ddagger$ In the methods accepting both a parameter value
145 \var{param} and an arc length \var{arclen}, exactly one of these
146 arguments has to provided. Each argument can either be a
147 number/length or a tuple. In the former case, the parameter value
148 \var{param} (\var{arclen}) refers to the whole path and has to be
149 smaller or equal to \method{self.range()} (\method{self.arclen()}),
150 otherwise an exception is raised. In the latter case, the first
151 element has to be an integer specifying the subpath and the second
152 element specifies the parameter value or the arc length inside of
153 this subpath.
154 \end{itemize}
156 \subsection{Path elements}
158 \label{path:pathitem}
160 The class \class{pathitem} is the superclass of all PostScript path
161 construction primitives. It is never used directly, but only by
162 instantiating its subclasses, which correspond one by one to the
163 PostScript primitives.
165 Except for the path elements ending in \code{_pt}, all coordinates
166 passed to the path elements can be given as number (in which case they
167 are interpreted as user units with the currently set default type) or in
168 \PyX\ lengths.
170 The following operation move the current point and open a new subpath:
172 \begin{classdesc}{moveto}{x, y}
173 Path element which sets the current point to the absolute coordinates (\var{x},
174 \var{y}). This operation opens a new subpath.
175 \end{classdesc}
177 \begin{classdesc}{rmoveto}{dx, dy}
178 Path element which moves the current point by (\var{dx}, \var{dy}).
179 This operation opens a new subpath.
180 \end{classdesc}
182 Drawing a straight line can be accomplished using:
184 \begin{classdesc}{lineto}{x, y}
185 Path element which appends a straight line from the current point to the
186 point with absolute coordinates (\var{x}, \var{y}), which becomes
187 the new current point.
188 \end{classdesc}
190 \begin{classdesc}{rlineto}{dx, dy}
191 Path element which appends a straight line from the current point to the
192 a point with relative coordinates (\var{dx}, \var{dy}), which becomes
193 the new current point.
194 \end{classdesc}
196 For the construction of arc segments, the following three operations
197 are available:
199 \begin{classdesc}{arc}{x, y, r, angle1, angle2}
200 Path element which appends an arc segment in counterclockwise 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}{arcn}{x, y, r, angle1, angle2}
211 Path element which appends an arc segment in clockwise direction
212 with absolute coordinates (\var{x}, \var{y}) of the center and
213 radius \var{r} from \var{angle1} to \var{angle2} (in degrees).
214 If before the operation, the current point is defined, a straight line
215 is from the current point to the beginning of the arc segment is
216 prepended. Otherwise, a subpath, which thus is the first one in the
217 path, is opened. After the operation, the current point is at the end
218 of the arc segment.
219 \end{classdesc}
221 \begin{classdesc}{arct}{x1, y1, x2, y2, r}
222 Path element which appends an arc segment of radius \var{r}
223 connecting between (\var{x1}, \var{y1}) and (\var{x2}, \var{y2}).\\
224 \end{classdesc}
226 B\'ezier curves can be constructed using: \
228 \begin{classdesc}{curveto}{x1, y1, x2, y2, x3, y3}
229 Path element which appends a B\'ezier curve with
230 the current point as first control point and the other control points
231 (\var{x1}, \var{y1}), (\var{x2}, \var{y2}), and (\var{x3}, \var{y3}).
232 \end{classdesc}
234 \begin{classdesc}{rcurveto}{dx1, dy1, dx2, dy2, dx3, dy3}
235 Path element which appends a B\'ezier curve with
236 the current point as first control point and the other control points
237 defined relative to the current point by the coordinates
238 (\var{dx1}, \var{dy1}), (\var{dx2}, \var{dy2}), and (\var{dx3}, \var{dy3}).
239 \end{classdesc}
241 Note that when calculating the bounding box (see Sect.~\ref{bbox}) of
242 B\'ezier curves, \PyX{} uses for performance reasons the so-called
243 control box, i.e., the smallest rectangle enclosing the four control
244 points of the B\'ezier curve. In general, this is not the smallest
245 rectangle enclosing the B\'ezier curve.
247 Finally, an open subpath can be closed using:
249 \begin{classdesc}{closepath}{}
250 Path element which closes the current subpath.
251 \end{classdesc}
253 For performance reasons, two non-PostScript path elements are defined,
254 which perform multiple identical operations:
256 \begin{classdesc}{multilineto_pt}{points_pt}
257 Path element which appends straight line segments starting from
258 the current point and going through the list of points given
259 in the \var{points_pt} argument. All coordinates have to
260 be given in PostScript points.
261 \end{classdesc}
263 \begin{classdesc}{multicurveto_pt}{points_pt}
264 Path element which appends B\'ezier curve segments starting from
265 the current point and going through the list of each three control
266 points given in the \var{points_pt} argument.
267 \end{classdesc}
270 \subsection{Class \class{normpath}}
272 \label{path:normpath}
274 The \class{normpath} class represents a specialized form of a
275 \class{path} containing only the elements \class{moveto},
276 \class{lineto}, \class{curveto} and \class{closepath}. Such normalized
277 paths are used for all of the more sophisticated path operations
278 which are denoted by a $\dagger$ in the description of the \class{path}
279 class above.
281 Any path can easily be converted to its normalized form by calling
282 the \class{normpath} constructor, i.e.,
283 \begin{verbatim}
284 np = p.normpath())
285 \end{verbatim}
286 Additionally, you can specify the accuracy (in points) which is used
287 in all \class{normpath} calculations by means of the argument
288 \var{epsilon}, which defaults to the value defined using
289 \function{path.set}. Initially, this value is set to $10^{-5}$ points.
290 Note that the sum of a \class{normpath} and a \class{path} always
291 yields a \class{normpath}.
293 Alternatively, you can pass a list of \class{subnormpath} instances.
295 \begin{classdesc}{normpath}{subnormpaths=[]}
296 Construct a \class{normpath} consisting of \var{subnormpaths}, which
297 is a list of \class{subnormpath} instances.
298 \end{classdesc}
300 In addition to the \class{path} methods, a \class{normpath} instance
301 also offers the following methods, which operate on the instance itself:
303 \begin{methoddesc}{join}{other}
304 Join \var{other}, which has to be a \class{path} instance, to
305 the \class{normpath} instance.
306 \end{methoddesc}
308 \begin{methoddesc}{reverse}{}
309 Reverses the \class{normpath} instance.
310 \end{methoddesc}
312 \begin{methoddesc}{transform}{trafo}
313 Transforms the \class{normpath} instance according to the linear
314 transformation \var{trafo}.
315 \end{methoddesc}
317 \subsection{Class \class{subnormpath}}
319 XXX To be written
321 \subsection{Predefined paths}
323 \label{path:predefined}
325 For convenience, some oft-used paths are already predefined. All
326 of them are subclasses of the \class{path} class.
328 \begin{classdesc}{line}{x0, y0, x1, y1}
329 A straight line from the point (\var{x0}, \var{y0}) to the point (\var{x1}, \var{y1}).
330 \end{classdesc}
332 \begin{classdesc}{curve}{x0, y0, x1, y1, x2, y2, x3, y3}
333 A B\'ezier curve with
334 control points (\var{x0}, \var{y0}), $\dots$, (\var{x3}, \var{y3}).\
335 \end{classdesc}
337 \begin{classdesc}{rect}{x, y, w, h}
338 A closed rectangle with lower left point (\var{x}, \var{y}), width \var{w}, and
339 height \var{h}.
340 \end{classdesc}
342 \begin{classdesc}{circle}{x, y, r}
343 A closed circle with center (\var{x}, \var{y}) and radius \var{r}.
344 \end{classdesc}
346 %%% Local Variables:
347 %%% mode: latex
348 %%% TeX-master: "manual.tex"
349 %%% ispell-dictionary: "british"
350 %%% End: