Debian Codegraph @

texmf-dist/doc/latex/hobby/hobby.tex

raw
1 %\immediate\write18{tex hobby.dtx}
2 \documentclass{ltxdoc}
3 \usepackage[T1]{fontenc}
4 \usepackage{csquotes}
5 \usepackage{lmodern}
6 \usepackage{tikz}
7 \usepackage{pgfplots}
8 \usepackage{amsmath}
9 \usepackage{fancyvrb}
10 \usetikzlibrary{hobby,decorations.pathreplacing}
11 \usepackage{listings}
12 \usepackage{hyperref}
13 \pgfplotsset{compat=1.9}
14
15 \lstset{
16 breakatwhitespace=true,
17 breaklines=true,
18 language=[LaTeX]TeX,
19 basicstyle=\small\ttfamily,
20 keepspaces=true,
21 columns=fullflexible
22 }
23 % hobby specific keywords (we can't put keywords with spaces :()
24 \lstset{
25 emphstyle={\color{red}},
26 emph={and,angle,blank,blanks,closed,controls,curl,curve,designated,disjoint,excess,finish,Hobby,hobby,in,invert,next,out,path,previous,quick,restore,save,shortcut,show,soft,tension,through,use}
27 }
28 % tikz keywords used in this documentation
29 \lstset{
30 emphstyle={[2]\color{blue!70!black}},
31 emph={[2]addplot,axis,blue,coordinates,distance,double,draw,every,foreach,grid,help,line,lines,plot,postaction,red,rotate,scale smooth,scale,smooth,style,thick,tikz,tikzpicture,to,ultra,white,width,xshift,yellow}
32 }
33
34 \EnableCrossrefs
35 \CodelineIndex
36 \RecordChanges
37
38 \newenvironment{example}
39 {\VerbatimEnvironment
40 \begin{VerbatimOut}{example.out}}
41 {\end{VerbatimOut}
42 \begin{center}
43 \setlength{\parindent}{0pt}
44 \fbox{\begin{minipage}{.9\linewidth}
45 \lstinputlisting[]{example.out}
46 \end{minipage}}
47 \fbox{\begin{minipage}{.9\linewidth}
48 \centering
49 \input{example.out}
50 \end{minipage}}
51 \end{center}
52 }
53
54
55
56 \tikzset{
57 show curve controls/.style={
58 decoration={
59 show path construction,
60 curveto code={
61 \draw [blue, dashed]
62 (\tikzinputsegmentfirst) -- (\tikzinputsegmentsupporta)
63 node [at end, draw, solid, red, inner sep=2pt]{};
64 \draw [blue, dashed]
65 (\tikzinputsegmentsupportb) -- (\tikzinputsegmentlast)
66 node [at start, draw, solid, red, inner sep=2pt]{};
67 }
68 },decorate
69 },
70 }
71
72 %\bibliographystyle{plain}
73
74 \renewcommand{\thefootnote}{\fnsymbol{footnote}}
75
76 \providecommand*{\url}{\texttt}
77 \title{The \textsf{Hobby} package}
78 \author{Andrew Stacey \\ \url{[email protected]}}
79 \date{\hobbyVersion\ from\ \hobbyDate}
80 \begin{document}
81 \maketitle
82
83 \section{Introduction}
84
85 John Hobby's algorithm, \cite{MR834054}, produces a curve through a given set of points.
86 The curve is constructed as a list of cubic B\'ezier curves with endpoints at subsequent points in the list.
87 The parameters of the curves are chosen so that the joins are ``smooth''.
88 The algorithm was devised as part of the MetaPost program.
89
90 TikZ/PGF has the ability to draw a curve through a given set of points but its algorithm is somewhat simpler than Hobby's and consequently does not produce as aesthetically pleasing curve as Hobby's algorithm does.
91 This package implements Hobby's algorithm in \TeX{} so that TikZ/PGF can make use of it and thus produce nicer curves through a given set of points.
92
93 Hobby's algorithm allows for considerable customisation in that it can take into account various parameters.
94 These are all allowed in this implementation.
95
96 There is also a ``quick'' version presented here.
97 This is a modification of Hobby's algorithm with the feature that any point only influences a finite number (in fact, two) of the previous segments (in Hobby's algorithm the influence of a point dies out exponentially but never completely).
98 This is achieved by applying Hobby's algorithm to subpaths.
99 The resulting path produced with this ``quick'' version is not as ideal as that produced by Hobby's full algorithm, but is still much better than that produced by the \Verb+plot[smooth]+ method in TikZ/PGF, as can be seen in Figure~\ref{fig:comparison}.
100 As this is intended as a simpler method, it does not (at present) admit the same level of customisation as the full implementation.
101 The ``quick'' algorithm is described in full in Section~\ref{sec:quick}.
102
103 The full algorithm is implemented in \LaTeX3 with no reference to TikZ or PGF.
104 It makes extensive use of the \Verb+fp+ and \Verb+prop+ libraries for the computation steps.
105 The TikZ library is simply a wrapper that takes the user's input, converts it into the right format for the \LaTeX3 code, and then calls that code to generate the path.
106 The ``quick'' version does not use \LaTeX3 and relies instead on the \Verb+PGFMath+ library for the computation.
107
108
109 Figure~\ref{fig:comparison} is a comparison of the three methods.
110 The red curve is drawn using Hobby's algorithm.
111 The blue curve is drawn with the \Verb+plot[smooth]+ method from TikZ/PGF.
112 The green curve uses the ``quick'' version.
113 Figure~\ref{fig:metapost} compares the implementation with that given by MetaPost.
114
115 \begin{figure}
116 \centering
117 \begin{tikzpicture}[scale=.5]
118 \draw[red,line width=5pt] (0,0) to[curve through={(6,4) .. (4,9) .. (1,7)}] (3,5);
119 \draw[ultra thick,blue] plot[smooth] coordinates {(0,0) (6,4) (4,9) (1,7) (3,5)};
120 \draw[green,line width=2pt] (0,0) to[quick curve through={(6,4) (4,9) (1,7)}] (3,5);
121 \end{tikzpicture}
122 \caption{Comparison of the three algorithms}
123 \label{fig:comparison}
124 \end{figure}
125
126
127 \begin{figure}
128 \centering
129 \begin{tikzpicture}[scale=.5]
130 \draw[scale=.1,postaction=show curve controls,line width=1mm,red] (0,0)
131 .. controls (26.76463,-1.84543) and (51.4094,14.58441) .. (60,40)
132 .. controls (67.09875,61.00188) and (59.76253,84.57518) .. (40,90)
133 .. controls (25.35715,94.01947) and (10.48064,84.5022) .. (10,70)
134 .. controls (9.62895,58.80421) and (18.80421,49.62895) .. (30,50);
135 \fill[green] (0,0) circle[radius=2pt]
136 (6,4) circle[radius=2pt]
137 (4,9) circle[radius=2pt]
138 (1,7) circle[radius=2pt]
139 (3,5) circle[radius=2pt];
140 \draw[postaction=show curve controls,thick] (0,0) to[curve through={(6,4) .. (4,9) .. (1,7)}] (3,5);
141 \begin{scope}[xshift=10cm]
142 \draw[scale=.1,postaction=show curve controls,line width=1mm,red] (0,0)
143 .. controls (5.18756,-26.8353) and (60.36073,-18.40036) .. (60,40)
144 .. controls (59.87714,59.889) and (57.33896,81.64203) .. (40,90)
145 .. controls (22.39987,98.48387) and (4.72404,84.46368) .. (10,70)
146 .. controls (13.38637,60.7165) and (26.35591,59.1351) .. (30,50)
147 .. controls (39.19409,26.95198) and (-4.10555,21.23804) .. (0,0); %
148 \fill[green] (0,0) circle[radius=2pt]
149 (6,4) circle[radius=2pt]
150 (4,9) circle[radius=2pt]
151 (1,7) circle[radius=2pt]
152 (3,5) circle[radius=2pt];
153 \draw[postaction=show curve controls,thick] (0,0) to[closed,curve through={(6,4) .. (4,9) .. (1,7)}] (3,5);
154 \end{scope}
155 \end{tikzpicture}
156 \caption{Hobby's algorithm in TikZ overlaying the output of MetaPost}
157 \label{fig:metapost}
158 \end{figure}
159
160 \section{Usage}
161 The package is provided in form of a TikZ library.
162 It can be loaded with
163 \begin{verbatim}
164 \usetikzlibrary{hobby}
165 \end{verbatim}
166
167 \textbf{Warning}: This package makes extensive use of \LaTeX3.
168 On occasion, updates to \LaTeX3 packages have resulted in this package behaving oddly or not working at all.
169 The most up to date version of this package can be obtained from my \href{https://github.org/loopspace/hobby}{github page} (download \Verb+hobby.dtx+ and run \Verb+tex hobby.dtx+ to generate the files).
170 Often, such issues are reported on the \href{http://tex.stackexchange.com}{TeX-SX} site and workarounds quickly found so it is worth checking there as well.
171
172 \bigskip
173
174 There are a variety of ways of specifying the data to the algorithm to generate the curve.
175
176 \subsection{As a \textsf{to path}.}
177
178 The key \Verb+curve through={<points>}+ installs a \Verb+to path+ which draws a smooth curve through the given points.
179 The points should be specified as a list which can be optionally separated by dots.
180 The purpose of allowing the dots is to make it simpler to switch between the \Verb+to path+ method and the \Verb+shortcut+ method (described in Section~\ref{sec:shortcut}).
181 However, note that the two methods are not completely synonymous due to how one can specify options so care must still be taken when switching.
182
183 \begin{example}
184 \begin{tikzpicture}[scale=.5]
185 \draw (0,0) to[curve through={(6,4) .. (4,9) .. (1,7)}] (3,5);
186 \end{tikzpicture}
187 \end{example}
188
189 \begin{example}
190 \begin{tikzpicture}[scale=.5]
191 \draw (0,0) to[curve through={(6,4) (4,9) (1,7)}] (3,5);
192 \end{tikzpicture}
193 \end{example}
194
195 There is a corresponding key \Verb+quick curve through={<points>}+ which uses the ``quick'' algorithm.
196 Again, the dots are optional.
197
198 \begin{example}
199 \begin{tikzpicture}[scale=.5]
200 \draw (0,0) to[quick curve through={(6,4) .. (4,9) .. (1,7)}] (3,5);
201 \end{tikzpicture}
202 \end{example}
203
204 \subsection{The \textsf{shortcut} Method}
205 \label{sec:shortcut}
206
207 There is also the facility to subvert TikZ's path processor and define curves simply using the \Verb+..+ separator between points.
208 Note that this relies on something a little special in TikZ: the syntax \Verb+(0,0) .. (2,3)+ is currently detected and processed but there is no action assigned to that syntax.
209 As a later version of TikZ may assign some action to that syntax, this package makes its override optional via the key \Verb+use Hobby shortcut+ (which can be set globally if so desired).
210
211 \begin{example}
212 \begin{tikzpicture}[scale=.5,use Hobby shortcut]
213 \draw (-3,0) -- (0,0) .. (6,4) .. (4,9) .. (1,7) .. (3,5) -- ++(2,0);
214 \end{tikzpicture}
215 \end{example}
216
217 \begin{example}
218 \begin{tikzpicture}[scale=.5,use quick Hobby shortcut]
219 \draw (-3,0) -- (0,0) .. (6,4) .. (4,9) .. (1,7) .. (3,5) -- ++(2,0);
220 \end{tikzpicture}
221 \end{example}
222
223 \subsection{The \textsf{Plot Handler} Method}
224
225 The algorithms can also be used via the \Verb+plot handler+ syntax.
226 This library registers three plot handlers: \Verb+hobby+, \Verb+closed hobby+, and \Verb+quick hobby+.
227 The first is an open curve through the points using the full algorithm, the second is a closed curve, and the third uses the quick algorithm (and is thus an open curve).
228
229 \begin{example}
230 \tikz[smooth] \draw plot coordinates {(0,0) (1,1) (2,0) (3,1) (2,1) (10:2cm)};
231
232 \tikz[hobby] \draw plot coordinates {(0,0) (1,1) (2,0) (3,1) (2,1) (10:2cm)};
233
234 \tikz[closed hobby] \draw plot coordinates {(0,0) (1,1) (2,0) (3,1) (2,1) (10:2cm)};
235
236 \tikz[quick hobby] \draw plot coordinates {(0,0) (1,1) (2,0) (3,1) (2,1) (10:2cm)};
237 \end{example}
238
239 This has the side effect that these can be used with the \Verb+pgfplots+ package.
240 However, the Hobby algorithm is designed to draw a curve in 2D-{}space and does not take into account the fact that when plotting a graph then the two dimensions are treated differently.
241
242 \begin{example}
243 \begin{tikzpicture}
244 \begin{axis}
245 \addplot +[smooth] {rnd};
246 \addplot +[hobby] {rnd};
247 \end{axis}
248 \end{tikzpicture}
249 \end{example}
250
251 \subsection{Basic Level PGF Commands}
252
253 (Suggested by the question \href{http://tex.stackexchange.com/q/159896/86}{How to combine Hobby paths with PGF Basic Layer commands?} on TeX-SX.)
254
255 In some circumstances, it is convenient to bypass TikZ and use more basic PGF commands for building a path.
256 It is possible to add a path built using Hobby's algorithm in this fashion.
257 The commands are:
258
259 \begin{itemize}
260 \item \Verb+\pgfpathhobby+ to initialise the path.
261 If this is followed by a braced group then the contents of that are taken as options to the algorithm.
262
263 \item \Verb+\pgfpathhobbypt{<pgf point specification>}+ to add a point to the path.
264 If this is followed by a braced group then the contents of that are taken as options for that point.
265
266 \item \Verb+\pgfpathhobbyend+ finalises the path.
267 This applies the algorithm to the set of specified points and adds it to the current path.
268 \end{itemize}
269
270 \begin{example}
271 \begin{tikzpicture}
272 \pgfpathmoveto{\pgfpoint{0}{0}}
273 \pgfpathlineto{\pgfpoint{1cm}{0}}
274 \pgfpathhobby{closed=true}
275 \pgfpathhobbypt{\pgfpoint{1cm}{2cm}}{tension in=2}
276 \pgfpathhobbypt{\pgfpoint{2cm}{1cm}}
277 \pgfpathhobbypt{\pgfpoint{3cm}{0cm}}
278 \pgfpathhobbyend
279 \pgfusepath{stroke}
280 \end{tikzpicture}
281 \end{example}
282
283 \section{Customisation}
284
285 There are various ways to customise the path generated by the Hobby algorithms.
286 The full algorithm has a variety of parameters which can be varied to produce different paths through the same points.
287 These vary from specifying that the path be open or closed, to specifying ``tensions'' at each point to change how the path approaches or leaves it.
288
289 \subsection{Algorithm Customisations}
290
291 The main algorithm (i.e., not the ``quick'' variant) can deal with open or closed paths, it is possible to vary the ``tensions'' between the specified points of the paths, and for an open path it is possible to specify the incoming and outgoing angles either directly or via certain ``curl'' parameters.
292 When using the \Verb+to path+ specification, the parameters can be specified before or after the \Verb+curve through+ key or as options to the coordinates.
293 When using the \Verb+shortcut+ specification, the parameters can be given on the path or on coordinates.
294
295 On occasion, it is ambiguous which curve an option belongs to.
296 This is most likely if a coordinate happens to belong to two curves, or if a coordinate is parsed before TikZ knows that it is constructing a curve using this library.
297 The simplest solution is to move the option to a place where there is no ambiguity.
298 Other solutions to this problem will be detailed later.
299
300 Let us start with the customisations to the algorithm.
301
302 \begin{itemize}
303 \item Basic curve.
304 \begin{example}
305 \begin{tikzpicture}
306 \draw[postaction=show curve controls] (0,0) to[curve through={(1,.5) .. (2,0) .. (3,.5)}] (4,0);
307 \draw[xshift=5cm,use Hobby shortcut,postaction=show curve controls] (0,0) .. (1,.5) .. (2,0) .. (3,.5) .. (4,0);
308 \end{tikzpicture}
309 \end{example}
310
311 \item The path can be open, as above, or closed.
312 %%
313 \begin{example}
314 \begin{tikzpicture}[scale=.5]
315 \draw[postaction=show curve controls] (0,0) to[closed,curve through={(1,.5) .. (2,0) .. (3,.5)}] (4,0);
316 \draw[xshift=5cm,use Hobby shortcut,postaction=show curve controls] ([closed]0,0) .. (1,.5) .. (2,0) .. (3,.5) .. (4,0);
317 \end{tikzpicture}
318 \end{example}
319
320 \item Specifying the angle at which the curve goes \emph{out} and at which it comes \emph{in}.
321 The angles given are absolute.
322 \begin{example}
323 \begin{tikzpicture}
324 \draw[postaction=show curve controls] (0,0) to[out angle=0,in angle=180,curve through={(1,.5) .. (2,0) .. (3,.5)}] (4,0);
325 \draw[xshift=5cm,use Hobby shortcut,postaction=show curve controls] ([out angle=0,in angle=180]0,0) .. (1,.5) .. (2,0) .. (3,.5) .. (4,0);
326 \end{tikzpicture}
327 \end{example}
328
329 \item Applying tension as the curve comes in to a point.
330 \begin{example}
331 \begin{tikzpicture}
332 \draw[postaction=show curve controls] (0,0) to[curve through={(1,.5) .. ([tension in=2]2,0) .. (3,.5)}] (4,0);
333 \draw[xshift=5cm,use Hobby shortcut,postaction=show curve controls] (0,0) .. (1,.5) .. ([tension in=2]2,0) .. (3,.5) .. (4,0);
334 \end{tikzpicture}
335 \end{example}
336
337 \item Applying the same tension as a curve comes in and goes out of a point.
338 \begin{example}
339 \begin{tikzpicture}
340 \draw[postaction=show curve controls] (0,0) to[curve through={(1,.5) .. ([tension=2]2,0) .. (3,.5)}] (4,0);
341 \end{tikzpicture}
342 \end{example}
343
344 \item Specifying the \emph{curl} parameters.
345 \begin{example}
346 \begin{tikzpicture}
347 \draw[postaction=show curve controls] (0,0) to[in curl=.1,out curl=3,curve through={(1,.5) .. (2,0) .. (3,.5)}] (4,0);
348 \draw[xshift=5cm,use Hobby shortcut,postaction=show curve controls] (0,0) .. ([in curl=.1,out curl=3]1,.5) .. (2,0) .. (3,.5) .. (4,0);
349 \end{tikzpicture}
350 \end{example}
351 \end{itemize}
352
353 \subsection{Edge Cases}
354
355 Angles are constrained to lie in the interval \((-\pi,\pi]\).
356 This can introduce edge cases as there is a point where we have to compare an angle with \(-\pi\) and if it is equal, add \(2 \pi\).
357 This will occur if the path ``doubles back'' on itself as in the next example.
358 By nudging the repeated point slightly, the behaviour changes drastically.
359
360 \begin{example}
361 \begin{tikzpicture}[use Hobby shortcut]
362 \draw (0,0) .. (1,0) .. (0,0) .. (0,-1);
363 \draw[xshift=2cm] (0,0) .. (1,0) .. (0,0.1) .. (0,-1);
364 \draw[xshift=4cm] (0,0) .. (1,0) .. (0,-0.1) .. (0,-1);
365 \end{tikzpicture}
366 \end{example}
367
368 Due to the precision of the computations, it is not possible to always get this test correct.
369 The simplest solution is to nudge the repeated point in one direction or the other.
370 Experimenting shows that the ``nudge factor'' can be extremely small (note that it will be proportional to the distance between the specified points).
371 It is best to nudge it in the direction most normal to the line between the specified points as the goal is to nudge the difference of the angles.
372 An alternative solution is to add an additional point for the curve to go through.
373 \begin{example}
374 \begin{tikzpicture}[use Hobby shortcut]
375 \draw (0,0) .. (1,0) .. (0,0) .. (0,-1);
376 \draw[xshift=2cm] (0,0) .. (1,0) .. (0,0.002) .. (0,-1);
377 \draw[xshift=4cm] (0,0) .. (1,0) .. (0,-0.002) .. (0,-1);
378 \end{tikzpicture}
379 \end{example}
380
381 Lastly, it is possible to add an \Verb+excess angle+ key to a coordinate.
382 This will add the corresponding multiple of \(2\pi\) to the angle difference.
383 \begin{example}
384 \begin{tikzpicture}[use Hobby shortcut]
385 \draw (0,0) .. (1,0) .. (0,0) .. (0,-1);
386 \draw[xshift=2cm] (0,0) .. ([excess angle=1]1,0) .. (0,0) .. (0,-1);
387 \draw[xshift=4cm] (0,0) .. ([excess angle=-1]1,0) .. (0,0) .. (0,-1);
388 \end{tikzpicture}
389 \end{example}
390
391 Although this is intended to be an integer, no check is done and so some quite odd curves can result from changing this parameter.
392
393 \subsection{Reusing Paths}
394
395 Although the (full) algorithm has good theoretical computation time, using \TeX\ for its implementation does not provide for fast runs.
396 The externalisation library of TikZ/PGF can be used to save whole pictures, but it can be useful to save a generated path within a single \Verb+tikzpicture+ for later use within that same picture.
397 The implementation allows for this by separating the generation of the path from its use.
398
399 \begin{example}
400 \begin{tikzpicture}
401 \draw[line width=3mm,red,use Hobby shortcut,save Hobby path={saved}] (0,0) .. (1,1) .. (2,0);
402 \draw[xshift=2cm,ultra thick,yellow] (0,0) [restore and use Hobby path={saved}{}];
403 \end{tikzpicture}
404 \end{example}
405 %
406
407 Note that the key \Verb+restore and use Hobby path+ is given \emph{after} the initial \Verb+(0,0)+.
408 This is because by default the path generated by the Hobby algorithm does not start with an explicit \Verb+moveto+ since that is the standard behaviour of all of PGF's path construction macros.
409 So the \Verb+(0,0)+ ensures that our path is well-{}formed by issuing an initial \Verb+moveto+.
410 An alternative would be to use the key \Verb+disjoint+ which does add an initial \Verb+moveto+.
411
412 \begin{example}
413 \begin{tikzpicture}
414 \draw[line width=3mm,red,use Hobby shortcut,save Hobby path={saved}] (0,0) .. (1,1) .. (2,0);
415 \draw[xshift=2cm,ultra thick,yellow,restore and use Hobby path={saved}{disjoint}];
416 \end{tikzpicture}
417 \end{example}
418 %
419
420 An example of where this is useful is in drawing knot diagrams.
421 When so doing, it is sometimes convenient to draw a path (or segment of a path) twice in order to get the under/over crossings correct.
422 For this situation, it can be useful to designate certain parts of the path as \Verb+blank+, whereby we mean to redraw them later.
423 The point of a blank segment of a curve is that it is still taken into account when computing the algorithm but is left blank when it comes to rendering.
424 A path can then be redrawn with the blank/non-{}blank segments reversed.
425 As it might be desired to have only some blank segments drawn the second time, there are two types of blank.
426 Only a \Verb+soft+ blank will be reversed in these circumstances.
427
428 \begin{example}
429 \begin{tikzpicture}[use Hobby shortcut,line width=1mm,rotate=90]
430 \draw[blue,save Hobby path={left}] ([out angle=90,in angle=-90]1,0) .. (1,1) .. ([blank=soft]0,2) .. (1,3) .. (1,4);
431 \draw[red] ([out angle=90,in angle=-90]0,0) .. (0,1) .. (1,2) .. (0,3) .. (0,4);
432 \draw[blue,restore and use Hobby path={left}{disjoint,invert soft blanks}];
433 \end{tikzpicture}
434 \end{example}
435 %
436
437 This can be taken a step further.
438 The generated data can be saved to the \Verb+aux+ file and read back in, avoiding the need to regenerate it on each run.
439 To engage this facility, the Hobby path has to be named (via \Verb+save Hobby path+) and the key \Verb+Hobby externalise+ (or \Verb+Hobby externalize+) must be given in a context that applies (such as on the path or on the surrounding scope).
440
441 \bigskip
442
443
444 The relevant keys are the following.
445
446 \begin{itemize}
447 \item \Verb+use previous Hobby path[=<options>]+.
448 This (re)uses the previously generated Hobby path.
449 As all the data is globally stored, this can technically be in a different \Verb+tikzpicture+.
450 The \Verb+<options>+ will be applied, in so far as they are options that can be applied after the algorithm has run.
451
452 \item \Verb+save Hobby path=<name>+.
453 Saves a path for later use.
454 The path is saved in a global macro so can be reused in another picture.
455
456 \item \Verb+restore Hobby path=<name>+.
457 This restores the named Hobby path (if it exists).
458 It does not \emph{use} it.
459 After this key, \Verb+use previous Hobby path+ will use the restored path.
460
461 \item \Verb+restore and use Hobby path={<name>}{<options>}+.
462 This restores the named path and uses it with \Verb+<options>+ applied.
463
464 \item \Verb+Hobby externalise+ or \Verb+Hobby externalize+.
465 This puts in place the code for saving the generated data to the \Verb+aux+ file.
466 On subsequent runs, it uses the saved data rather than the current data.
467 For a curve to make use of this, it has to be named via the \Verb+save Hobby path+ key.
468 So to regenerate the data, either delete the \Verb+aux+ file, remove the \Verb+save Hobby path+ key for one compilation run, or issue the command \Verb+\HobbyDisableAux+ which disables writing paths to the \Verb+aux+ file (note that the paths will be regenerated on the run \emph{after} the first run with this command issued).
469 \end{itemize}
470
471 The options that can be applied are those that affect the rendering of the curve but not its generation.
472 When the curve is rendered (or \emph{used}, in the above parlance), \TeX\ steps along the coordinates of the generated curve and carries out an action for each piece.
473 This action can be modified after the curve has been generated.
474 The action will be one of:
475 %
476 \begin{itemize}
477 \item Move to the end point (ignoring the control points).
478 \item Draw a Bezier curve to the end point through the control points.
479 \item Draw a Bezier curve to the end point through the control points and then move to the end point.
480 \end{itemize}
481 %
482 The last is subtle: the move doesn't actually go anywhere but it ``breaks'' the curve at the designated point.
483 In particular, a later \Verb+cycle+ would return to this point (or a later break) rather than to the start of the curve.
484
485 These actions are triggered by the keys \Verb+blank+ and \Verb+break+.
486 Each should be specified to the coordinate at the \emph{end} of the segment under consideration.
487 The \Verb+blank+ key can be given the argument \Verb+soft+.
488 The effect of this is seen when the key \Verb+invert soft blanks+ is used.
489 This swaps the drawing action so that non-{}blank segments are skipped and \emph{soft} blanks are drawn.
490 Non-{}soft-{}blank segments are still not drawn.
491
492 \begin{example}
493 \begin{tikzpicture}[use Hobby shortcut]
494 \draw (0,0) .. (1,1) .. ([blank=soft]2,0) .. (3,1) .. ([blank]4,0) .. (5,1);
495 \draw[red,use previous Hobby path={invert soft blanks,disjoint}];
496 \end{tikzpicture}
497 \end{example}
498 %
499
500 As a more practical application, consider the following rendering of a trefoil knot.
501
502 \begin{example}
503 \begin{tikzpicture}[
504 use Hobby shortcut,
505 every path/.style={
506 line width=1mm,
507 white,
508 double=red,
509 double distance=.5mm
510 }
511 ]
512 \draw ([closed]0,2) .. ([blank=soft]210:.5) .. (-30:2) .. ([blank=soft]0,.5) .. (210:2) .. ([blank=soft]-30:.5);
513 \draw[use previous Hobby path={invert soft blanks,disjoint}];
514 \end{tikzpicture}
515 \end{example}
516 %
517
518 This could easily be generalised using the \Verb+\foreach+ command, as demonstrated in the next code.
519
520 \begin{example}
521 \begin{tikzpicture}[
522 use Hobby shortcut,
523 every path/.style={
524 line width=1mm,
525 white,
526 double=red,
527 double distance=.5mm
528 }
529 ]
530 \def\nfoil{9}
531 \draw ([closed]0,2)
532 foreach \k in {1,...,\nfoil}{
533 .. ([blank=soft]90+360*\k/\nfoil-180/\nfoil:-.5) .. (90+360*\k/\nfoil:2)
534 };
535 \draw[use previous Hobby path={invert soft blanks,disjoint}];
536 \end{tikzpicture}
537 \end{example}
538 %
539
540 \subsection{Breaking the Path}
541
542 One issue with the shortcut notation is that it is not possible (using this notation) to have two sets of curves following directly on from each other because there is no clear demarcation of the boundary.
543 To make this possible, there is a key \Verb+Hobby action+, which installs an action to be taken after the point has been processed.
544 The general key \Verb+Hobby action={code}+ can install arbitrary code.
545 Probably the more useful variant is \Verb+Hobby finish+ which runs the algorithm on the points gathered so far.
546 An example of the use of this is to make it possible to specify tangencies at certain points.
547 Technically, once a tangent direction has been specified, the Hobby algorithm splits the set of points there and works on each piece separately.
548 The following key implements this, the technicalities are due to the fact that the tangent angle has to be used twice: once to specify the angle of the path coming in to that point and once to specify the angle of the path coming out.
549 Note that specifying the tangent vector at every point means that the algorithm is not actually being used.
550 However, Hobby's formulae for the lengths of the control points is still being used.
551
552 \begin{example}
553 \begin{tikzpicture}[
554 use Hobby shortcut,
555 tangent/.style={%
556 in angle={(180+#1)},
557 Hobby finish,
558 designated Hobby path=next,
559 out angle=#1,
560 },
561 ]
562 \draw[help lines] (-5,-5) grid (5,5);
563 \draw (-5,0) -- (5,0) (0,-5) -- (0,5);
564 \draw[thick] (-5,2) .. ([tangent=0]-3,3) .. (-1,1) .. (0,-1.3) .. ([tangent=0]1,-2) .. ([tangent=45]2,-1.5) .. ([tangent=0]3,-2) .. (5,-4);
565 \end{tikzpicture}
566 \end{example}
567 %
568
569
570
571 \section{Implementing Hobby's Algorithm}
572 We start with a list of \(n+1\) points, \(z_0, \dotsc, z_n\).
573 The base code assumes that these are already stored in two arrays\footnote{Arrays are thinly disguised property lists.}: the \(x\)--coordinates in \Verb+\l_hobby_points_x_array+ and the \(y\)--coordinates in \Verb+\l_hobby_points_y_array+.
574 As our arrays are \(0\)--indexed, the actual number of points is one more than this.
575 For a closed curve, we have \(z_n = z_0\)\footnote{Note that there is a difference between a closed curve and an open curve whose endpoints happen to overlap.}.
576 For closed curves it will be convenient to add an additional point at \(z_1\): thus \(z_{n+1} = z_1\).
577 This makes \(z_n\) an internal point and makes the algorithms for closed paths and open paths agree longer than they would otherwise.
578 The number of apparent points is stored as \Verb+\l_hobby_npoints_int+.
579 Thus for an open path, \Verb+\l_hobby_npoints_int+ is \(n\), whilst for a closed path, it is \(n+1\)\footnote{In fact, we allow for the case where the user specifies a closed path but with \(z_n \ne z_0\).
580 In that case, we assume that the user meant to repeat \(z_0\).
581 This adds another point to the list.}.
582 Following Hobby, let us write \(n'\) for \(n\) if the path is open and \(n+1\) if closed.
583 From this we compute the distances and angles between successive points, storing these again as arrays.
584 These are \Verb+\l_hobby_distances_array+ and \Verb+\l_hobby_angles_array+.
585 The term indexed by \(k\) is the distance (or angle) of the line between the \(k\)th point and the \(k+1\)th point.
586 For the internal nodes\footnote{Hobby calls the specified points \emph{knots}.}, we store the difference in the angles in \Verb+\l_hobby_psi_array+.
587 The \(k\)th value on this is the angle subtended at the \(k\)th node.
588 This is thus indexed from \(1\) to \(n'-1\).
589 The bulk of the work consists in setting up a linear system to compute the angles of the control points.
590 At a node, say \(z_i\), we have various pieces of information:
591 \begin{enumerate}
592 \item The angle of the incoming curve, \(\phi_i\), relative to the straight line from \(z_{i-1}\) to \(z_i\)
593 \item The angle of the outgoing curve, \(\theta_i\), relative to the straight line from \(z_i\) to \(z_{i+1}\)
594 \item The tension of the incoming curve, \(\overline{\tau}_i\)
595 \item The tension of the outgoing curve, \(\tau_i\)
596 \item The speed of the incoming curve, \(\sigma_i\)
597 \item The speed of the outgoing curve, \(\rho_i\)
598 \end{enumerate}
599 The tensions are known at the start.
600 The speeds are computed from the angles.
601 Thus the key thing to compute is the angles.
602 This is done by imposing a ``mock curvature'' condition.
603 The formula for the mock curvature is:
604 %%
605 \[
606 \hat{k}(\theta,\phi,\tau,\overline{\tau}) = \tau^2 \left( \frac{2(\theta + \phi)}{\overline{\tau}} - 6\theta\right)
607 \]
608 %%
609 and the condition that the mock curvatures have to satisfy is that at each \emph{internal} node, the curvatures must match:
610 %
611 \[
612 \hat{k}(\phi_i,\theta_{i-1},\overline{\tau}_i,\tau_{i-1})/d_{i-1} = \hat{k}(\theta_i,\phi_{i+1},\tau_i,\overline{\tau}_{i+1})/d_i.
613 \]
614 %%
615 Substituting in yields:
616 %%
617 \[
618 \frac{\overline{\tau}_i^2}{d_{i-1}} \left( \frac{2(\phi_i + \theta_{i-1})}{\tau_{i-1}} - 6\phi_i\right) = \frac{\tau_i^2}{d_i} \left( \frac{2(\theta_i + \phi_{i+1})}{\overline{\tau}_{i+1}} - 6\theta_i \right).
619 \]
620 %%
621 Let us rearrange that to the following:
622 %%
623 \begin{align*}
624 d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 &\theta_{i-1} \\
625 %%
626 {}+
627 d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (1 - 3 \tau_{i-1}) &\phi_i \\
628 %%
629 {}-
630 d_{i-1} \tau_{i-1} \tau_i^2 (1 - 3 \overline{\tau}_{i+1}) &\theta_i \\
631 %%
632 {}-
633 d_{i-1} \tau_{i-1} \tau_i^2 &\phi_{i+1} \\
634 %%
635 &\qquad =
636 0
637 \end{align*}
638 %%
639 For both open and closed paths this holds for \(i=1\) to \(i=n' - 1\).
640 We also have the condition that \(\theta_i + \phi_i = -\psi_i\) where \(\psi_i\) is the angle subtended at a node by the lines to the adjacent nodes.
641 This holds for the internal nodes\footnote{Recall that by dint of repetition, all nodes are effectively internal for a closed path.}.
642 Therefore for \(i=1\) to \(n'-1\) the above simplifies to the following:
643 %
644 \begin{align*}
645 d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 &\theta_{i-1} \\
646 {}+
647 (d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (3 \tau_{i-1} - 1)
648 {}+
649 d_{i-1} \tau_{i-1} \tau_i^2 (3 \overline{\tau}_{i+1} - 1)) &\theta_i \\
650 {}+
651 d_{i-1} \tau_{i-1} \tau_i^2 & \theta_{i+1} \\
652 =
653 - d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (3 \tau_{i-1} - 1) &\psi_i \\
654 {}- d_{i-1} \tau_{i-1} \tau_i^2& \psi_{i+1}
655 \end{align*}
656 For an open path we have two more equations.
657 One involves \(\theta_0\).
658 The other is the above for \(i = n'-1 = n-1\) with additional information regarding \(\psi_n\).
659 It may be that one or either of \(\theta_0\) or \(\phi_n\) is specified in advance.
660 If so, we shall write the given values with a bar: \(\overline{\theta}_0\) and \(\overline{\phi}_n\).
661 In that case, the first equation is simply setting \(\theta_0\) to that value and the last equation involves substituting the value for \(\phi_n\) into the above.
662 If not, they are given by formulae involving ``curl'' parameters \(\chi_0\) and \(\chi_n\) and result in the equations:
663 %
664 \begin{align*}
665 \theta_0 &= \frac{\tau_0^3 + \chi_0 \overline{\tau}_1^3(3 \tau_0 - 1)}{\tau_0^3(3 \overline{\tau}_1 - 1) + \chi_0 \overline{\tau}_1^3} \phi_1 \\
666 \phi_n &= \frac{\overline{\tau}_n^3 + \chi_n \tau_{n-1}^3(3 \overline{\tau}_n - 1)}{\overline{\tau}_n^3(3 \tau_{n-1} - 1) + \chi_n \tau_{n-1}^3} \theta_{n-1}
667 \end{align*}
668 %%
669 Using \(\phi_1 = - \psi_1 - \theta_1\), the first rearranges to:
670 %%
671 \[
672 (\tau_0^3(3 \overline{\tau}_1 - 1) + \chi_0 \overline{\tau}_1^3) \theta_0 + (\tau_0^3 + \chi_0 \overline{\tau}_1^3(3 \tau_0 - 1)) \theta_1 = - (\tau_0^3 + \chi_0 \overline{\tau}_1^3(3 \tau_0 - 1)) \psi_1.
673 \]
674 %%
675 The second should be substituted in to the general equation with \(i = n-1\).
676 This yields:
677 %%
678 \begin{align*}
679 d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 &\theta_{n-2} \\
680 {}+
681 \big(d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 (3 \tau_{n-2} - 1)
682 {}+
683 d_{n-2} \tau_{n-2} \tau_{n-1}^2 (3 \overline{\tau}_{n} - 1) \\
684 {}- d_{n-2} \tau_{n-2} \tau_{n-1}^2 \frac{\overline{\tau}_n^3 + \chi_n \tau_{n-1}^3(3 \overline{\tau}_n - 1)}{\overline{\tau}_n^3(3 \tau_{n-1} - 1) + \chi_n \tau_{n-1}^3}\big) & \theta_{n-1} \\
685 =
686 - d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 (3 \tau_{n-2} - 1) &\psi_{n-1}
687 \end{align*}
688 %%
689 This gives \(n'\) equations in \(n'\) unknowns (\(\theta_0\) to \(\theta_{n-1}\)).
690 The coefficient matrix is tridiagonal.
691 It is more natural to index the entries from \(0\).
692 Let us write \(A_i\) for the subdiagonal, \(B_i\) for the main diagonal, and \(C_i\) for the superdiagonal.
693 Let us write \(D_i\) for the target vector.
694 Then for an open path we have the following formulae:
695 %%
696 \begin{align*}
697 A_i &= d_i \overline{\tau}_{i+1} \overline{\tau}^2_i \\
698 B_0 &= \begin{cases}
699 1 & \text{if}\; \overline{\theta}_0\; \text{given} \\
700 \tau_0^3(3 \overline{\tau}_1 - 1) + \chi_0 \overline{\tau}^3_1 & \text{otherwise}
701 \end{cases} \\
702 B_i &= d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (3 \tau_{i-1} -1) + d_{i-1} \tau_{i-1} \tau_i^2(3 \overline{\tau}_{i+1} - 1) \\
703 B_{n-1} &= \begin{cases} d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 (3 \tau_{n-2} - 1) + d_{n-2} \tau_{n-2} \tau_{n-1}^2(3 \overline{\tau}_{n} - 1) & \text{if}\; \overline{\phi}_n\; \text{given} \\
704 d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 (3 \tau_{n-2} - 1) + d_{n-2} \tau_{n-2} \tau_{n-1}^2(3 \overline{\tau}_{n} - 1)
705 \\
706 - d_{n-2} \tau_{n-2} \tau_{n-1}^2 \frac{\overline{\tau}_n^3 + \chi_n \tau_{n-1}^3(3 \overline{\tau}_n - 1)}{\overline{\tau}_n^3(3 \tau_{n-1} - 1) + \chi_n \tau_{n-1}^3}) & \text{otherwise}
707 \end{cases} \\
708 C_0 &= \begin{cases}
709 0 & \text{if}\; \overline{\theta}_0\; \text{given} \\
710 \tau_0^3 + \chi_0 \overline{\tau}_1^3(3\tau_0 - 1) & \text{otherwise}
711 \end{cases} \\
712 C_i &= d_{i-1} \tau_{i-1} \tau_i^2 \\
713 D_0 &= \begin{cases}
714 \overline{\theta}_0 & \text{if}\; \overline{\theta}_0\; \text{given} \\
715 - (\tau_0^3 + \chi_0 \overline{\tau}_1^3(3 \tau_0 - 1)) \psi_1 & \text{otherwise}
716 \end{cases} \\
717 D_i &= - d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (3 \tau_{i-1} - 1) \psi_i
718 - d_{i-1} \tau_{i-1} \tau_i^2 \psi_{i+1} \\
719 D_{n-1} &= \begin{cases}
720 - d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 (3 \tau_{n-2} - 1) \psi_{n-1} - d_{n-2} \tau_{n-2} \tau_{n-1}^2 \overline{\phi}_n & \text{if}\; \overline{\phi}_n\; \text{given} \\
721 - d_{n-1} \overline{\tau}_{n} \overline{\tau}_{n-1}^2 (3 \tau_{n-2} - 1) \psi_{n-1} & \text{otherwise}
722 \end{cases}
723 \end{align*}
724 For a closed path, we have \(n\) equations in \(n+2\) unknowns (\(\theta_0\) to \(\theta_{n+1}\)).
725 However, we have not included all the information.
726 Since we have repeated points, we need to identify \(\theta_0\) with \(\theta_n\) and \(\theta_1\) with \(\theta_{n+1}\).
727 To get a system with \(n'\) equations in \(n'\) unknowns, we add the equation \(\theta_0 - \theta_n = 0\) and substitute in \(\theta_{n+1} = \theta_1\).
728 The resulting matrix is not quite tridiagonal but has extra entries on the off-corners.
729 However, it can be written in the form \(M + u v^\top\) with \(M\) tridiagonal.
730 There is some freedom in choosing \(u\) and \(v\).
731 For simplest computation, we take \(u = e_0 + e_{n'-1}\).
732 This means that \(v = d_{n'-2} \tau_{n'-2} \tau_{n'-1}^2 e_1 - e_{n'-1}\).
733 With the same notation as above, the matrix \(M\) is given by the following formulae:
734 %%
735 \begin{align*}
736 A_i &= d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 \\
737 %%
738 B_0 &= 1 \\
739 %%
740 B_i &= d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (3 \tau_{i-1} -1) + d_{i-1} \tau_{i-1} \tau_i^2(3 \overline{\tau}_{i+1} - 1) \\
741 %%
742 B_{n'-1} &= d_{n'-1} \overline{\tau}_{n'} \overline{\tau}_{n'-1}^2 (3 \tau_{n'-2} -1) + d_{n'-2} \tau_{n'-2} \tau_{n'-1}^2(3 \overline{\tau}_{n'} - 1) + 1\\
743 %%
744 C_0 &= - d_{n'-2} \tau_{n'-2} \tau_{n'-1}^2 \\
745 %%
746 C_i &= d_{i-1} \tau_{i-1} \tau_i^2 \\
747 %%
748 D_0 &= 0 \\
749 %%
750 D_i &= - d_i \overline{\tau}_{i+1} \overline{\tau}_i^2 (3 \tau_{i-1} - 1) \psi_i
751 - d_{i-1} \tau_{i-1} \tau_i^2 \psi_{i+1} \\
752 %%
753 D_{n'-1} &= - d_{n'-1} \overline{\tau}_{n'} \overline{\tau}_{n'-1}^2 (3 \tau_{n'-2} - 1) \psi_{n'-1}
754 - d_{n'-2} \tau_{n'-2} \tau_{n'-1}^2 \psi_1
755 \end{align*}
756 The next step in the implementation is to compute these coefficients and store them in appropriate arrays.
757 Having done that, we need to solve the resulting tridiagonal system.
758 This is done by looping through the arrays doing the following substitutions (starting at \(i = 1\)):
759 %
760 \begin{align*}
761 B_i' &= B_{i-1}' B_i - A_i C_{i-1}' \\
762 C_i' &= B_{i-1}' C_i \\
763 D_i' &= B_{i-1}' D_i - A_i D_{i-1}'
764 \end{align*}
765 %%
766 followed by back-substitution:
767 %%
768 \begin{align*}
769 \theta_{n-1} &= D_{n-1}'/B_{n-1}' \\
770 \theta_i &= (D_i' - C_i' \theta_{i+1})/B_i'
771 \end{align*}
772 %%
773 For a closed path, we run this both with the vector \(D\) and the vector \(u = e_0 + e_{n'-1}\).
774 Then to get the real answer, we use the Sherman--{}Morrison formula:
775 %%
776 \[
777 (M + u v^\top)^{-1} D = M^{-1} D - \frac{M^{-1} u v^\top M^{-1} D}{1 + v^\top M^{-1} u}.
778 \]
779 %%
780 This leaves us with the values for \(\theta_i\).
781 We now substitute these into Hobby's formulae for the lengths:
782 %%
783 \begin{align*}
784 \rho_i &= \frac{2 + \alpha_i}{1 + (1 - c) \cos \theta_i + c \cos \phi_{i+1}} \\
785 \sigma_{i+1} &= \frac{2 - \alpha_i}{1 + (1 - c) \cos \phi_{i+1} + c \cos \theta_i} \\
786 \text{where} \;\alpha_i &= a (\sin \theta_i - b \sin \phi_{i+1})(\sin \phi_{i+1} - b \sin \theta_i)(\cos \theta_i - \cos \phi_{i+1})
787 \end{align*}
788 %%
789 and \(a = \sqrt{2}\), \(b = 1/16\), and \(c = (3 - \sqrt{5})/2\).
790 These are actually the \emph{relative} lengths so need to be adjusted by a factor of \(d_i/3\).
791 Now \(\theta_i\) is the angle relative to the line from \(z_i\) to \(z_{i+1}\), so to get the true angle we need to add back that angle.
792 Fortunately, we stored those angles at the start.
793 So the control points are:
794 %%
795 \begin{gather*}
796 d_i \rho_i (\cos (\theta_i + \omega_i), \sin (\theta_i + \omega_i))/3 + z_i \\
797 - d_i \sigma_{i+1} (\cos(\omega_i - \phi_{i+1}), \sin(\omega_i - \phi_{i+1}))/3 + z_{i+1}
798 \end{gather*}
799
800 \section{A Piecewise Version of Hobby's Algorithm}
801 \label{sec:quick}
802 Here we present a variant of Hobby's algorithm.
803 One difficulty with Hobby's algorithm is that it works with the path as a whole.
804 It is therefore not possible to build up a path piecewise.
805 We therefore modify it to correct for this.
806 Obviously, the resulting path will be less ``ideal'', but will have the property that adding new points will not affect earlier segments.
807 The method we use is to employ Hobby's algorithm on two-{}segment subpaths.
808 When applied to a two-{}segment subpath, the algorithm provides two cubic Bezier curves: one from the \(k\)th point to the \(k+1\)st point and the second from the \(k+1\)st to the \(k+2\)nd.
809 Of this data, we keep the first segment and use that for the path between the \(k\)th and \(k+1\)st points.
810 We also remember the outgoing angle of the first segment and use that as the incoming angle on the next computation (which will involve the \(k+1\)st, \(k+2\)nd, and \(k+3\)rd points).
811 The two ends are slightly different to the middle segments.
812 On the first segment, we might have no incoming angle.
813 On the last segment, we render both pieces.
814 This means that for the initial segment, we have a \(2 \times 2\) linear system:
815 %%
816 \[
817 \begin{bmatrix}
818 B_0 & C_0 \\
819 A_1 & B_1
820 \end{bmatrix}
821 \Theta = \begin{bmatrix}
822 D_0 \\ D_1
823 \end{bmatrix}
824 \]
825 %%
826 This has solution:
827 %%
828 \[
829 \Theta = \frac{1}{B_0 B_1 - C_0 A_1} \begin{bmatrix} B_1 & - C_0 \\ -A_1 & B_0 \end{bmatrix} \begin{bmatrix} D_0 \\ D_1 \end{bmatrix} = \frac{1}{B_0 B_1 - C_0 A_1} \begin{bmatrix} B_1 D_0 - C_0 D_1 \\ B_0 D_1 - A_1 D_0 \end{bmatrix}
830 \]
831 Now we have the following values for the constants:
832 %%
833 \begin{align*}
834 A_1 &= d_1 \overline{\tau}_2 \overline{\tau}_1^2 \\
835 %%
836 B_0 &= \tau_0^3 (3 \overline{\tau}_1 - 1) + \chi_0 \overline{\tau}_1^3 \\
837 %%
838 B_1 &= d_1 \overline{\tau}_2 \overline{\tau}_1^2 (3 \tau_0 - 1) + d_0 \tau_0 \tau_1^2(3 \overline{\tau}_2 - 1) - d_0 \tau_0 \tau_1^2 \frac{\overline{\tau}_2^3 + \chi_2 \tau_1^3 (3 \overline{\tau}_2 - 1)}{\overline{\tau}_2^3 (3 \tau_1 - 1) + \chi_2 \tau_1^3} \\
839 %%
840 C_0 &= \tau_0^3 + \chi_0 \overline{\tau}_1^3 (3 \tau_0 - 1) \\
841 %%
842 D_0 &= - (\tau_0^3 + \chi_0 \overline{\tau}_1^3 ( 3 \tau_0 - 1)) \psi_1 \\
843 %%
844 D_1 &= - d_1 \overline{\tau}_2 \overline{\tau}_1^2 (3 \tau_0 - 1) \psi_1
845 \end{align*}
846
847 Let us, as we are aiming for simplicity, assume that the tensions and curls are all \(1\).
848 Then we have \(A_1 = d_1\), \(B_0 = 3\), \(B_1 = 2 d_1 + 2 d_0 - d_0 = 2 d_1 + d_0\), \(C_0 = 3\), \(D_0 = - 3 \psi_1\), \(D_1 = - 2 d_1 \psi_1\).
849 Thus the linear system is:
850 %%
851 \[
852 \begin{bmatrix}
853 3 & 3 \\
854 d_1 & 2 d_1 + d_0
855 \end{bmatrix}
856 \Theta = - \psi_1 \begin{bmatrix}
857 3 \\ 2 d_1
858 \end{bmatrix}
859 \]
860 %%
861 which we can row reduce to:
862 %%
863 \[
864 \begin{bmatrix}
865 1 & 1 \\
866 0 & d_1 + d_0
867 \end{bmatrix}
868 \Theta = -\psi_1 \begin{bmatrix}
869 1 \\ d_1
870 \end{bmatrix}
871 \]
872 %%
873 whence \(\theta_1 = -\psi_1 \frac{d_1}{d_0 + d_1}\) and \(\theta_0 = -\psi_1 - \theta_1 = -\psi_1\frac{d_0 }{d_0 + d_1}\).
874 We also compute \(\phi_1 = -\psi_1 - \theta_1 = \theta_0\) and \(\phi_2 = \theta_1\) (in the simple version).
875 We use \(\theta_0\) and \(\phi_1\) to compute the bezier curve of the first segment, make a note of \(\theta_1\), and -- assuming there are more segments -- throw away \(\phi_2\).
876
877 For the inner segments, we have the system:
878 %%
879 \[
880 \begin{bmatrix}
881 1 & 0 \\
882 A_1 & B_1
883 \end{bmatrix}
884 \Theta = \begin{bmatrix}
885 \theta_0 \\
886 D_1
887 \end{bmatrix}
888 \]
889 %%
890 which has the solution \(\theta_1 = (D_1 - A_1 \theta_0)/B_1\).
891 The values of the constants in this case are:
892 %%
893 \begin{align*}
894 A_1 &= d_1 \overline{\tau}_2 \overline{\tau}_1^2 \\
895 %%
896 B_1 &= d_1 \overline{\tau}_2 \overline{\tau}_1^2 (3 \tau_0 - 1) + d_0 \tau_0 \tau_1^2(3 \overline{\tau}_2 - 1) - d_0 \tau_0 \tau_1^2 \frac{\overline{\tau}_2^3 + \chi_2 \tau_1^3 (3 \overline{\tau}_2 - 1)}{\overline{\tau}_2^3 (3 \tau_1 - 1) + \chi_2 \tau_1^3} \\
897 %%
898 D_1 &= - d_1 \overline{\tau}_2 \overline{\tau}_1^2 (3 \tau_0 - 1) \psi_1
899 \end{align*}
900 Again, let us consider the simpler case.
901 Then \(A_1 = d_1\), \(B_1 = 2 d_1 + d_0\), and \(D_1 = - 2 d_1 \psi_1\).
902 Thus \(\theta_1 = (-2 d_1 \psi_1 - d_1 \theta_0)/(2 d_1 + d_0) = - (2 \psi_1 + \theta_0) \frac{d_1}{2 d_1 + d_0}\).
903 We compute \(\phi_1 = -\psi_1 - \theta_1 = \frac{- \psi_1 d_0 + \theta_0 d_1}{2 d_1 + d_0}\) and \(\phi_2 = \theta_1\).
904 Then we store \(\theta_1\) for the next iteration.
905
906 The actual curves are then produced from the angles using the same formulae for the lengths of the control points as in the main algorithm.
907
908 At the last stage, we render both segments of the generated curve.
909
910 \section{Acknowledgements}
911
912 This package began life as an answer to the question \href{http://tex.stackexchange.com/q/54771/86}{Curve through a sequence of points with Metapost and TikZ}.
913 Once released upon the unsuspecting world, various questions on the \href{http://tex.stackexchange.com}{TeX-SX} site have prompted new features (and bug-fixes).
914 Most of these can be found by looking at the \href{http://tex.stackexchange.com/questions/tagged/hobby}{list of questions tagged ``hobby''} on that site.
915
916
917 \begin{thebibliography}{1} \bibitem{MR834054} John~D. Hobby. \newblock Smooth, easy to compute interpolating splines. \newblock {\em Discrete Comput. Geom.}, 1:123--140, 1986. \end{thebibliography}
918
919 \end{document}