Debian Codegraph @

texmf-dist/source/latex/hobby/hobby_code.dtx

raw
1 % \iffalse meta-comment
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 ----------------------------------------------------------------
7 hobby --- a TikZ/PGF library for drawing smooth(ish) curves using
8 Hobby's algorithm (implemented in LaTeX3)
9 E-mail: [email protected]
10 Released under the LaTeX Project Public License v1.3c or later
11 See http://www.latex-project.org/lppl.txt
12 ----------------------------------------------------------------
13
14 This package defines a path generation function for TikZ/PGF
15 which implements Hobby's algorithm for a path built out of Bezier
16 curves which passes through a given set of points.
17
18 The implementation is in LaTeX3. It can be used as as a TikZ
19 `to path`.
20 %</readme>
21 %<*internal>
22 \fi
23 \def\nameofplainTeX{plain}
24 \ifx\fmtname\nameofplainTeX\else
25 \expandafter\begingroup
26 \fi
27 %</internal>
28 %<*install>
29 \input docstrip.tex
30 \keepsilent
31 \askforoverwritefalse
32 \preamble
33 ----------------------------------------------------------------
34 hobby --- a TikZ/PGF library for drawing smooth(ish) curves using
35 Hobby's algorithm (implemented in LaTeX3)
36 E-mail: [email protected]
37 Released under the LaTeX Project Public License v1.3c or later
38 See http://www.latex-project.org/lppl.txt
39 ----------------------------------------------------------------
40
41 \endpreamble
42 \postamble
43
44 Copyright (C) 2012-2021 by Andrew Stacey <[email protected]>
45
46 This file may be distributed and/or modified under the conditions
47 of the LaTeX Project Public License, either version 1.3 of this
48 license or (at your option) any later version.
49 The latest version of this license is in:
50
51 http://www.latex-project.org/lppl.txt
52
53 and version 1.3 or later is part of all distributions of LaTeX
54 version 2005/12/01 or later.
55
56 This work is "maintained" (as per LPPL maintenance status) by
57 Andrew Stacey.
58
59 This work consists of the files hobby_code.dtx
60 hobby.tex
61 and the derived files hobby.code.tex
62 pgflibraryhobby.code.tex
63 tikzlibraryhobby.code.tex
64 pml3array.sty
65 hobby-l3draw.sty
66 hobby.ins
67 hobby.pdf
68 hobby_code.pdf
69 README.txt
70
71 \endpostamble
72 \usedir{tex/latex/hobby}
73 \generate{\file{tikzlibraryhobby.code.tex} {\from{hobby_code.dtx}{tikzlibrary}}}
74 \generate{\file{pgflibraryhobby.code.tex} {\from{hobby_code.dtx}{pgflibrary}}}
75 \generate{\file{hobby.code.tex} {\from{hobby_code.dtx}{hobby}}}
76 \generate{\file{pml3array.sty} {\from{hobby_code.dtx}{array}}}
77 \generate{\file{hobby-l3draw.sty} {\from{hobby_code.dtx}{l3hobby}}}
78 %</install>
79 %<install>\endbatchfile
80 %<*internal>
81 \usedir{source/latex/hobby}
82 \generate{
83 \file{\jobname.ins}{\from{\jobname.dtx}{install}}
84 }
85 \nopreamble\nopostamble
86 \generate{
87 \file{README.txt}{\from{\jobname.dtx}{readme}}
88 }
89 \ifx\fmtname\nameofplainTeX
90 \expandafter\endbatchfile
91 \else
92 \expandafter\endgroup
93 \fi
94 %</internal>
95 %<*driver>
96 \documentclass[full]{l3doc}
97 \usepackage[T1]{fontenc}
98 \usepackage{csquotes}
99 \usepackage{lmodern}
100 \usepackage{tikz}
101 \usepackage{amsmath}
102 \usetikzlibrary{hobby,decorations.pathreplacing}
103 \usepackage[margin=3cm]{geometry}
104 \EnableCrossrefs
105 \CodelineIndex
106 \RecordChanges
107 \addtolength{\hoffset}{.4in}
108 \begin{document}
109 \DocInput{\jobname.dtx}
110 \end{document}
111 %</driver>
112 % \fi
113 %
114 % \CheckSum{3427}
115 %
116 % \CharacterTable
117 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
118 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
119 % Digits \0\1\2\3\4\5\6\7\8\9
120 % Exclamation \! Double quote \" Hash (number) \#
121 % Dollar \$ Percent \% Ampersand \&
122 % Acute accent \' Left paren \( Right paren \)
123 % Asterisk \* Plus \+ Comma \,
124 % Minus \- Point \. Solidus \/
125 % Colon \: Semicolon \; Less than \<
126 % Equals \= Greater than \> Question mark \?
127 % Commercial at \@ Left bracket \[ Backslash \\
128 % Right bracket \] Circumflex \^ Underscore \_
129 % Grave accent \` Left brace \{ Vertical bar \|
130 % Right brace \} Tilde \~}
131 %
132 %
133 %
134 % \DoNotIndex{\newcommand,\newenvironment}
135 %
136 % \providecommand*{\url}{\texttt}
137 % \title{The \textsf{Hobby} package: code}
138 % \author{Andrew Stacey \\ \url{[email protected]}}
139 % \date{\hobbyVersion\ from\ \hobbyDate}
140 % \maketitle
141 %
142 %
143 % \StopEventually{\PrintChanges}
144 % \section{Implementation}
145 %
146 % \subsection{Main Code}
147 %
148 % \iffalse
149 %<*hobby>
150 % \fi
151 %
152 % We use \LaTeX3 syntax so need to load the requisite packages
153 % \begin{macrocode}
154 \RequirePackage{pml3array}
155 \ExplSyntaxOn
156 % \end{macrocode}
157 %
158 % \begin{macrocode}
159 \cs_generate_variant:Nn \fp_set:Nn {Nx}
160 \cs_generate_variant:Nn \tl_if_eq:nnTF {VnTF}
161 \cs_generate_variant:Nn \tl_if_eq:nnTF {xnTF}
162 % \end{macrocode}
163 %
164 % \subsubsection{Initialisation}
165 %
166 % We declare all our variables.
167 %
168 % Start with version and date, together with a check to see if we've been loaded twice (fail gracefully if so).
169 %
170 % \begin{macrocode}
171 \tl_clear:N \l_tmpa_tl
172 \tl_if_exist:NT \g__hobby_version
173 {
174 \tl_set:Nn \l_tmpa_tl {
175 \ExplSyntaxOff
176 \tl_clear:N \l_tmpa_tl
177 \endinput
178 }
179 }
180 \tl_use:N \l_tmpa_tl
181
182 \tl_new:N \g__hobby_version
183 \tl_new:N \g__hobby_date
184 \tl_gset:Nn \g__hobby_version {1.12}
185 \tl_gset:Nn \g__hobby_date {2023-09-01}
186 \DeclareDocumentCommand \hobbyVersion {}
187 {
188 \tl_use:N \g__hobby_version
189 }
190 \DeclareDocumentCommand \hobbyDate {}
191 {
192 \tl_use:N \g__hobby_date
193 }
194 % \end{macrocode}
195 %
196 % The function for computing the lengths of the control points depends on three parameters.
197 % These are set to \(a = \sqrt{2}\), \(b = 1/16\), and \(c = \frac{3 - \sqrt{5}}{2}\).
198 % \begin{macrocode}
199 \fp_new:N \g_hobby_parama_fp
200 \fp_new:N \g_hobby_paramb_fp
201 \fp_new:N \g_hobby_paramc_fp
202 \fp_gset:Nn \g_hobby_parama_fp {2^.5}
203 \fp_gset:Nn \g_hobby_paramb_fp {1/16}
204 \fp_gset:Nn \g_hobby_paramc_fp {(3-5^.5)/2}
205 % \end{macrocode}
206 %
207 % Now we define our objects for use in generating the path.
208 %
209 % \begin{macro}{\l_hobby_closed_bool}
210 % \Verb+\l_hobby_closed_bool+ is \Verb+true+ if the path is closed.
211 % \begin{macrocode}
212 \bool_new:N \l_hobby_closed_bool
213 % \end{macrocode}
214 % \end{macro}
215 %
216 % \begin{macro}{\l_hobby_disjoint_bool}
217 % \Verb+\l_hobby_disjoint_bool+ is \Verb+true+ if the path should start with a \Verb+moveto+ command.
218 % \begin{macrocode}
219 \bool_new:N \l_hobby_disjoint_bool
220 % \end{macrocode}
221 % \end{macro}
222 %
223 % \begin{macro}{\l_hobby_save_aux_bool}
224 % \Verb+\l_hobby_save_aux_bool+ is \Verb+true+ if when saving paths then they should be saved to the \Verb+aux+ file.
225 % \begin{macrocode}
226 \bool_new:N \l_hobby_save_aux_bool
227 \bool_set_true:N \l_hobby_save_aux_bool
228 \DeclareDocumentCommand \HobbyDisableAux {}
229 {
230 \bool_set_false:N \l_hobby_save_aux_bool
231 }
232 % \end{macrocode}
233 % \end{macro}
234 %
235 % \begin{macro}{\g__hobby_points_array}
236 % \Verb+\g__hobby_points_array+ is an array holding the specified points on the path.
237 % In the \LaTeX3 code, a ``point'' is a token list of the form \Verb+<number>, <number>+.
238 % This gives us the greatest flexibility in passing points back and forth between the \LaTeX3 code and any calling code.
239 % The array is indexed by integers beginning with \(0\).
240 % In the documentation, we will use the notation \(z_k\) to refer to the \(k\)th point.
241 % \begin{macrocode}
242 \array_new:N \g__hobby_points_array
243 % \end{macrocode}
244 % \end{macro}
245 %
246 % \begin{macro}{\g__hobby_points_x_array}
247 % \Verb+\g__hobby_points_x_array+ is an array holding the \(x\)--{}coordinates of the specified points.
248 % \begin{macrocode}
249 \array_new:N \g__hobby_points_x_array
250 % \end{macrocode}
251 % \end{macro}
252 %
253 % \begin{macro}{\l_hobby_points_y_array}
254 % \Verb+\g__hobby_points_y_array+ is an array holding the \(y\)--{}coordinates of the specified points.
255 % \begin{macrocode}
256 \array_new:N \g__hobby_points_y_array
257 % \end{macrocode}
258 % \end{macro}
259 %
260 % \begin{macro}{\g__hobby_actions_array}
261 % \Verb+\g__hobby_actions_array+ is an array holding the (encoded) action to be taken out on the segment of the path ending at that point.
262 % \begin{macrocode}
263 \array_new:N \g__hobby_actions_array
264 % \end{macrocode}
265 % \end{macro}
266 %
267 % \begin{macro}{\g__hobby_angles_array}
268 % \Verb+\g__hobby_angles_array+ is an array holding the angles of the lines between the points.
269 % Specifically, the angle indexed by \(k\) is the angle in radians of the line from \(z_k\) to \(z_{k+1}\).
270 % \begin{macrocode}
271 \array_new:N \g__hobby_angles_array
272 % \end{macrocode}
273 % \end{macro}
274 %
275 % \begin{macro}{\g__hobby_distances_array}
276 % \Verb+\g__hobby_distances_array+ is an array holding the distances between the points.
277 % Specifically, the distance indexed by \(k\), which we will write as \(d_k\), is the length of the line from \(z_k\) to \(z_{k+1}\).
278 % \begin{macrocode}
279 \array_new:N \g__hobby_distances_array
280 % \end{macrocode}
281 % \end{macro}
282 %
283 % \begin{macro}{\g__hobby_tension_out_array}
284 % \Verb+\g__hobby_tension_out_array+ is an array holding the tension for the path as it leaves each point.
285 % This is a parameter that controls how much the curve ``flexes'' as it leaves the point.
286 % In the following, this will be written \(\tau_k\).
287 % \begin{macrocode}
288 \array_new:N \g__hobby_tension_out_array
289 % \end{macrocode}
290 % \end{macro}
291 %
292 % \begin{macro}{\g__hobby_tension_in_array}
293 % \Verb+\g__hobby_tension_in_array+ is an array holding the tension for the path as it arrives at each point.
294 % This is a parameter that controls how much the curve ``flexes'' as it gets to the point.
295 % In the following, this will be written \(\overline{\tau}_k\).
296 % \begin{macrocode}
297 \array_new:N \g__hobby_tension_in_array
298 % \end{macrocode}
299 % \end{macro}
300 %
301 % \begin{macro}{\g__hobby_matrix_a_array}
302 % \Verb+\g__hobby_matrix_a_array+ is an array holding the subdiagonal of the linear system that has to be solved to find the angles of the control points.
303 % In the following, this will be denoted by \(A_i\).
304 % The first index is \(1\).
305 % \begin{macrocode}
306 \array_new:N \g__hobby_matrix_a_array
307 % \end{macrocode}
308 % \end{macro}
309 %
310 % \begin{macro}{\g__hobby_matrix_b_array}
311 % \Verb+\g__hobby_matrix_b_array+ is an array holding the diagonal of the linear system that has to be solved to find the angles of the control points.
312 % In the following, this will be denoted by \(B_i\).
313 % The first index is \(0\).
314 % \begin{macrocode}
315 \array_new:N \g__hobby_matrix_b_array
316 % \end{macrocode}
317 % \end{macro}
318 %
319 % \begin{macro}{\g__hobby_matrix_c_array}
320 % \Verb+\g__hobby_matrix_c_array+ is an array holding the superdiagonal of the linear system that has to be solved to find the angles of the control points.
321 % In the following, this will be denoted by \(C_i\).
322 % The first index is \(0\).
323 % \begin{macrocode}
324 \array_new:N \g__hobby_matrix_c_array
325 % \end{macrocode}
326 % \end{macro}
327 %
328 % \begin{macro}{\g__hobby_matrix_d_array}
329 % \Verb+\g__hobby_matrix_d_array+ is an array holding the target vector of the linear system that has to be solved to find the angles of the control points.
330 % In the following, this will be denoted by \(D_i\).
331 % The first index is \(1\).
332 % \begin{macrocode}
333 \array_new:N \g__hobby_matrix_d_array
334 % \end{macrocode}
335 % \end{macro}
336 %
337 % \begin{macro}{\g__hobby_vector_u_array}
338 % \Verb+\g__hobby_vector_u_array+ is an array holding the perturbation of the linear system for closed paths.
339 % The coefficient matrix for an \emph{open} path is tridiagonal and that means that Gaussian elimination runs faster than expected (\(O(n)\) instead of \(O(n^3)\)).
340 % The matrix for a closed path is not tridiagonal but is not far off.
341 % It can be solved by perturbing it to a tridiagonal matrix and then modifying the result.
342 % This array represents a utility vector in that perturbation.
343 % In the following, the vector will be denoted by \(u\).
344 % The first index is \(1\).
345 % \begin{macrocode}
346 \array_new:N \g__hobby_vector_u_array
347 % \end{macrocode}
348 % \end{macro}
349 %
350 % \begin{macro}{\g__hobby_excess_angle_array}
351 % \Verb+\g__hobby_excess_angle_array+ is an array that allows the user to say that the algorithm should add a multiple of \(2 \pi\) to the angle differences.
352 % This is because these angles are wrapped to the interval \((-\pi,\pi]\) but the wrapping might go wrong near the end points due to computation accuracy.
353 % The first index is \(1\).
354 % \begin{macrocode}
355 \array_new:N \g__hobby_excess_angle_array
356 % \end{macrocode}
357 % \end{macro}
358 %
359 % \begin{macro}{\g__hobby_psi_array}
360 % \Verb+\g__hobby_psi_array+ is an array holding the difference of the angles of the lines entering and exiting a point.
361 % That is, \(\psi_k\) is the angle between the lines joining \(z_k\) to \(z_{k-1}\) and \(z_{k+1}\).
362 % The first index is \(1\).
363 % \begin{macrocode}
364 \array_new:N \g__hobby_psi_array
365 % \end{macrocode}
366 % \end{macro}
367 %
368 % \begin{macro}{\g__hobby_theta_array}
369 % \Verb+\g__hobby_theta_array+ is an array holding the angles of the outgoing control points for the generated path.
370 % These are measured relative to the line joining the point to the next point on the path.
371 % The first index is \(0\).
372 % \begin{macrocode}
373 \array_new:N \g__hobby_theta_array
374 % \end{macrocode}
375 % \end{macro}
376 %
377 % \begin{macro}{\g__hobby_phi_array}
378 % \Verb+\g__hobby_phi_array+ is an array holding the angles of the incoming control points for the generated path.
379 % These are measured relative to the line joining the point to the previous point on the path.
380 % The first index is \(1\).
381 % \begin{macrocode}
382 \array_new:N \g__hobby_phi_array
383 % \end{macrocode}
384 % \end{macro}
385 %
386 % \begin{macro}{\g__hobby_sigma_array}
387 % \Verb+\g__hobby_sigma_array+ is an array holding the lengths of the outgoing control points for the generated path.
388 % The units are such that the length of the line to the next specified point is one unit.
389 % \begin{macrocode}
390 \array_new:N \g__hobby_sigma_array
391 % \end{macrocode}
392 % \end{macro}
393 %
394 % \begin{macro}{\g__hobby_rho_array}
395 % \Verb+\g__hobby_rho_array+ is an array holding the lengths of the incoming control points for the generated path.
396 % The units are such that the length of the line to the previous specified point is one unit.
397 % \begin{macrocode}
398 \array_new:N \g__hobby_rho_array
399 % \end{macrocode}
400 % \end{macro}
401 %
402 % \begin{macro}{\g__hobby_controla_array}
403 % \Verb+\g__hobby_controla_array+ is an array holding the coordinates of the first control points on the curves.
404 % The format is the same as for \Verb+\g__hobby_points_array+.
405 % \begin{macrocode}
406 \array_new:N \g__hobby_controla_array
407 % \end{macrocode}
408 % \end{macro}
409 %
410 % \begin{macro}{\g__hobby_controlb_array}
411 % \Verb+\g__hobby_controlb_array+ is an array holding the coordinates of the second control points on the curves.
412 % The format is the same as for \Verb+\g__hobby_points_array+.
413 % \begin{macrocode}
414 \array_new:N \g__hobby_controlb_array
415 % \end{macrocode}
416 % \end{macro}
417 %
418 % \begin{macro}{\l_hobby_matrix_v_fp}
419 % \Verb+\l_hobby_matrix_v_fp+ is a number which is used when doing the perturbation of the solution of the linear system for a closed curve.
420 % There is actually a vector, \(v\), that this corresponds to but that vector only has one component that needs computation.
421 % \begin{macrocode}
422 \fp_new:N \l_hobby_matrix_v_fp
423 % \end{macrocode}
424 % \end{macro}
425 %
426 % \begin{macro}{\l_hobby_tempa_tl}
427 % \Verb+\l_hobby_tempa_tl+ is a temporary variable of type \Verb+tl+.
428 % \begin{macrocode}
429 \fp_new:N \l_hobby_tempa_tl
430 % \end{macrocode}
431 % \end{macro}
432 %
433 % \begin{macro}{\l_hobby_tempb_tl}
434 % \Verb+\l_hobby_tempb_tl+ is a temporary variable of type \Verb+tl+.
435 % \begin{macrocode}
436 \fp_new:N \l_hobby_tempb_tl
437 % \end{macrocode}
438 % \end{macro}
439 %
440 % \begin{macro}{\l_hobby_tempa_fp}
441 % \Verb+\l_hobby_tempa_fp+ is a temporary variable of type \Verb+fp+.
442 % \begin{macrocode}
443 \fp_new:N \l_hobby_tempa_fp
444 % \end{macrocode}
445 % \end{macro}
446 %
447 % \begin{macro}{\l_hobby_tempb_fp}
448 % \Verb+\l_hobby_tempb_fp+ is a temporary variable of type \Verb+fp+.
449 % \begin{macrocode}
450 \fp_new:N \l_hobby_tempb_fp
451 % \end{macrocode}
452 % \end{macro}
453 %
454 % \begin{macro}{\l_hobby_tempc_fp}
455 % \Verb+\l_hobby_tempc_fp+ is a temporary variable of type \Verb+fp+.
456 % \begin{macrocode}
457 \fp_new:N \l_hobby_tempc_fp
458 % \end{macrocode}
459 % \end{macro}
460 %
461 % \begin{macro}{\l_hobby_tempd_fp}
462 % \Verb+\l_hobby_tempd_fp+ is a temporary variable of type \Verb+fp+.
463 % \begin{macrocode}
464 \fp_new:N \l_hobby_tempd_fp
465 % \end{macrocode}
466 % \end{macro}
467 %
468 % \begin{macro}{\l_hobby_temps_fp}
469 % \Verb+\l_hobby_temps_fp+ is a temporary variable of type \Verb+fp+.
470 % \begin{macrocode}
471 \fp_new:N \l_hobby_temps_fp
472 % \end{macrocode}
473 % \end{macro}
474 %
475 % \begin{macro}{\g__hobby_in_curl_fp}
476 % \Verb+\g__hobby_in_curl_fp+ is the ``curl'' at the end of an open path.
477 % This is used if the angle at the end is not specified.
478 % \begin{macrocode}
479 \fp_new:N \g__hobby_in_curl_fp
480 \fp_gset:Nn \g__hobby_in_curl_fp {1}
481 % \end{macrocode}
482 % \end{macro}
483 %
484 % \begin{macro}{\g__hobby_out_curl_fp}
485 % \Verb+\g__hobby_out_curl_fp+ is the ``curl'' at the start of an open path.
486 % This is used if the angle at the start is not specified.
487 % \begin{macrocode}
488 \fp_new:N \g__hobby_out_curl_fp
489 \fp_gset:Nn \g__hobby_out_curl_fp {1}
490 % \end{macrocode}
491 % \end{macro}
492 %
493 % \begin{macro}{\g__hobby_in_angle_fp}
494 % \Verb+\g__hobby_in_angle_fp+ is the angle at the end of an open path.
495 % If this is not specified, it will be computed automatically.
496 % It is set to \Verb+\c_inf_fp+ to allow easy detection of when it has been specified.
497 % \begin{macrocode}
498 \fp_new:N \g__hobby_in_angle_fp
499 \fp_gset_eq:NN \g__hobby_in_angle_fp \c_inf_fp
500 % \end{macrocode}
501 % \end{macro}
502 %
503 % \begin{macro}{\g__hobby_out_angle_fp}
504 % \Verb+\g__hobby_out_angle_fp+ is the angle at the start of an open path.
505 % If this is not specified, it will be computed automatically.
506 % It is set to \Verb+\c_inf_fp+ to allow easy detection of when it has been specified.
507 % \begin{macrocode}
508 \fp_new:N \g__hobby_out_angle_fp
509 \fp_gset_eq:NN \g__hobby_out_angle_fp \c_inf_fp
510 % \end{macrocode}
511 % \end{macro}
512 %
513 % \begin{macro}{\g__hobby_npoints_int}
514 % \Verb+\g__hobby_npoints_int+ is one less than the number of points on the curve.
515 % As our list of points starts at \(0\), this is the index of the last point.
516 % In the algorithm for a closed curve, some points are repeated whereupon this is incremented so that it is always the index of the last point.
517 % \begin{macrocode}
518 \int_new:N \g__hobby_npoints_int
519 % \end{macrocode}
520 % \end{macro}
521 %
522 % \begin{macro}{\g__hobby_draw_int}
523 % \begin{macrocode}
524 \int_new:N \g__hobby_draw_int
525 % \end{macrocode}
526 % \end{macro}
527 %
528 % A ``point'' is a key-value list setting the x-value, the y-value, and the tensions at that point.
529 % Using keys makes it easier to pass points from the algorithm code to the calling code and vice versa without either knowing too much about the other.
530 % \begin{macrocode}
531 \keys_define:nn {hobby / read in all} {
532 point .code:n = {
533 \fp_set:Nn \l_hobby_tempa_fp {\clist_item:nn {#1} {1}}
534 \fp_set:Nn \l_hobby_tempb_fp {\clist_item:nn {#1} {2}}
535 },
536 tension~out .fp_set:N = \l_hobby_tempc_fp,
537 tension~in .fp_set:N = \l_hobby_tempd_fp,
538 excess~angle .fp_set:N = \l_hobby_temps_fp,
539 break .tl_set:N = \l_tmpb_tl,
540 blank .tl_set:N = \l_tmpa_tl,
541 tension .meta:n = { tension~out=#1, tension~in=#1 },
542 break .default:n = false,
543 blank .default:n = false,
544 invert~soft~blanks .choice:,
545 invert~soft~blanks / true .code:n = {
546 \int_gset:Nn \g__hobby_draw_int {0}
547 },
548 invert~soft~blanks / false .code:n = {
549 \int_gset:Nn \g__hobby_draw_int {1}
550 },
551 invert~soft~blanks .default:n = true,
552 tension~out .default:n = 1,
553 tension~in .default:n = 1,
554 excess~angle .default:n = 0,
555 in~angle .fp_gset:N = \g__hobby_in_angle_fp,
556 out~angle .fp_gset:N = \g__hobby_out_angle_fp,
557 in~curl .fp_gset:N = \g__hobby_in_curl_fp,
558 out~curl .fp_gset:N = \g__hobby_out_curl_fp,
559 closed .bool_gset:N = \g__hobby_closed_bool,
560 closed .default:n = true,
561 disjoint .bool_gset:N = \g__hobby_disjoint_bool,
562 disjoint .default:n = true,
563 break~default .code:n = {
564 \keys_define:nn { hobby / read in all }
565 {
566 break .default:n = #1
567 }
568 },
569 blank~default .code:n = {
570 \keys_define:nn { hobby / read in all }
571 {
572 blank .default:n = #1
573 }
574 },
575 }
576 % \end{macrocode}
577 % There are certain other parameters that can be set for a given curve.
578 % \begin{macrocode}
579 \keys_define:nn { hobby / read in params} {
580 in~angle .fp_gset:N = \g__hobby_in_angle_fp,
581 out~angle .fp_gset:N = \g__hobby_out_angle_fp,
582 in~curl .fp_gset:N = \g__hobby_in_curl_fp,
583 out~curl .fp_gset:N = \g__hobby_out_curl_fp,
584 closed .bool_gset:N = \g__hobby_closed_bool,
585 closed .default:n = true,
586 disjoint .bool_gset:N = \g__hobby_disjoint_bool,
587 disjoint .default:n = true,
588 break~default .code:n = {
589 \keys_define:nn { hobby / read in all }
590 {
591 break .default:n = #1
592 }
593 },
594 blank~default .code:n = {
595 \keys_define:nn { hobby / read in all }
596 {
597 blank .default:n = #1
598 }
599 },
600 invert~soft~blanks .choice:,
601 invert~soft~blanks / true .code:n = {
602 \int_gset:Nn \g__hobby_draw_int {0}
603 },
604 invert~soft~blanks / false .code:n = {
605 \int_gset:Nn \g__hobby_draw_int {1}
606 },
607 invert~soft~blanks .default:n = true,
608 }
609 % \end{macrocode}
610 % \begin{macro}{\hobby_distangle:n}
611 % Computes the distance and angle between successive points.
612 % The argument given is the index of the current point.
613 % Assumptions: the points are in \Verb+\g__hobby_points_x_array+ and \Verb+\g__hobby_points_y_array+ and the index of the last point is \Verb+\g__hobby_npoints_int+.
614 % \begin{macrocode}
615 \cs_set:Nn \hobby_distangle:n {
616 \fp_set:Nn \l_hobby_tempa_fp {
617 (\array_get:Nn \g__hobby_points_x_array {#1 + 1})
618 - (\array_get:Nn \g__hobby_points_x_array {#1})}
619
620 \fp_set:Nn \l_hobby_tempb_fp {
621 (\array_get:Nn \g__hobby_points_y_array {#1 + 1})
622 - (\array_get:Nn \g__hobby_points_y_array {#1})}
623
624 \fp_set:Nn \l_hobby_tempc_fp { atan ( \l_hobby_tempb_fp, \l_hobby_tempa_fp ) }
625 \fp_veclen:NVV \l_hobby_tempd_fp \l_hobby_tempa_fp \l_hobby_tempb_fp
626
627 \array_gpush:Nx \g__hobby_angles_array {\fp_to_tl:N \l_hobby_tempc_fp}
628 \array_gpush:Nx \g__hobby_distances_array {\fp_to_tl:N \l_hobby_tempd_fp}
629 }
630 % \end{macrocode}
631 % \end{macro}
632 %
633 % \begin{macro}{\fp_veclen:NVV}
634 % Computes the length of the vector specified by the latter two arguments, storing the answer in the first.
635 % \begin{macrocode}
636 \cs_new:Nn \fp_veclen:Nnn {
637 \fp_set:Nn #1 {((#2)^2 + (#3)^2)^.5}
638 }
639 \cs_generate_variant:Nn \fp_veclen:Nnn {NVV}
640 % \end{macrocode}
641 % \end{macro}
642 %
643 % \begin{macro}{\hobby_ctrllen:Nnn}
644 % Computes the length of the control point vector from the two angles, storing the answer in the first argument given.
645 % \begin{macrocode}
646 \cs_new:Nn \hobby_ctrllen:Nnn {
647 \fp_set:Nn #1 {(2 - \g_hobby_parama_fp
648 * ( sin(#2) - \g_hobby_paramb_fp * sin(#3) )
649 * ( sin(#3) - \g_hobby_paramb_fp * sin(#2) )
650 * ( cos(#2) - cos(#3) ) )
651 / ( 1 + (1 - \g_hobby_paramc_fp) * cos(#3) + \g_hobby_paramc_fp * cos(#2))}
652 }
653 \cs_generate_variant:Nn \hobby_ctrllen:Nnn {NVV}
654 % \end{macrocode}
655 % \end{macro}
656 %
657 % \begin{macro}{\hobby_append_point_copy:n}
658 % This function adds a copy of the point (numbered by its argument) to the end of the list of points, copying all the relevant data (coordinates, tension, etc.).
659 %
660 % Originally from Bruno Le Foch on TeX-SX.
661 % \begin{macrocode}
662 \cs_new_protected:Npn \hobby_append_point_copy:n #1
663 {
664 \hobby_append_point_copy_aux:Nn \g__hobby_points_array {#1}
665 \hobby_append_point_copy_aux:Nn \g__hobby_points_x_array {#1}
666 \hobby_append_point_copy_aux:Nn \g__hobby_points_y_array {#1}
667 \hobby_append_point_copy_aux:Nn \g__hobby_tension_in_array {#1}
668 \hobby_append_point_copy_aux:Nn \g__hobby_tension_out_array {#1}
669 \hobby_append_point_copy_aux:Nn \g__hobby_excess_angle_array {#1}
670 \hobby_append_point_copy_aux:Nn \g__hobby_actions_array {#1}
671 }
672 \cs_new_protected:Npn \hobby_append_point_copy_aux:Nn #1#2
673 { \array_gpush:Nx #1 { \array_get:Nn #1 {#2} } }
674 % \end{macrocode}
675 % \end{macro}
676 %
677 % \begin{macro}{\hobby_gen_path:}
678 % This is the curve generation function.
679 % We assume at the start that we have an array containing all the points that the curve must go through, and the various curve parameters have been initialised.
680 % So these must be set up by a wrapper function which then calls this one.
681 % The list of required information is:
682 % \begin{enumerate}
683 % \item \Verb+\g__hobby_points_x_array+
684 % \item \Verb+\g__hobby_points_y_array+
685 % \item \Verb+\g__hobby_tension_out_array+
686 % \item \Verb+\g__hobby_tension_in_array+
687 % \item \Verb+\g__hobby_excess_angle_array+
688 % \item \Verb+\g__hobby_in_curl_fp+
689 % \item \Verb+\g__hobby_out_curl_fp+
690 % \item \Verb+\g__hobby_in_angle_fp+
691 % \item \Verb+\g__hobby_out_angle_fp+
692 % \item \Verb+\g__hobby_closed_bool+
693 % \item \Verb+\g__hobby_actions_array+
694 % \end{enumerate}
695 %
696 % \begin{macrocode}
697 \cs_new:Nn \hobby_gen_path:
698 {
699 % \end{macrocode}
700 % For much of the time, we can pretend that a closed path is the same as an open path.
701 % To do this, we need to make the end node an internal node by repeating the \(z_1\) node as the \(z_{n+1}\)th node.
702 % We also check that the last (\(z_n\)) and first (\(z_0\)) nodes are the same, otherwise we repeat the \(z_0\) node as well.
703 % \begin{macrocode}
704 \bool_if:NT \g__hobby_closed_bool {
705 % \end{macrocode}
706 % Are the \(x\)-values of the first and last points different?
707 % \begin{macrocode}
708 \fp_compare:nTF {(\array_get:Nn \g__hobby_points_x_array {0})
709 =
710 (\array_top:N \g__hobby_points_x_array)}
711 {
712 % \end{macrocode}
713 % No, so compare the \(y\)-values.
714 % Are the \(y\)-values of the first and last points different?
715 % \begin{macrocode}
716 \fp_compare:nF {
717 \array_get:Nn \g__hobby_points_y_array {0}
718 =
719 \array_top:N \g__hobby_points_y_array
720 }
721 {
722 % \end{macrocode}
723 % Yes, so we need to duplicate the first point, with all of its data.
724 % \begin{macrocode}
725 \hobby_append_point_copy:n {0}
726 }
727 }
728 {
729 % \end{macrocode}
730 % Yes, so we need to duplicate the first point, with all of its data.
731 % \begin{macrocode}
732 \hobby_append_point_copy:n {0}
733 }
734 % \end{macrocode}
735 % Now that we are sure that the first and last points are identical, we need to duplicate the first-but-one point (and all of its data).
736 % \begin{macrocode}
737 \hobby_append_point_copy:n {1}
738 }
739 % \end{macrocode}
740 %
741 % Set \Verb+\g__hobby_npoints_int+ to the number of points (minus one).
742 % \begin{macrocode}
743 \int_gset:Nn \g__hobby_npoints_int {\array_length:N \g__hobby_points_y_array}
744 % \end{macrocode}
745 % At this point, we need to decide what to do.
746 % This will depend on whether we have any intermediate points.
747 % \begin{macrocode}
748 \int_compare:nNnTF {\g__hobby_npoints_int} = {0} {
749 % \end{macrocode}
750 % Only one point, do nothing
751 % \begin{macrocode}
752 }
753 {
754 \int_compare:nNnTF {\g__hobby_npoints_int} = {1} {
755 % \end{macrocode}
756 % Only two points, skip processing.
757 % Just need to set the incoming and outgoing angles
758 % \begin{macrocode}
759 \hobby_distangle:n {0}
760 \fp_compare:nF { \g__hobby_out_angle_fp == \c_inf_fp }
761 {
762 \fp_set:Nn \l_hobby_tempa_fp { \g__hobby_out_angle_fp
763 - \array_get:Nn \g__hobby_angles_array {0}}
764 % \end{macrocode}
765 % We want to ensure that these angles lie in the range \((-\pi,\pi]\).
766 % So if the angle is bigger than \(\pi\), we subtract \(2 \pi\).
767 % (It shouldn't be that we can get bigger than \(3 \pi\) - check this)
768 % \begin{macrocode}
769 \fp_compare:nT {\l_hobby_tempa_fp > \c_pi_fp }
770 {
771 \fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
772 }
773 % \end{macrocode}
774 % Similarly, we check to see if the angle is less than \(-\pi\).
775 % \begin{macrocode}
776 \fp_compare:nT {\l_hobby_tempa_fp < -\c_pi_fp }
777 {
778 \fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
779 }
780 \array_gput:Nnx \g__hobby_theta_array {0} {\fp_to_tl:N \l_hobby_tempa_fp}
781 \fp_compare:nT { \g__hobby_in_angle_fp == \c_inf_fp }
782 {
783 %^^A \fp_mul:Nn \l_hobby_tempa_fp {-1}
784 \array_gput:Nnx \g__hobby_phi_array {1}{ \fp_to_tl:N \l_hobby_tempa_fp}
785 }
786 }
787 \fp_compare:nTF { \g__hobby_in_angle_fp == \c_inf_fp }
788 {
789 \fp_compare:nT { \g__hobby_out_angle_fp == \c_inf_fp }
790 {
791 \array_gput:Nnx \g__hobby_phi_array {1} {0}
792 \array_gput:Nnx \g__hobby_theta_array {0} {0}
793 }
794 }
795 {
796 \fp_set:Nn \l_hobby_tempa_fp { - \g__hobby_in_angle_fp + \c_pi_fp
797 + (\array_get:Nn \g__hobby_angles_array {0})}
798 \fp_compare:nT {\l_hobby_tempa_fp > \c_pi_fp }
799 {
800 \fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
801 }
802 \fp_compare:nT {\l_hobby_tempa_fp < -\c_pi_fp }
803 {
804 \fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
805 }
806
807 \array_gput:Nnx \g__hobby_phi_array {1}
808 {\fp_to_tl:N \l_hobby_tempa_fp}
809 \fp_compare:nT { \g__hobby_out_angle_fp == \c_inf_fp }
810 {
811 %^^A \fp_mul:Nn \l_hobby_tempa_fp {-1}
812 \array_gput:Nnx \g__hobby_theta_array {0}{ \fp_to_tl:N \l_hobby_tempa_fp}
813 }
814 }
815
816 }
817 {
818 % \end{macrocode}
819 % Got enough points, go on with processing
820 % \begin{macrocode}
821 \hobby_compute_path:
822 }
823 \hobby_build_path:
824 }
825 }
826 % \end{macrocode}
827 % \end{macro}
828 %
829 %
830 % \begin{macro}{\hobby_compute_path:}
831 % This is the path builder where we have enough points to run the algorithm.
832 % \begin{macrocode}
833 \cs_new:Nn \hobby_compute_path:
834 {
835 % \end{macrocode}
836 % Our first step is to go through the list of points and compute the distances and angles between successive points.
837 % Thus \(d_i\) is the distance from \(z_i\) to \(z_{i+1}\) and the angle is the angle of the line from \(z_i\) to \(z_{i+1}\).
838 % \begin{macrocode}
839 \int_step_function:nnnN {0} {1} {\g__hobby_npoints_int - 1} \hobby_distangle:n
840 % \end{macrocode}
841 %
842 % For the majority of the code, we're only really interested in the differences of the angles.
843 % So for each internal point we compute the differences in the angles.
844 % \begin{macrocode}
845 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
846 \fp_set:Nx \l_hobby_tempa_fp {
847 \array_get:Nn \g__hobby_angles_array {##1}
848 - \array_get:Nn \g__hobby_angles_array {##1 - 1}}
849 % \end{macrocode}
850 % We want to ensure that these angles lie in the range \((-\pi,\pi]\).
851 % So if the angle is bigger than \(\pi\), we subtract \(2 \pi\).
852 % (It shouldn't be that we can get bigger than \(3 \pi\) - check this.)
853 % \begin{macrocode}
854 \fp_compare:nTF {\l_hobby_tempa_fp > \c_pi_fp }
855 {
856 \fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
857 }
858 {}
859 % \end{macrocode}
860 % Similarly, we check to see if the angle is less than \(-\pi\).
861 % \begin{macrocode}
862 \fp_compare:nTF {\l_hobby_tempa_fp <= -\c_pi_fp }
863 {
864 \fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
865 }
866 {}
867 % \end{macrocode}
868 % The wrapping routine might not get it right at the edges so we add in the override.
869 % \begin{macrocode}
870 \array_get:NnNTF \g__hobby_excess_angle_array {##1} \l_tmpa_tl {
871 \fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp * \l_tmpa_tl}
872 }{}
873 % \end{macrocode}
874 % \begin{macrocode}
875 \array_gput:Nnx \g__hobby_psi_array {##1}{\fp_to_tl:N \l_hobby_tempa_fp}
876 }
877 % \end{macrocode}
878 %
879 % Next, we generate the matrix.
880 % We start with the subdiagonal.
881 % This is indexed from \(1\) to \(n-1\).
882 % \begin{macrocode}
883 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
884 \array_gput:Nnx \g__hobby_matrix_a_array {##1} {\fp_to_tl:n {
885 \array_get:Nn \g__hobby_tension_in_array {##1}^2
886 * \array_get:Nn \g__hobby_distances_array {##1}
887 * \array_get:Nn \g__hobby_tension_in_array {##1 + 1}
888 }}
889 }
890 % \end{macrocode}
891 %
892 % Next, we attack main diagonal.
893 % We might need to adjust the first and last terms, but we'll do that in a minute.
894 % \begin{macrocode}
895 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
896
897 \array_gput:Nnx \g__hobby_matrix_b_array {##1} {\fp_to_tl:n
898 {(3 * (\array_get:Nn \g__hobby_tension_in_array {##1 + 1}) - 1) *
899 (\array_get:Nn \g__hobby_tension_out_array {##1})^2 *
900 (\array_get:Nn \g__hobby_tension_out_array {##1 - 1})
901 * ( \array_get:Nn \g__hobby_distances_array {##1 - 1})
902 +
903 (3 * (\array_get:Nn \g__hobby_tension_out_array {##1 - 1}) - 1)
904 * (\array_get:Nn \g__hobby_tension_in_array {##1})^2
905 * (\array_get:Nn \g__hobby_tension_in_array {##1 + 1})
906 * (\array_get:Nn \g__hobby_distances_array {##1})}
907 }
908 }
909 % \end{macrocode}
910 %
911 % Next, the superdiagonal.
912 % \begin{macrocode}
913 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 2} {
914
915 \array_gput:Nnx \g__hobby_matrix_c_array {##1} {\fp_to_tl:n
916 {(\array_get:Nn \g__hobby_tension_in_array {##1})^2
917 * (\array_get:Nn \g__hobby_tension_in_array {##1 - 1})
918 * (\array_get:Nn \g__hobby_distances_array {##1 - 1})
919 }}
920
921 }
922 % \end{macrocode}
923 %
924 % Lastly (before the adjustments), the target vector.
925 % \begin{macrocode}
926 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 2} {
927
928 \array_gput:Nnx \g__hobby_matrix_d_array {##1} {\fp_to_tl:n
929 {
930 - (\array_get:Nn \g__hobby_psi_array {##1 + 1})
931 * (\array_get:Nn \g__hobby_tension_out_array {##1})^2
932 * (\array_get:Nn \g__hobby_tension_out_array {##1 - 1})
933 * (\array_get:Nn \g__hobby_distances_array {##1 - 1})
934 - (3 * (\array_get:Nn \g__hobby_tension_out_array {##1 - 1}) - 1)
935 * (\array_get:Nn \g__hobby_psi_array {##1})
936 * (\array_get:Nn \g__hobby_tension_in_array {##1})^2
937 * (\array_get:Nn \g__hobby_tension_in_array {##1 + 1})
938 * (\array_get:Nn \g__hobby_distances_array {##1})
939 }
940 }
941 }
942 % \end{macrocode}
943 %
944 % Next, there are some adjustments at the ends.
945 % These differ depending on whether the path is open or closed.
946 % \begin{macrocode}
947 \bool_if:NTF \g__hobby_closed_bool {
948 % \end{macrocode}
949 % Closed path
950 % \begin{macrocode}
951 \array_gput:Nnx \g__hobby_matrix_c_array {0} {\fp_to_tl:n {
952 - (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 2})
953 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2})
954 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^2
955 }}
956
957 \array_gput:Nnn \g__hobby_matrix_b_array {0} {1}
958 \array_gput:Nnn \g__hobby_matrix_d_array {0} {0}
959
960 \array_gput:Nnx \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
961 (\array_get:Nn \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1})
962 + 1
963 }}
964
965 \array_gput:Nnx \g__hobby_matrix_d_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
966 - (\array_get:Nn \g__hobby_psi_array {1})
967 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -1})^2
968 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -2})
969 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 2})
970 - (3 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2}) - 1)
971 * (\array_get:Nn \g__hobby_psi_array {\g__hobby_npoints_int - 1})
972 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int - 1})^2
973 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int})
974 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int -1})
975 }
976 }
977 % \end{macrocode}
978 % We also need to populate the \(u\)-vector
979 % \begin{macrocode}
980 \array_gput:Nnn \g__hobby_vector_u_array {0} {1}
981 \array_gput:Nnn \g__hobby_vector_u_array {\g__hobby_npoints_int - 1} {1}
982 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 2} {
983 \array_gput:Nnn \g__hobby_vector_u_array {##1} {0}
984 }
985 % \end{macrocode}
986 % And define the significant entry in the \(v\)-vector.
987 % \begin{macrocode}
988 \fp_set:Nn \l_hobby_matrix_v_fp {
989 (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -1})^2
990 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -2})
991 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int -2})
992 }
993 }
994 {
995 % \end{macrocode}
996 % Open path.
997 % First, we test to see if \(\theta_0\) has been specified.
998 % \begin{macrocode}
999 \fp_compare:nTF { \g__hobby_out_angle_fp == \c_inf_fp }
1000 {
1001 \array_gput:Nnx \g__hobby_matrix_b_array {0} {\fp_to_tl:n {
1002 (\array_get:Nn \g__hobby_tension_in_array {1})^3
1003 * \g__hobby_in_curl_fp
1004 +
1005 (3 * (\array_get:Nn \g__hobby_tension_in_array {1}) - 1)
1006 * (\array_get:Nn \g__hobby_tension_out_array {0})^3
1007 }}
1008
1009 \array_gput:Nnx \g__hobby_matrix_c_array {0} {\fp_to_tl:n {
1010 (\array_get:Nn \g__hobby_tension_out_array {0})^3
1011 +
1012 (3 * (\array_get:Nn \g__hobby_tension_out_array {0}) - 1)
1013 * (\array_get:Nn \g__hobby_tension_in_array {1})^3
1014 * \g__hobby_in_curl_fp
1015 }}
1016
1017 \array_gput:Nnx \g__hobby_matrix_d_array {0} {\fp_to_tl:n {
1018 -( (\array_get:Nn \g__hobby_tension_out_array {0})^3
1019 +
1020 (3 * (\array_get:Nn \g__hobby_tension_out_array {0}) - 1)
1021 * (\array_get:Nn \g__hobby_tension_in_array {1})^3
1022 * \g__hobby_in_curl_fp)
1023 * (\array_get:Nn \g__hobby_psi_array {1})
1024 }}
1025
1026 }
1027 {
1028 \array_gput:Nnn \g__hobby_matrix_b_array {0} {1}
1029 \array_gput:Nnn \g__hobby_matrix_c_array {0} {0}
1030 \fp_set:Nn \l_hobby_tempa_fp { \g__hobby_out_angle_fp
1031 - \array_get:Nn \g__hobby_angles_array {0}}
1032 % \end{macrocode}
1033 % We want to ensure that these angles lie in the range \((-\pi,\pi]\).
1034 % So if the angle is bigger than \(\pi\), we subtract \(2 \pi\).
1035 % (It shouldn't be that we can get bigger than \(3 \pi\) - check this)
1036 % \begin{macrocode}
1037 \fp_compare:nT {\l_hobby_tempa_fp > \c_pi_fp }
1038 {
1039 \fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
1040 }
1041 % \end{macrocode}
1042 % Similarly, we check to see if the angle is less than \(-\pi\).
1043 % \begin{macrocode}
1044 \fp_compare:nT {\l_hobby_tempa_fp < -\c_pi_fp }
1045 {
1046 \fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
1047 }
1048 \array_gput:Nnx \g__hobby_matrix_d_array {0} {\fp_to_tl:N \l_hobby_tempa_fp}
1049 }
1050 % \end{macrocode}
1051 %
1052 % Next, if \(\phi_n\) has been given.
1053 % \begin{macrocode}
1054 \fp_compare:nTF { \g__hobby_in_angle_fp == \c_inf_fp }
1055 {
1056
1057 \array_gput:Nnx \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
1058 \array_get:Nn \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1}
1059 - (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^2
1060 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2})
1061 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 2})
1062 *
1063 ((3 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int} ) - 1)
1064 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^3 \l_tmpa_tl
1065 * \g__hobby_out_curl_fp
1066 +
1067 (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int })^3)
1068 /
1069 ((3 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -2}) - 1)
1070 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int})^3
1071 +
1072 ( \array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^3
1073 * \g__hobby_out_curl_fp)
1074 }}
1075
1076 \array_gput:Nnx \g__hobby_matrix_d_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
1077 - (3 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2}) - 1)
1078 * (\array_get:Nn \g__hobby_psi_array {\g__hobby_npoints_int - 1})
1079 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int - 1})^2
1080 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int})
1081 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 1})
1082 }}
1083
1084 }
1085 {
1086 \fp_set:Nn \l_hobby_tempa_fp { - \g__hobby_in_angle_fp + \c_pi_fp
1087 + (\array_get:Nn \g__hobby_angles_array {\g__hobby_npoints_int - 1})}
1088 \fp_compare:nT {\l_hobby_tempa_fp > \c_pi_fp }
1089 {
1090 \fp_sub:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
1091 }
1092 \fp_compare:nT {\l_hobby_tempa_fp < -\c_pi_fp }
1093 {
1094 \fp_add:Nn \l_hobby_tempa_fp {2 * \c_pi_fp}
1095 }
1096
1097 \array_gput:Nnx \g__hobby_phi_array {\g__hobby_npoints_int}
1098 {\fp_to_tl:N \l_hobby_tempa_fp}
1099
1100 \array_gput:Nnx \g__hobby_matrix_d_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
1101 \l_hobby_tempa_fp
1102 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^2
1103 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2})
1104 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 2})
1105 -
1106 (3 * ( \array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 2}) - 1)
1107 * (\array_get:Nn \g__hobby_psi_array {\g__hobby_npoints_int - 1})
1108 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int - 1})^2
1109 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int})
1110 * (\array_get:Nn \g__hobby_distances_array {\g__hobby_npoints_int - 1}) }}
1111 }
1112 % \end{macrocode}
1113 % End of adjustments for open paths.
1114 % \begin{macrocode}
1115 }
1116 % \end{macrocode}
1117 %
1118 % Now we have the tridiagonal matrix in place, we implement the solution.
1119 % We start with the forward eliminations.
1120 % \begin{macrocode}
1121 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
1122
1123 \array_gput:Nnx \g__hobby_matrix_b_array {##1} {\fp_to_tl:n {
1124 (\array_get:Nn \g__hobby_matrix_b_array {##1 - 1})
1125 * (\array_get:Nn \g__hobby_matrix_b_array {##1})
1126 -
1127 (\array_get:Nn \g__hobby_matrix_c_array {##1 - 1})
1128 * (\array_get:Nn \g__hobby_matrix_a_array {##1})
1129 }}
1130 % \end{macrocode}
1131 % The last time, we don't touch the \(C\)-vector.
1132 % \begin{macrocode}
1133 \int_compare:nT {##1 < \g__hobby_npoints_int - 1} {
1134
1135 \array_gput:Nnx \g__hobby_matrix_c_array {##1} {\fp_to_tl:n {
1136 (\array_get:Nn \g__hobby_matrix_b_array {##1 - 1})
1137 * (\array_get:Nn \g__hobby_matrix_c_array {##1})
1138 }}
1139 }
1140
1141 \array_gput:Nnx \g__hobby_matrix_d_array {##1} {\fp_to_tl:n {
1142 (\array_get:Nn \g__hobby_matrix_b_array {##1 - 1})
1143 * (\array_get:Nn \g__hobby_matrix_d_array {##1})
1144 -
1145 (\array_get:Nn \g__hobby_matrix_d_array {##1 - 1})
1146 * (\array_get:Nn \g__hobby_matrix_a_array {##1})
1147 }}
1148 % \end{macrocode}
1149 % On a closed path, we also want to know \(M^{-1} u\) so need to do the elimination steps on \(u\) as well.
1150 % \begin{macrocode}
1151 \bool_if:NT \g__hobby_closed_bool {
1152 \array_gput:Nnx \g__hobby_vector_u_array {##1} {\fp_to_tl:n {
1153 (\array_get:Nn \g__hobby_matrix_b_array {##1 - 1})
1154 * (\array_get:Nn \g__hobby_vector_u_array {##1})
1155 -
1156 (\array_get:Nn \g__hobby_vector_u_array {##1 - 1})
1157 * (\array_get:Nn \g__hobby_matrix_a_array {##1})
1158 }}
1159 }
1160 }
1161 % \end{macrocode}
1162 % Now we start the back substitution.
1163 % The first step is slightly different to the general step.
1164 % \begin{macrocode}
1165 \array_gput:Nnx \g__hobby_theta_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
1166 (\array_get:Nn \g__hobby_matrix_d_array {\g__hobby_npoints_int - 1})
1167 / (\array_get:Nn \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1})
1168 }}
1169 % \end{macrocode}
1170 % For a closed path, we need to work with \(u\) as well.
1171 % \begin{macrocode}
1172 \bool_if:NT \g__hobby_closed_bool {
1173 \array_gput:Nnx \g__hobby_vector_u_array {\g__hobby_npoints_int - 1} {\fp_to_tl:n {
1174 (\array_get:Nn \g__hobby_vector_u_array {\g__hobby_npoints_int - 1})
1175 / (\array_get:Nn \g__hobby_matrix_b_array {\g__hobby_npoints_int - 1})
1176 }}
1177 }
1178 % \end{macrocode}
1179 % Now we iterate over the vectors, doing the remaining back substitutions.
1180 % \begin{macrocode}
1181 \int_step_inline:nnnn {\g__hobby_npoints_int - 2} {-1} {0} {
1182
1183 \array_gput:Nnx \g__hobby_theta_array {##1} {\fp_to_tl:n {
1184 ( (\array_get:Nn \g__hobby_matrix_d_array {##1})
1185 - (\array_get:Nn \g__hobby_theta_array {##1 + 1})
1186 * (\array_get:Nn \g__hobby_matrix_c_array {##1})
1187 ) / (\array_get:Nn \g__hobby_matrix_b_array {##1})
1188 }}
1189 }
1190 \bool_if:NT \g__hobby_closed_bool {
1191 % \end{macrocode}
1192 % On a closed path, we also need to work out \(M^{-1} u\).
1193 % \begin{macrocode}
1194 \int_step_inline:nnnn {\g__hobby_npoints_int - 2} {-1} {0} {
1195 \array_gput:Nnx \g__hobby_vector_u_array {##1} {\fp_to_tl:n
1196 {
1197 ((\array_get:Nn \g__hobby_vector_u_array {##1})
1198 - (\array_get:Nn \g__hobby_vector_u_array {##1 + 1})
1199 * (\array_get:Nn \g__hobby_matrix_c_array {##1})
1200 ) / (\array_get:Nn \g__hobby_matrix_b_array {##1})
1201 }}
1202 }
1203 % \end{macrocode}
1204 % Then we compute \(v^\top M^{-1}u\) and \(v^\top M^{-1} \theta\).
1205 % As \(v\) has a particularly simple form, these inner products are easy to compute.
1206 % \begin{macrocode}
1207
1208 \fp_set:Nn \l_hobby_tempb_fp {
1209 ((\array_get:Nn \g__hobby_theta_array {1})
1210 * \l_hobby_matrix_v_fp
1211 - (\array_get:Nn \g__hobby_theta_array {\g__hobby_npoints_int - 1})
1212 ) / (
1213 (\array_get:Nn \g__hobby_vector_u_array {1})
1214 * \l_hobby_matrix_v_fp
1215 - (\array_get:Nn \g__hobby_vector_u_array {\g__hobby_npoints_int - 1})
1216 + 1
1217 )}
1218
1219 \int_step_inline:nnnn {0} {1} {\g__hobby_npoints_int - 1} {
1220
1221 \array_gput:Nnx \g__hobby_theta_array {##1} {\fp_to_tl:n {
1222 (\array_get:Nn \g__hobby_theta_array {##1})
1223 - (\array_get:Nn \g__hobby_vector_u_array {##1})
1224 * \l_hobby_tempb_fp
1225 }}
1226 }
1227 }
1228 % \end{macrocode}
1229 %
1230 % Now that we have computed the \(\theta_i\)s, we can quickly compute the \(\phi_i\)s.
1231 %
1232 % \begin{macrocode}
1233 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int - 1} {
1234
1235 \array_gput:Nnx \g__hobby_phi_array {##1} {\fp_to_tl:n {
1236 - (\array_get:Nn \g__hobby_psi_array {##1})
1237 - (\array_get:Nn \g__hobby_theta_array {##1})
1238 }}
1239 }
1240 % \end{macrocode}
1241 %
1242 % If the path is open, this works for all except \(\phi_n\).
1243 % If the path is closed, we can drop our added point.
1244 % Cheaply, of course.
1245 % \begin{macrocode}
1246 \bool_if:NTF \g__hobby_closed_bool {
1247 \int_gdecr:N \g__hobby_npoints_int
1248 }{
1249 % \end{macrocode}
1250 % If \(\phi_n\) was not given, we compute it from \(\theta_{n-1}\).
1251 % \begin{macrocode}
1252 \fp_compare:nT { \g__hobby_in_angle_fp == \c_inf_fp }
1253 {
1254 \array_gput:Nnx \g__hobby_phi_array {\g__hobby_npoints_int} {\fp_to_tl:n {
1255 ((3 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int}) - 1)
1256 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^3
1257 * \g__hobby_out_curl_fp
1258 +
1259 (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int })^3)
1260 /
1261 ((3 * (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int -2}) - 1)
1262 * (\array_get:Nn \g__hobby_tension_in_array {\g__hobby_npoints_int})^3 \l_tmpa_tl
1263 +
1264 (\array_get:Nn \g__hobby_tension_out_array {\g__hobby_npoints_int - 1})^3
1265 * \g__hobby_out_curl_fp)
1266 *
1267 (\array_get:Nn \g__hobby_theta_array {\g__hobby_npoints_int -1})
1268 }}
1269 }
1270 }
1271 }
1272 % \end{macrocode}
1273 % \end{macro}
1274 %
1275 %
1276 % \begin{macro}{\hobby_build_path:}
1277 % Once we've computed the angles, we build the actual path.
1278 % \begin{macrocode}
1279 \cs_new:Nn \hobby_build_path:
1280 {
1281 % \end{macrocode}
1282 % Next task is to compute the \(\rho_i\) and \(\sigma_i\).
1283 % \begin{macrocode}
1284 \int_step_inline:nnnn {0} {1} {\g__hobby_npoints_int - 1} {
1285
1286 \fp_set:Nn \l_hobby_tempa_fp {\array_get:Nn \g__hobby_theta_array {##1}}
1287
1288 \fp_set:Nn \l_hobby_tempb_fp {\array_get:Nn \g__hobby_phi_array {##1 + 1}}
1289
1290 \hobby_ctrllen:NVV \l_hobby_temps_fp \l_hobby_tempa_fp \l_hobby_tempb_fp
1291
1292 \array_gput:Nnx \g__hobby_sigma_array {##1 + 1} {\fp_to_tl:N \l_hobby_temps_fp}
1293
1294 \hobby_ctrllen:NVV \l_hobby_temps_fp \l_hobby_tempb_fp \l_hobby_tempa_fp
1295
1296 \array_gput:Nnx \g__hobby_rho_array {##1} {\fp_to_tl:N \l_hobby_temps_fp}
1297
1298 }
1299 % \end{macrocode}
1300 % Lastly, we generate the coordinates of the control points.
1301 % \begin{macrocode}
1302 \int_step_inline:nnnn {0} {1} {\g__hobby_npoints_int - 1} {
1303 \array_gput:Nnx \g__hobby_controla_array {##1 + 1} {\fp_eval:n {
1304 (\array_get:Nn \g__hobby_points_x_array {##1})
1305 +
1306 (\array_get:Nn \g__hobby_distances_array {##1}) *
1307 (\array_get:Nn \g__hobby_rho_array {##1}) *
1308 cos ( (\array_get:Nn \g__hobby_angles_array {##1})
1309 +
1310 (\array_get:Nn \g__hobby_theta_array {##1}))
1311 /3
1312 }, \fp_eval:n {
1313 ( \array_get:Nn \g__hobby_points_y_array {##1}) +
1314 (\array_get:Nn \g__hobby_distances_array {##1}) *
1315 (\array_get:Nn \g__hobby_rho_array {##1}) *
1316 sin ( (\array_get:Nn \g__hobby_angles_array {##1})
1317 +
1318 (\array_get:Nn \g__hobby_theta_array {##1}))
1319 /3
1320 }
1321 }
1322 }
1323 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int} {
1324 \array_gput:Nnx \g__hobby_controlb_array {##1} {
1325 \fp_eval:n {\array_get:Nn \g__hobby_points_x_array {##1}
1326 - (\array_get:Nn \g__hobby_distances_array {##1 - 1})
1327 * (\array_get:Nn \g__hobby_sigma_array {##1})
1328 * cos((\array_get:Nn \g__hobby_angles_array {##1 - 1})
1329 - (\array_get:Nn \g__hobby_phi_array {##1}))/3
1330 }, \fp_eval:n {
1331 (\array_get:Nn \g__hobby_points_y_array {##1})
1332 - (\array_get:Nn \g__hobby_distances_array {##1 - 1})
1333 * (\array_get:Nn \g__hobby_sigma_array {##1})
1334 * sin((\array_get:Nn \g__hobby_angles_array {##1 - 1})
1335 - (\array_get:Nn \g__hobby_phi_array {##1}))/3
1336 } }
1337 }
1338 }
1339 % \end{macrocode}
1340 % \end{macro}
1341 %
1342 % \begin{macro}{\hobbyinit}
1343 % Initialise the settings for Hobby's algorithm
1344 % \begin{macrocode}
1345 \NewDocumentCommand \hobbyinit {m m m} {
1346 \hobby_set_cmds:NNN #1#2#3
1347 \hobby_clear_path:
1348 }
1349 % \end{macrocode}
1350 % \end{macro}
1351 %
1352 % \begin{macro}{\hobbyaddpoint}
1353 % This adds a point, possibly with tensions, to the current stack.
1354 % \begin{macrocode}
1355 \NewDocumentCommand \hobbyaddpoint { m } {
1356 \keys_set:nn { hobby/read in all }
1357 {
1358 tension~out,
1359 tension~in,
1360 excess~angle,
1361 blank,
1362 break,
1363 #1
1364 }
1365 \tl_if_eq:VnTF \l_tmpa_tl {true}
1366 {\tl_set:Nn \l_tmpa_tl {2}}
1367 {
1368 \tl_if_eq:VnTF \l_tmpa_tl {soft}
1369 {\tl_set:Nn \l_tmpa_tl {0}}
1370 {\tl_set:Nn \l_tmpa_tl {1}}
1371 }
1372 \tl_if_eq:VnTF \l_tmpb_tl {true}
1373 {\tl_put_right:Nn \l_tmpa_tl {1}}
1374 {\tl_put_right:Nn \l_tmpa_tl {0}}
1375 \tl_set:Nx \l_hobby_tempa_tl {\fp_use:N \l_hobby_tempa_fp}
1376 \tl_set:Nx \l_hobby_tempb_tl {\fp_use:N \l_hobby_tempb_fp}
1377 \hobby_add_point:VVVVVV \l_hobby_tempa_tl \l_hobby_tempb_tl \l_hobby_tempc_fp \l_hobby_tempd_fp \l_hobby_temps_fp \l_tmpa_tl
1378 }
1379 % \end{macrocode}
1380 % \end{macro}
1381 %
1382 % \begin{macro}{\hobby_add_point:n}
1383 % \begin{macrocode}
1384 \cs_new_nopar:Npn \hobby_add_point:nnnnnn #1#2#3#4#5#6
1385 {
1386 \array_gpush:Nn \g__hobby_actions_array { #6 }
1387 \array_gpush:Nn \g__hobby_tension_out_array { #3 }
1388 \array_gpush:Nn \g__hobby_tension_in_array { #4 }
1389 \array_gpush:Nn \g__hobby_excess_angle_array { #5 }
1390 \array_gpush:Nn \g__hobby_points_array { #1, #2 }
1391 \array_gpush:Nn \g__hobby_points_x_array { #1 }
1392 \array_gpush:Nn \g__hobby_points_y_array { #2 }
1393 }
1394 \cs_generate_variant:Nn \hobby_add_point:nnnnnn {VVVVVV}
1395 % \end{macrocode}
1396 % \end{macro}
1397 %
1398 % \begin{macro}{\hobbysetparams}
1399 % This sets the parameters for the curve.
1400 % \begin{macrocode}
1401 \NewDocumentCommand \hobbysetparams { m } {
1402 \keys_set:nn { hobby / read in params }
1403 {
1404 #1
1405 }
1406 }
1407 % \end{macrocode}
1408 % \end{macro}
1409 %
1410 % \begin{macro}{\hobby_set_cmds:NNN}
1411 % The path-generation code doesn't know what to actually do with the path so the initialisation code will set some macros to do that.
1412 % This is an auxiliary command that sets these macros.
1413 % \begin{macrocode}
1414 \cs_new:Npn \hobby_moveto:nnn #1#2#3 {}
1415 \cs_new:Npn \hobby_curveto:nnn #1#2#3 {}
1416 \cs_new:Npn \hobby_close:n #1 {}
1417 \cs_generate_variant:Nn \hobby_moveto:nnn {VVV,nnV}
1418 \cs_generate_variant:Nn \hobby_curveto:nnn {VVV}
1419 \cs_generate_variant:Nn \hobby_close:n {V}
1420 \cs_new:Nn \hobby_set_cmds:NNN {
1421 \cs_gset_eq:NN \hobby_moveto:nnn #1
1422 \cs_gset_eq:NN \hobby_curveto:nnn #2
1423 \cs_gset_eq:NN \hobby_close:n #3
1424 }
1425 % \end{macrocode}
1426 % \end{macro}
1427 %
1428 % \begin{macro}{\hobbygenpath}
1429 % This is the user (well, sort of) command that generates the curve.
1430 % \begin{macrocode}
1431 \NewDocumentCommand \hobbygenpath { } {
1432 \array_if_empty:NF \g__hobby_points_array {
1433 \hobby_gen_path:
1434 }
1435 }
1436 % \end{macrocode}
1437 % \end{macro}
1438 %
1439 % \begin{macro}{\hobbygenifnecpath}
1440 % If the named path doesn't exist, it is generated and named.
1441 % If it does exist, we restore it.
1442 % Either way, we save it to the aux file.
1443 % \begin{macrocode}
1444 \NewDocumentCommand \hobbygenifnecpath { m } {
1445 \tl_if_exist:cTF {g_hobby_#1_path}
1446 {
1447 \tl_use:c {g_hobby_#1_path}
1448 }
1449 {
1450 \hobby_gen_path:
1451 }
1452 \hobby_save_path:n {#1}
1453 \hobby_save_path_to_aux:x {#1}
1454 }
1455 % \end{macrocode}
1456 % \end{macro}
1457 %
1458 % \begin{macro}{\hobbygenifnecusepath}
1459 % If the named path doesn't exist, it is generated and named.
1460 % If it does exist, we restore it.
1461 % Either way, we save it to the aux file.
1462 % \begin{macrocode}
1463 \NewDocumentCommand \hobbygenuseifnecpath { m } {
1464 \tl_if_exist:cTF {g_hobby_#1_path}
1465 {
1466 \tl_use:c {g_hobby_#1_path}
1467 }
1468 {
1469 \hobby_gen_path:
1470 }
1471 \hobby_save_path:n {#1}
1472 \hobby_save_path_to_aux:x {#1}
1473 \hobby_use_path:
1474 }
1475 % \end{macrocode}
1476 % \end{macro}
1477 %
1478 % \begin{macro}{\hobbyusepath}
1479 % This is the user (well, sort of) command that uses the last generated curve.
1480 % \begin{macrocode}
1481 \NewDocumentCommand \hobbyusepath { m } {
1482 \hobbysetparams{#1}
1483 \hobby_use_path:
1484 }
1485 % \end{macrocode}
1486 % \end{macro}
1487 %
1488 % \begin{macro}{\hobbysavepath}
1489 % This is the user (well, sort of) command that uses the last generated curve.
1490 % \begin{macrocode}
1491 \NewDocumentCommand \hobbysavepath { m } {
1492 \hobby_save_path:n {#1}
1493 }
1494 % \end{macrocode}
1495 % \end{macro}
1496 %
1497 % \begin{macro}{\hobbyrestorepath}
1498 % This is the user (well, sort of) command that uses the last generated curve.
1499 % \begin{macrocode}
1500 \NewDocumentCommand \hobbyrestorepath { m } {
1501 \tl_if_exist:cT {g_hobby_#1_path} {
1502 \tl_use:c {g_hobby_#1_path}
1503 }
1504 }
1505 % \end{macrocode}
1506 % \end{macro}
1507 %
1508 % \begin{macro}{\hobbyshowpath}
1509 % This is the user (well, sort of) command that uses the last generated curve.
1510 % \begin{macrocode}
1511 \NewDocumentCommand \hobbyshowpath { m } {
1512 \tl_if_exist:cT {g_hobby_#1_path} {
1513 \tl_show:c {g_hobby_#1_path}
1514 }
1515 }
1516 % \end{macrocode}
1517 % \end{macro}
1518 %
1519 % \begin{macro}{\hobbygenusepath}
1520 % This is the user (well, sort of) command that generates a curve and uses it.
1521 % \begin{macrocode}
1522 \NewDocumentCommand \hobbygenusepath { } {
1523 \array_if_empty:NF \g__hobby_points_array {
1524 \hobby_gen_path:
1525 \hobby_use_path:
1526 }
1527 }
1528 % \end{macrocode}
1529 % \end{macro}
1530 %
1531 % \begin{macro}{\hobbyclearpath}
1532 % This is the user (well, sort of) command that generates a curve and uses it.
1533 % \begin{macrocode}
1534 \NewDocumentCommand \hobbyclearpath { } {
1535 \hobby_clear_path:
1536 }
1537 % \end{macrocode}
1538 % \end{macro}
1539 %
1540 % \begin{macro}{\hobby_use_path:}
1541 % This is the command that uses the curve.
1542 % As the curve data is stored globally, the same data can be reused by calling this function more than once without calling the generating function.
1543 % \begin{macrocode}
1544 \tl_new:N \l_tmpc_tl
1545 \tl_new:N \l_tmpd_tl
1546 \cs_new:Nn \hobby_use_path: {
1547 \bool_if:NT \g__hobby_disjoint_bool {
1548 \array_get:NnN \g__hobby_points_array {0} \l_tmpa_tl
1549 \hobby_moveto:nnV {} {} \l_tmpa_tl
1550 }
1551 \int_step_inline:nnnn {1} {1} {\g__hobby_npoints_int} {
1552 \array_get:NnN \g__hobby_controla_array {##1} \l_tmpa_tl
1553 \array_get:NnN \g__hobby_controlb_array {##1} \l_tmpb_tl
1554 \array_get:NnN \g__hobby_points_array {##1} \l_tmpc_tl
1555 \array_get:NnN \g__hobby_actions_array {##1} \l_tmpd_tl
1556 \int_compare:nNnTF {\tl_item:Nn \l_tmpd_tl {1}} = {\g__hobby_draw_int} {
1557 \hobby_curveto:VVV \l_tmpa_tl \l_tmpb_tl \l_tmpc_tl
1558 }{
1559 \bool_gset_false:N \g__hobby_closed_bool
1560 \hobby_moveto:VVV \l_tmpa_tl \l_tmpb_tl \l_tmpc_tl
1561 }
1562 \tl_if_eq:xnTF {\tl_item:Nn \l_tmpd_tl {2}} {1} {
1563 \bool_gset_false:N \g__hobby_closed_bool
1564 \hobby_moveto:VVV \l_tmpa_tl \l_tmpb_tl \l_tmpc_tl
1565 }{}
1566 }
1567 \bool_if:NT \g__hobby_closed_bool {
1568 \array_get:NnN \g__hobby_points_array {0} \l_tmpa_tl
1569 \hobby_close:V \l_tmpa_tl
1570 }
1571 }
1572 % \end{macrocode}
1573 % \end{macro}
1574 %
1575 % \begin{macro}{\hobby_save_path:n}
1576 % This command saves all the data needed to reinvoke the curve in a global token list that can be used to restore it afterwards.
1577 % \begin{macrocode}
1578 \cs_new:Nn \hobby_save_path:n {
1579 \tl_clear:N \l_tmpa_tl
1580 \tl_put_right:Nn \l_tmpa_tl {\int_gset:Nn \g__hobby_npoints_int}
1581 \tl_put_right:Nx \l_tmpa_tl {{\int_use:N \g__hobby_npoints_int}}
1582 \bool_if:NTF \g__hobby_disjoint_bool {
1583 \tl_put_right:Nn \l_tmpa_tl {\bool_gset_true:N}
1584 }{
1585 \tl_put_right:Nn \l_tmpa_tl {\bool_gset_false:N}
1586 }
1587 \tl_put_right:Nn \l_tmpa_tl {\g__hobby_disjoint_bool}
1588 \bool_if:NTF \g__hobby_closed_bool {
1589 \tl_put_right:Nn \l_tmpa_tl {\bool_gset_true:N}
1590 }{
1591 \tl_put_right:Nn \l_tmpa_tl {\bool_gset_false:N}
1592 }
1593 \tl_put_right:Nn \l_tmpa_tl {\g__hobby_closed_bool}
1594 \tl_put_right:Nn \l_tmpa_tl {\array_gclear:N \g__hobby_points_array}
1595 \array_map_inline:Nn \g__hobby_points_array {
1596 \tl_put_right:Nn \l_tmpa_tl {
1597 \array_gput:Nnn \g__hobby_points_array {##1} {##2}
1598 }
1599 }
1600 \tl_put_right:Nn \l_tmpa_tl {\array_gclear:N \g__hobby_actions_array}
1601 \array_map_inline:Nn \g__hobby_actions_array {
1602 \tl_put_right:Nn \l_tmpa_tl {
1603 \array_gput:Nnn \g__hobby_actions_array {##1} {##2}
1604 }
1605 }
1606 \tl_put_right:Nn \l_tmpa_tl {\array_gclear:N \g__hobby_controla_array}
1607 \array_map_inline:Nn \g__hobby_controla_array {
1608 \tl_put_right:Nn \l_tmpa_tl {
1609 \array_gput:Nnn \g__hobby_controla_array {##1} {##2}
1610 }
1611 }
1612 \tl_put_right:Nn \l_tmpa_tl {\array_gclear:N \g__hobby_controlb_array}
1613 \array_map_inline:Nn \g__hobby_controlb_array {
1614 \tl_put_right:Nn \l_tmpa_tl {
1615 \array_gput:Nnn \g__hobby_controlb_array {##1} {##2}
1616 }
1617 }
1618 \tl_gclear_new:c {g_hobby_#1_path}
1619 \tl_gset_eq:cN {g_hobby_#1_path} \l_tmpa_tl
1620 }
1621 % \end{macrocode}
1622 % \end{macro}
1623 %
1624 % \begin{macro}{\hobby_save_path_to_aux:n}
1625 % \begin{macrocode}
1626 \int_set:Nn \l_tmpa_int {\char_value_catcode:n {`@}}
1627 \char_set_catcode_letter:N @
1628 \cs_new:Npn \hobby_save_path_to_aux:n #1 {
1629 \bool_if:nT {
1630 \tl_if_exist_p:c {g_hobby_#1_path}
1631 &&
1632 ! \tl_if_exist_p:c {g_hobby_#1_path_saved}
1633 &&
1634 \l_hobby_save_aux_bool
1635 }
1636 {
1637 \tl_clear:N \l_tmpa_tl
1638 \tl_put_right:Nn \l_tmpa_tl {
1639 \ExplSyntaxOn
1640 \tl_gclear_new:c {g_hobby_#1_path}
1641 \tl_gput_right:cn {g_hobby_#1_path}
1642 }
1643 \tl_put_right:Nx \l_tmpa_tl {
1644 {\tl_to_str:c {g_hobby_#1_path}}
1645 }
1646 \tl_put_right:Nn \l_tmpa_tl {
1647 \ExplSyntaxOff
1648 }
1649 \protected@write\@auxout{}{
1650 \tl_to_str:N \l_tmpa_tl
1651 }
1652 \tl_new:c {g_hobby_#1_path_saved}
1653 }
1654 }
1655 \char_set_catcode:nn {`@} {\l_tmpa_int}
1656 \cs_generate_variant:Nn \hobby_save_path_to_aux:n {x}
1657 % \end{macrocode}
1658 % \end{macro}
1659 %
1660 % \begin{macro}{\hobby_clear_path:}
1661 % \begin{macrocode}
1662 \cs_new:Nn \hobby_clear_path:
1663 {
1664 \array_gclear:N \g__hobby_points_array
1665 \array_gclear:N \g__hobby_points_x_array
1666 \array_gclear:N \g__hobby_points_y_array
1667 \array_gclear:N \g__hobby_angles_array
1668 \array_gclear:N \g__hobby_actions_array
1669 \array_gclear:N \g__hobby_distances_array
1670 \array_gclear:N \g__hobby_tension_out_array
1671 \array_gclear:N \g__hobby_tension_in_array
1672 \array_gclear:N \g__hobby_excess_angle_array
1673 \array_gclear:N \g__hobby_matrix_a_array
1674 \array_gclear:N \g__hobby_matrix_b_array
1675 \array_gclear:N \g__hobby_matrix_c_array
1676 \array_gclear:N \g__hobby_matrix_d_array
1677 \array_gclear:N \g__hobby_vector_u_array
1678 \array_gclear:N \g__hobby_psi_array
1679 \array_gclear:N \g__hobby_theta_array
1680 \array_gclear:N \g__hobby_phi_array
1681 \array_gclear:N \g__hobby_sigma_array
1682 \array_gclear:N \g__hobby_rho_array
1683 \array_gclear:N \g__hobby_controla_array
1684 \array_gclear:N \g__hobby_controlb_array
1685 \bool_gset_false:N \g__hobby_closed_bool
1686 \bool_gset_false:N \g__hobby_disjoint_bool
1687
1688 \int_gset:Nn \g__hobby_npoints_int {-1}
1689 \int_gset:Nn \g__hobby_draw_int {1}
1690 \fp_gset_eq:NN \g__hobby_in_angle_fp \c_inf_fp
1691 \fp_gset_eq:NN \g__hobby_out_angle_fp \c_inf_fp
1692 \fp_gset_eq:NN \g__hobby_in_curl_fp \c_one_fp
1693 \fp_gset_eq:NN \g__hobby_out_curl_fp \c_one_fp
1694 }
1695 % \end{macrocode}
1696 % \end{macro}
1697 % \begin{macrocode}
1698 \ExplSyntaxOff
1699 % \end{macrocode}
1700 % \iffalse
1701 %</hobby>
1702 % \fi
1703 %
1704 % \subsection{PGF Library}
1705 %
1706 % \iffalse
1707 %<*pgflibrary>
1708 % \fi
1709 %
1710 % The PGF level is very simple.
1711 % All we do is set up the path-construction commands that get passed to the path-generation function.
1712 % \begin{macrocode}
1713 \input{hobby.code.tex}
1714 % \end{macrocode}
1715 % Points are communicated as key-pairs.
1716 % These keys translate from the \LaTeX3 style points to PGF points.
1717 % \begin{macrocode}
1718 \pgfkeys{
1719 /pgf/hobby/.is family,
1720 /pgf/hobby/.cd,
1721 point/.code={%
1722 \hobby@parse@pt#1\relax}
1723 }
1724 \def\hobby@parse@pt#1,#2\relax{%
1725 \pgf@x=#1cm\relax
1726 \pgf@y=#2cm\relax
1727 }
1728 % \end{macrocode}
1729 %
1730 % \begin{macro}{hobbyatan2}
1731 % The original PGF version of \Verb+atan2+ had the arguments the wrong way around.
1732 % This was fixed in the CVS version in July 2013, but as old versions are likely to be in use for some time, we define a wrapper function that ensures that the arguments are correct.
1733 % \begin{macrocode}
1734 \pgfmathparse{atan2(0,1)}
1735 \def\hobby@temp{0.0}
1736 \ifx\pgfmathresult\hobby@temp
1737 \pgfmathdeclarefunction{hobbyatan2}{2}{%
1738 \pgfmathatantwo@{#1}{#2}%
1739 }
1740 \else
1741 \pgfmathdeclarefunction{hobbyatan2}{2}{%
1742 \pgfmathatantwo@{#2}{#1}%
1743 }
1744 \fi
1745 % \end{macrocode}
1746 % \end{macro}
1747 %
1748 % \begin{macro}{\hobby@curveto}
1749 % This is passed to the path-generation code to translate the path into a PGF path.
1750 % \begin{macrocode}
1751 \def\hobby@curveto#1#2#3{%
1752 \pgfpathcurveto{\hobby@topgf{#1}}{\hobby@topgf{#2}}{\hobby@topgf{#3}}%
1753 }
1754 % \end{macrocode}
1755 % \end{macro}
1756 %
1757 % \begin{macro}{\hobby@moveto}
1758 % This is passed to the path-generation code to translate the path into a PGF path.
1759 % \begin{macrocode}
1760 \def\hobby@moveto#1#2#3{%
1761 \pgfpathmoveto{\hobby@topgf{#3}}%
1762 }
1763 % \end{macrocode}
1764 % \end{macro}
1765 %
1766 % \begin{macro}{\hobby@topgf}
1767 % Translates a \LaTeX3 point to a PGF point.
1768 % \begin{macrocode}
1769 \def\hobby@topgf#1{%
1770 \pgfqkeys{/pgf/hobby}{point={#1}}%
1771 }
1772 % \end{macrocode}
1773 % \end{macro}
1774 %
1775 % \begin{macro}{\hobby@close}
1776 % Closes a path.
1777 % \begin{macrocode}
1778 \def\hobby@close#1{%
1779 \pgfpathclose
1780 }
1781 % \end{macrocode}
1782 % \end{macro}
1783 %
1784 % \begin{macro}{\pgfpathhobby}
1785 % Low-level interface to the hobby construction.
1786 % This sets up the commands and starts the iterator.
1787 % \begin{macrocode}
1788 \def\pgfpathhobby{%
1789 \pgfutil@ifnextchar\bgroup{\pgfpath@hobby}{\pgfpath@hobby{}}}
1790 \def\pgfpath@hobby#1{%
1791 \hobbyinit\hobby@moveto\hobby@curveto\hobby@close
1792 \hobbysetparams{#1}%
1793 \pgfmathsetmacro\hobby@x{\the\pgf@path@lastx/1cm}%
1794 \pgfmathsetmacro\hobby@y{\the\pgf@path@lasty/1cm}%
1795 \hobbyaddpoint{point={\hobby@x, \hobby@y}}%
1796 }
1797 % \end{macrocode}
1798 % \end{macro}
1799 %
1800 % \begin{macro}{\pgfpathhobbypt}
1801 % Adds a point to the construction
1802 % \begin{macrocode}
1803 \def\pgfpathhobbypt#1{%
1804 #1%
1805 \pgfmathsetmacro\hobby@x{\the\pgf@x/1cm}%
1806 \pgfmathsetmacro\hobby@y{\the\pgf@y/1cm}%
1807 \pgfutil@ifnextchar\bgroup{\pgfpathhobbyptparams}{\pgfpathhobbyptparams{}}%
1808 }
1809 % \end{macrocode}
1810 % \end{macro}
1811 %
1812 % \begin{macro}{\pgfpathhobbyptparams}
1813 % \begin{macrocode}
1814 \def\pgfpathhobbyptparams#1{%
1815 \hobbyaddpoint{#1,point={\hobby@x, \hobby@y}}%
1816 }
1817 % \end{macrocode}
1818 % \end{macro}
1819 %
1820 % \begin{macro}{\pgfpathhobbyend}
1821 % \begin{macrocode}
1822 \def\pgfpathhobbyend{%
1823 \ifhobby@externalise
1824 \ifx\hobby@path@name\pgfutil@empty
1825 \hobbygenusepath
1826 \else
1827 \hobbygenuseifnecpath{\hobby@path@name}%
1828 \fi
1829 \else
1830 \hobbygenusepath
1831 \fi
1832 \ifx\hobby@path@name\pgfutil@empty
1833 \else
1834 \hobbysavepath{\hobby@path@name}%
1835 \fi
1836 \global\let\hobby@path@name=\pgfutil@empty
1837 }
1838 % \end{macrocode}
1839 % \end{macro}
1840 %
1841 %
1842 % Plot handlers
1843 %
1844 % \begin{macro}{\pgfplothanderhobby}
1845 % Basic plot handler; uses full algorithm but therefore expensive
1846 % \begin{macrocode}
1847 \def\pgfplothandlerhobby{%
1848 \def\pgf@plotstreamstart{%
1849 \hobbyinit\hobby@moveto\hobby@curveto\hobby@close
1850 \global\let\pgf@plotstreampoint=\pgf@plot@hobby@firstpt
1851 \global\let\pgf@plotstreamspecial=\pgfutil@gobble
1852 \gdef\pgf@plotstreamend{%
1853 \ifhobby@externalise
1854 \ifx\hobby@path@name\pgfutil@empty
1855 \hobbygenusepath
1856 \else
1857 \hobbygenuseifnecpath{\hobby@path@name}%
1858 \fi
1859 \else
1860 \hobbygenusepath
1861 \fi
1862 \ifx\hobby@path@name\pgfutil@empty
1863 \else
1864 \hobbysavepath{\hobby@path@name}%
1865 \fi
1866 \global\let\hobby@path@name=\pgfutil@empty
1867 }%
1868 \let\tikz@scan@point@options=\pgfutil@empty
1869 }
1870 }
1871 % \end{macrocode}
1872 % \end{macro}
1873 %
1874 % \begin{macro}{\pgfplothandlerclosedhobby}
1875 % Same as above but produces a closed curve
1876 % \begin{macrocode}
1877 \def\pgfplothandlerclosedhobby{%
1878 \def\pgf@plotstreamstart{%
1879 \hobbyinit\hobby@moveto\hobby@curveto\hobby@close
1880 \hobbysetparams{closed=true,disjoint=true}%
1881 \global\let\pgf@plotstreampoint=\pgf@plot@hobby@firstpt
1882 \global\let\pgf@plotstreamspecial=\pgfutil@gobble
1883 \gdef\pgf@plotstreamend{%
1884 \ifhobby@externalise
1885 \ifx\hobby@path@name\pgfutil@empty
1886 \hobbygenusepath
1887 \else
1888 \hobbygenuseifnecpath{\hobby@path@name}%
1889 \fi
1890 \else
1891 \hobbygenusepath
1892 \fi
1893 \ifx\hobby@path@name\pgfutil@empty
1894 \else
1895 \hobbysavepath{\hobby@path@name}%
1896 \fi
1897 \global\let\hobby@path@name=\pgfutil@empty
1898 }%
1899 }
1900 }
1901 % \end{macrocode}
1902 % \end{macro}
1903 %
1904 % \begin{macro}{\pgf@plot@hobby@firstpt}
1905 % First point, move or line as appropriate and then start the algorithm.
1906 % \begin{macrocode}
1907 \def\pgf@plot@hobby@firstpt#1{%
1908 \pgf@plot@first@action{#1}%
1909 \pgf@plot@hobby@handler{#1}%
1910 \global\let\pgf@plotstreampoint=\pgf@plot@hobby@handler
1911 }
1912 % \end{macrocode}
1913 % \end{macro}
1914 %
1915 % \begin{macro}{\pgf@plot@hobby@handler}
1916 % Add points to the array for the algorithm to work on.
1917 % \begin{macrocode}
1918 \def\pgf@plot@hobby@handler#1{%
1919 #1%
1920 \pgfmathsetmacro\hobby@x{\the\pgf@x/1cm}%
1921 \pgfmathsetmacro\hobby@y{\the\pgf@y/1cm}%
1922 \hobbyaddpoint{point={\hobby@x, \hobby@y}}%
1923 }
1924 % \end{macrocode}
1925 % \end{macro}
1926 %
1927 % \begin{macro}{\pgfplothandlerquickhobby}
1928 % Uses the ``quick'' algorithm.
1929 % \begin{macrocode}
1930 \def\pgfplothandlerquickhobby{%
1931 \def\pgf@plotstreamstart{%
1932 \global\let\hobby@quick@curveto=\pgfpathcurveto
1933 \global\let\pgf@plotstreampoint=\pgf@plot@qhobby@firstpt
1934 \global\let\pgf@plotstreamspecial=\pgfutil@gobble
1935 \global\let\pgf@plotstreamend=\pgf@plot@qhobby@end
1936 }
1937 }
1938 % \end{macrocode}
1939 % \end{macro}
1940 %
1941 % \begin{macro}{\pgf@plot@qhobby@firstpt}
1942 % Carry out first action (move or line) and save point.
1943 % \begin{macrocode}
1944 \def\pgf@plot@qhobby@firstpt#1{%
1945 #1%
1946 \edef\hobby@temp{\noexpand\pgf@plot@first@action{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}\hobby@temp%
1947 \xdef\hobby@qpoints{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
1948 \gdef\hobby@qpointa{}%
1949 \gdef\hobby@angle{}%
1950 \global\let\pgf@plotstreampoint=\pgf@plot@qhobby@secondpt
1951 }
1952 % \end{macrocode}
1953 % \end{macro}
1954 %
1955 % \begin{macro}{\pgf@plot@qhobby@secondpt}
1956 % Also need to save second point.
1957 % \begin{macrocode}
1958 \def\pgf@plot@qhobby@secondpt#1{%
1959 #1%
1960 \xdef\hobby@qpointa{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
1961 \global\let\pgf@plotstreampoint=\pgf@plot@qhobby@handler
1962 }
1963 % \end{macrocode}
1964 % \end{macro}
1965 %
1966 % \begin{macro}{\pgf@plot@qhobby@handler}
1967 % Wrapper around the computation macro that saves the variables globally.
1968 % \begin{macrocode}
1969 \def\pgf@plot@qhobby@handler#1{%
1970 #1
1971 \edef\hobby@temp{\noexpand\hobby@quick@compute{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}\hobby@temp
1972 \global\let\hobby@qpointa=\hobby@qpointa
1973 \global\let\hobby@qpoints=\hobby@qpoints
1974 \global\let\hobby@angle=\hobby@angle
1975 % \end{macrocode}
1976 % Also need to save some data for the last point
1977 % \begin{macrocode}
1978 \global\let\hobby@thetaone=\hobby@thetaone
1979 \global\let\hobby@phitwo=\hobby@phitwo
1980 \global\let\hobby@done=\hobby@done
1981 \global\let\hobby@omegaone=\hobby@omegaone
1982 }
1983 % \end{macrocode}
1984 % \end{macro}
1985 %
1986 % \begin{macro}{\pgf@plot@qhobby@end}
1987 % Wrapper around the finalisation step.
1988 % \begin{macrocode}
1989 \def\pgf@plot@qhobby@end{%
1990 \hobby@quick@computeend
1991 }
1992 % \end{macrocode}
1993 % \end{macro}
1994 %
1995 % \begin{macro}{\hobby@sf}
1996 % Working with points leads to computations out of range so we scale to get them into the computable arena.
1997 % \begin{macrocode}
1998 \pgfmathsetmacro\hobby@sf{10cm}
1999 % \end{macrocode}
2000 % \end{macro}
2001 %
2002 %
2003 % \begin{macro}{\hobby@quick@compute}
2004 % This is the macro that does all the work of computing the control points.
2005 % The argument is the current point, \Verb+\hobby@qpointa+ is the middle point, and \Verb+\hobby@qpoints+ is the first point.
2006 % \begin{macrocode}
2007 \def\hobby@quick@compute#1{%
2008 % \end{macrocode}
2009 % Save the current (second - counting from zero) point in \Verb+\pgf@xb+ and \Verb+\pgf@yb+.
2010 % \begin{macrocode}
2011 #1%
2012 \pgf@xb=\pgf@x
2013 \pgf@yb=\pgf@y
2014 % \end{macrocode}
2015 % Save the previous (first) point in \Verb+\pgf@xa+ and \Verb+\pgf@ya+.
2016 % \begin{macrocode}
2017 \hobby@qpointa
2018 \pgf@xa=\pgf@x
2019 \pgf@ya=\pgf@y
2020 % \end{macrocode}
2021 % Adjust so that \Verb+(\pgf@xb,\pgf@yb)+ is the vector from second to third.
2022 % Then compute and store the distance and angle of this vector.
2023 % We view this as the vector \emph{from} the midpoint and everything to do with that point has the suffix \Verb+one+.
2024 % Note that we divide by the scale factor here.
2025 % \begin{macrocode}
2026 \advance\pgf@xb by -\pgf@xa
2027 \advance\pgf@yb by -\pgf@ya
2028 \pgfmathsetmacro\hobby@done{sqrt((\pgf@xb/\hobby@sf)^2 + (\pgf@yb/\hobby@sf)^2)}%
2029 \pgfmathsetmacro\hobby@omegaone{rad(hobbyatan2(\pgf@yb,\pgf@xb))}%
2030 % \end{macrocode}
2031 % Now we do the same with the vector from the zeroth to the first point.
2032 % \begin{macrocode}
2033 \hobby@qpoints
2034 \advance\pgf@xa by -\pgf@x
2035 \advance\pgf@ya by -\pgf@y
2036 \pgfmathsetmacro\hobby@dzero{sqrt((\pgf@xa/\hobby@sf)^2 + (\pgf@ya/\hobby@sf)^2)}%
2037 \pgfmathsetmacro\hobby@omegazero{rad(hobbyatan2(\pgf@ya,\pgf@xa))}%
2038 % \end{macrocode}
2039 % \Verb+\hobby@psi+ is the angle subtended at the midpoint.
2040 % We adjust to ensure that it is in the right range.
2041 % \begin{macrocode}
2042 \pgfmathsetmacro\hobby@psi{\hobby@omegaone - \hobby@omegazero}%
2043 \pgfmathsetmacro\hobby@psi{\hobby@psi > pi ? \hobby@psi - 2*pi : \hobby@psi}%
2044 \pgfmathsetmacro\hobby@psi{\hobby@psi < -pi ? \hobby@psi + 2*pi : \hobby@psi}%
2045 % \end{macrocode}
2046 % Now we test to see if we're on the first run or not.
2047 % If the first, we have no incoming angle.
2048 % \begin{macrocode}
2049 \ifx\hobby@angle\pgfutil@empty
2050 % \end{macrocode}
2051 % First.
2052 % \begin{macrocode}
2053 \pgfmathsetmacro\hobby@thetaone{-\hobby@psi * \hobby@done%
2054 /(\hobby@done + \hobby@dzero)}%
2055 \pgfmathsetmacro\hobby@thetazero{-\hobby@psi - \hobby@thetaone}%
2056 \let\hobby@phione=\hobby@thetazero
2057 \let\hobby@phitwo=\hobby@thetaone
2058 \else
2059 % \end{macrocode}
2060 % Second or later.
2061 % \begin{macrocode}
2062 \let\hobby@thetazero=\hobby@angle
2063 \pgfmathsetmacro\hobby@thetaone{%
2064 -(2 * \hobby@psi + \hobby@thetazero) * \hobby@done%
2065 / (2 * \hobby@done + \hobby@dzero)}%
2066 \pgfmathsetmacro\hobby@phione{-\hobby@psi - \hobby@thetaone}%
2067 \let\hobby@phitwo=\hobby@thetaone
2068 \fi
2069 % \end{macrocode}
2070 % Save the outgoing angle.
2071 % \begin{macrocode}
2072 \let\hobby@angle=\hobby@thetaone
2073 % \end{macrocode}
2074 % Compute the control points from the angles.
2075 % \begin{macrocode}
2076 \hobby@quick@ctrlpts{\hobby@thetazero}{\hobby@phione}{\hobby@qpoints}{\hobby@qpointa}{\hobby@dzero}{\hobby@omegazero}%
2077 % \end{macrocode}
2078 % Now call the call-back function
2079 % \begin{macrocode}
2080 \edef\hobby@temp{\noexpand\hobby@quick@curveto{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}{\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
2081 \hobby@temp
2082 % \end{macrocode}
2083 % Cycle the points round for the next iteration.
2084 % \begin{macrocode}
2085 \global\let\hobby@qpoints=\hobby@qpointa
2086 #1
2087 \xdef\hobby@qpointa{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2088 % \end{macrocode}
2089 % Save needed values in global macros
2090 % \begin{macrocode}
2091 \global\let\hobby@angle=\hobby@angle
2092 \global\let\hobby@phitwo=\hobby@phitwo
2093 \global\let\hobby@thetaone=\hobby@thetaone
2094 \global\let\hobby@done=\hobby@done
2095 \global\let\hobby@omegaone=\hobby@omegaone
2096 }
2097 % \end{macrocode}
2098 % \end{macro}
2099 %
2100 % \begin{macro}{\hobby@wuick@computeend}
2101 % This is the additional code for the final run.
2102 % \begin{macrocode}
2103 \def\hobby@quick@computeend{%
2104 % \end{macrocode}
2105 % Compute the control points for the second part of the curve and add that to the path.
2106 % \begin{macrocode}
2107 \hobby@quick@ctrlpts{\hobby@thetaone}{\hobby@phitwo}{\hobby@qpoints}{\hobby@qpointa}{\hobby@done}{\hobby@omegaone}%
2108 % \end{macrocode}
2109 % Now call the call-back function
2110 % \begin{macrocode}
2111 \edef\hobby@temp{\noexpand\hobby@quick@curveto{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}{\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
2112 \hobby@temp
2113 }%
2114 % \end{macrocode}
2115 % \end{macro}
2116 %
2117 % \begin{macro}{\hobby@quick@ctrlpts}
2118 % Compute the control points from the angles and points given.
2119 % \begin{macrocode}
2120 \def\hobby@quick@ctrlpts#1#2#3#4#5#6{%
2121 \pgfmathsetmacro\hobby@alpha{%
2122 sqrt(2) * (sin(#1 r) - 1/16 * sin(#2 r))%
2123 * (sin(#2 r) - 1/16 * sin(#1 r))%
2124 * (cos(#1 r) - cos(#2 r))}%
2125 \pgfmathsetmacro\hobby@rho{%
2126 (2 + \hobby@alpha)/(1 + (1 - (3 - sqrt(5))/2)%
2127 * cos(#1 r) + (3 - sqrt(5))/2 * cos(#2 r))}%
2128 \pgfmathsetmacro\hobby@sigma{%
2129 (2 - \hobby@alpha)/(1 + (1 - (3 - sqrt(5))/2)%
2130 * cos(#2 r) + (3 - sqrt(5))/2 * cos(#1 r))}%
2131 #3%
2132 \pgf@xa=\pgf@x
2133 \pgf@ya=\pgf@y
2134 \pgfmathsetlength\pgf@xa{%
2135 \pgf@xa + #5 * \hobby@rho%
2136 * cos((#1 + #6) r)/3*\hobby@sf}%
2137 \pgfmathsetlength\pgf@ya{%
2138 \pgf@ya + #5 * \hobby@rho%
2139 * sin((#1 + #6) r)/3*\hobby@sf}%
2140 #4%
2141 \pgf@xb=\pgf@x
2142 \pgf@yb=\pgf@y
2143 \pgfmathsetlength\pgf@xb{%
2144 \pgf@xb - #5 * \hobby@sigma%
2145 * cos((-#2 + #6) r)/3*\hobby@sf}%
2146 \pgfmathsetlength\pgf@yb{%
2147 \pgf@yb - #5 * \hobby@sigma%
2148 * sin((-#2 + #6) r)/3*\hobby@sf}%
2149 #4%
2150 }
2151 % \end{macrocode}
2152 % \end{macro}
2153 %
2154 % \iffalse
2155 %</pgflibrary>
2156 % \fi
2157 %
2158 % \subsection{TikZ Library}
2159 %
2160 % \iffalse
2161 %<*tikzlibrary>
2162 % \fi
2163 %
2164 % \begin{macrocode}
2165 \usepgflibrary{hobby}
2166 \let\hobby@this@opts=\pgfutil@empty
2167 \let\hobby@next@opts=\pgfutil@empty
2168 \let\hobby@action=\pgfutil@empty
2169 \let\hobby@path@name=\pgfutil@empty
2170 \newif\ifhobby@externalise
2171 % \end{macrocode}
2172 %
2173 % We set various TikZ keys.
2174 % These include the \Verb+to path+ constructor and all the various parameters that will eventually get passed to the path-generation code.
2175 % \begin{macrocode}
2176 \def\hobby@point@options{}%
2177 \tikzset{
2178 curve through/.style={
2179 to path={
2180 \pgfextra{
2181 \expandafter\curvethrough\expandafter[\hobby@next@opts]{%
2182 (\tikztostart) .. #1 .. (\tikztotarget)%
2183 }
2184 }
2185 }
2186 },
2187 tension in/.code = {%
2188 \expandafter\gdef\expandafter\hobby@point@options\expandafter%
2189 {\hobby@point@options,tension in=#1}%
2190 },
2191 tension out/.code = {%
2192 \expandafter\gdef\expandafter\hobby@point@options\expandafter%
2193 {\hobby@point@options,tension out=#1}%
2194 },
2195 tension/.append code = {%
2196 \expandafter\gdef\expandafter\hobby@point@options\expandafter%
2197 {\hobby@point@options,tension=#1}%
2198 },
2199 excess angle/.code = {%
2200 \expandafter\gdef\expandafter\hobby@point@options\expandafter%
2201 {\hobby@point@options,excess angle=#1}%
2202 },
2203 break/.code = {%
2204 \expandafter\gdef\expandafter\hobby@point@options\expandafter%
2205 {\hobby@point@options,break=#1}%
2206 },
2207 blank/.code = {%
2208 \expandafter\gdef\expandafter\hobby@point@options\expandafter%
2209 {\hobby@point@options,blank=#1}%
2210 },
2211 designated Hobby path/.initial={next},
2212 clear next Hobby path options/.code={%
2213 \gdef\hobby@next@opts{}%
2214 },
2215 clear this Hobby path options/.code={%
2216 \gdef\hobby@this@opts{}%
2217 },
2218 clear Hobby path options/.style={%
2219 clear \pgfkeysvalueof{/tikz/designated Hobby path} Hobby path options
2220 },
2221 add option to this Hobby path/.code={%
2222 \expandafter\gdef\expandafter\hobby@this@opts\expandafter{\hobby@this@opts#1,}%
2223 },
2224 add option to next Hobby path/.code={%
2225 \expandafter\gdef\expandafter\hobby@next@opts\expandafter{\hobby@next@opts#1,}%
2226 },
2227 add option to Hobby path/.style={%
2228 add option to \pgfkeysvalueof{/tikz/designated Hobby path} Hobby path={#1}%
2229 },
2230 closed/.style = {%
2231 add option to Hobby path={closed=#1,disjoint=#1}%
2232 },
2233 invert blank/.style = {%
2234 add option to Hobby path={invert blank=#1}%
2235 },
2236 closed/.default = true,
2237 blank/.default = true,
2238 break/.default = true,
2239 invert blank/.default = true,
2240 in angle/.code = {%
2241 \pgfmathparse{(#1)*pi/180}%
2242 \edef\@temp{in angle=\pgfmathresult,}%
2243 \pgfkeysalso{add option to Hobby path/.expand once=\@temp}%
2244 },
2245 out angle/.code = {%
2246 \pgfmathparse{(#1)*pi/180}%
2247 \edef\@temp{out angle=\pgfmathresult,}%
2248 \pgfkeysalso{add option to Hobby path/.expand once=\@temp}%
2249 },
2250 in curl/.style = {%
2251 add option to Hobby path={in curl=#1}%
2252 },
2253 out curl/.style = {%
2254 add option to Hobby path={out curl=#1}%
2255 },
2256 use Hobby shortcut/.code={%
2257 \let\tikz@curveto@auto=\hobby@curveto@override
2258 \global\let\hobby@curveto@delegate=\hobby@curveto@auto
2259 },
2260 use quick Hobby shortcut/.code={%
2261 \let\tikz@curveto@auto=\hobby@curveto@override
2262 \global\let\hobby@curveto@delegate=\hobby@qcurveto@auto
2263 },
2264 use previous Hobby path/.code={%
2265 \hobbyusepath{#1}%
2266 },
2267 use previous Hobby path/.default={},%
2268 save Hobby path/.code={%
2269 \xdef\hobby@path@name{#1}%
2270 },
2271 restore Hobby path/.code={%
2272 \hobbyinit\hobby@tikz@moveto\hobby@tikz@curveto\hobby@tikz@close
2273 \global\let\hobby@collected@onpath\pgfutil@empty
2274 \hobbyrestorepath{#1}%
2275 },
2276 restore and use Hobby path/.code 2 args={%
2277 \hobbyinit\hobby@tikz@moveto\hobby@tikz@curveto\hobby@tikz@close
2278 \global\let\hobby@collected@onpath\pgfutil@empty
2279 \hobbyrestorepath{#1}%
2280 \hobbyusepath{#2}%
2281 },
2282 show Hobby path/.code={%
2283 \hobbyshowpath{#1}%
2284 },
2285 Hobby action/.code={%
2286 \expandafter\gdef\expandafter\hobby@action\expandafter{\hobby@action#1}%
2287 },
2288 Hobby finish/.style={%
2289 Hobby action=\hobby@finish%
2290 },
2291 Hobby externalise/.is if=hobby@externalise,
2292 Hobby externalize/.is if=hobby@externalise
2293 }
2294 % \end{macrocode}
2295 %
2296 % \begin{macro}{\hobby@tikz@curveto}
2297 % This is passed to the path-generation code to translate the path into a PGF path.
2298 % \begin{macrocode}
2299 \def\hobby@tikz@curveto#1#2#3{%
2300 \pgfutil@ifundefined{tikz@timer@start}{%
2301 \expandafter\hobby@topgf\expandafter{\hobby@initial@pt}%
2302 \edef\tikz@timer@start{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2303 }{}%
2304 \hobby@topgf{#1}%
2305 \edef\tikz@timer@cont@one{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2306 \hobby@topgf{#2}%
2307 \edef\tikz@timer@cont@two{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2308 \hobby@topgf{#3}%
2309 \let\tikz@timer=\tikz@timer@curve
2310 \edef\tikz@timer@end{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2311 \ifx\hobby@collected@onpath\pgfutil@empty
2312 \else
2313 \expandafter\hobby@nodes@onpath\hobby@collected@onpath\relax\relax
2314 \fi
2315 \pgfpathcurveto{\hobby@topgf{#1}}{\hobby@topgf{#2}}{\hobby@topgf{#3}}%
2316 \hobby@topgf{#3}%
2317 \edef\tikz@timer@start{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2318 }
2319 % \end{macrocode}
2320 % \end{macro}
2321 %
2322 % \begin{macro}{\hobby@tikz@moveto}
2323 % This is passed to the path-generation code to translate the path into a PGF path.
2324 % \begin{macrocode}
2325 \def\hobby@tikz@moveto#1#2#3{%
2326 \pgfutil@ifundefined{tikz@timer@start}{%
2327 \expandafter\hobby@topgf\expandafter{\hobby@initial@pt}%
2328 \edef\tikz@timer@start{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2329 }{}%
2330 \hobby@topgf{#3}%
2331 \edef\tikz@timer@end{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2332 \def\pgf@temp{#1}%
2333 \ifx\pgf@temp\pgfutil@empty
2334 \let\tikz@timer=\tikz@timer@line
2335 \expandafter\def\expandafter\hobby@collected@onpath\expandafter{\expandafter{\expandafter}\hobby@collected@onpath}
2336 \else
2337 \hobby@topgf{#1}%
2338 \edef\tikz@timer@cont@one{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2339 \hobby@topgf{#2}%
2340 \edef\tikz@timer@cont@two{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2341 \let\tikz@timer=\tikz@timer@curve
2342 \fi
2343 \ifx\hobby@collected@onpath\pgfutil@empty
2344 \else
2345 \expandafter\hobby@nodes@onpath\hobby@collected@onpath\relax\relax
2346 \fi
2347 \pgfpathmoveto{\hobby@topgf{#3}}%
2348 \hobby@topgf{#3}%
2349 \edef\tikz@timer@start{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2350 }
2351 % \end{macrocode}
2352 % \end{macro}
2353 %
2354 % \begin{macro}{\hobby@tikz@close}
2355 % Closes a path.
2356 % \begin{macrocode}
2357 \def\hobby@tikz@close#1{%
2358 \hobby@topgf{#1}%
2359 \edef\tikz@timer@end{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2360 \let\tikz@timer=\tikz@timer@line
2361 \ifx\hobby@collected@onpath\pgfutil@empty
2362 \else
2363 \expandafter\hobby@nodes@onpath\hobby@collected@onpath\relax\relax
2364 \fi
2365 \pgfpathclose
2366 }
2367 % \end{macrocode}
2368 % \end{macro}
2369 %
2370 % \begin{macro}{\hobby@nodes@onpath}
2371 % \begin{macrocode}
2372 \def\hobby@nodes@onpath#1#2\relax{%
2373 \gdef\hobby@collected@onpath{#2}%
2374 \def\pgf@temp{#1}%
2375 \ifx\pgf@temp\pgfutil@empty
2376 \else
2377 \def\@gtempa{\relax}
2378 \ifx\pgf@temp\@gtempa
2379 \else
2380 \tikz@node@is@a@labeltrue
2381 \tikz@scan@next@command#1\pgf@stop
2382 \tikz@node@is@a@labelfalse
2383 \fi
2384 \fi
2385 }
2386 % \end{macrocode}
2387 % \end{macro}
2388 %
2389 % \begin{macro}{\curvethrough}
2390 % This is the parent command.
2391 % We initialise the path-generation code, set any parameters, and then hand over control to the point processing macro.
2392 % \begin{macrocode}
2393 \newcommand\curvethrough[2][]{%
2394 \hobbyinit\hobby@tikz@moveto\hobby@tikz@curveto\hobby@tikz@close
2395 \global\let\hobby@collected@onpath\pgfutil@empty
2396 \let\hobby@initial@pt\pgfutil@empty
2397 \hobbysetparams{#1}%
2398 \tikzset{designated Hobby path=this}%
2399 \global\let\hobby@this@opts=\pgfutil@empty
2400 \global\let\hobby@next@opts=\pgfutil@empty
2401 \let\tikz@scan@point@options=\pgfutil@empty
2402 \def\hobby@point@options{}%
2403 \tikz@scan@one@point\hobby@processpt #2 \relax%
2404 }
2405 % \end{macrocode}
2406 % \end{macro}
2407 %
2408 % \begin{macro}{\hobby@processpt}
2409 % This processes a list of points in the format \Verb+(0,0) [..] (1,1)+.
2410 % Each point is scanned by TikZ and then added to the stack to be built into the path.
2411 % If there are any remaining points, we call ourself again with them.
2412 % Otherwise, we hand over control to the path-generation code.
2413 % \begin{macrocode}
2414 \newcommand\hobby@processpt[1]{%
2415 #1%
2416 \pgfmathsetmacro\hobby@x{\the\pgf@x/1cm}%
2417 \pgfmathsetmacro\hobby@y{\the\pgf@y/1cm}%
2418 \ifx\hobby@initial@pt\pgfutil@empty
2419 \xdef\hobby@initial@pt{\hobby@x, \hobby@y}%
2420 \fi
2421 \expandafter\hobbyaddpoint\expandafter{\hobby@point@options,%
2422 point={\hobby@x, \hobby@y}}%
2423 \def\hobby@point@options{}%
2424 \let\tikz@scan@point@options=\pgfutil@empty
2425 \pgfutil@ifnextchar\relax{%
2426 \expandafter\hobbysetparams\expandafter{\hobby@this@opts}%
2427 \ifhobby@externalise
2428 \ifx\hobby@path@name\pgfutil@empty
2429 \hobbygenusepath
2430 \else
2431 \hobbygenuseifnecpath{\hobby@path@name}%
2432 \fi
2433 \else
2434 \hobbygenusepath
2435 \fi
2436 \ifx\hobby@path@name\pgfutil@empty
2437 \else
2438 \hobbysavepath{\hobby@path@name}%
2439 \fi
2440 \global\let\hobby@path@name=\pgfutil@empty
2441 }{%
2442 \pgfutil@ifnextchar.{%
2443 \hobby@swallowdots}{%
2444 \tikz@scan@one@point\hobby@processpt}}}
2445 % \end{macrocode}
2446 % \end{macro}
2447 %
2448 % \begin{macro}{\hobby@swallowdots}
2449 % Remove dots from the input stream.
2450 % \begin{macrocode}
2451 \def\hobby@swallowdots.{%
2452 \pgfutil@ifnextchar.{%
2453 \hobby@swallowdots}{%
2454 \tikz@scan@one@point\hobby@processpt}}
2455 % \end{macrocode}
2456 % \end{macro}
2457 %
2458 % There is a ``spare hook'' in the TikZ path processing code.
2459 % If TikZ encounters a path of the form \Verb+(0,0) .. (1,1)+ then it calls a macro \Verb+\tikz@curveto@auto+.
2460 % However, that macro is not defined in the TikZ code.
2461 % The following code provides a suitable definition.
2462 % To play nice, we don't install it by default but define a key (defined above) that installs it.
2463 %
2464 % \begin{macro}{\hobby@curveto@override}
2465 % \begin{macrocode}
2466 \def\hobby@curveto@override{%
2467 \hobby@curveto@delegate}
2468 % \end{macrocode}
2469 % \end{macro}
2470 %
2471 % \begin{macro}{\hobby@curveto@auto}
2472 % When we're called by TikZ, we initialise the path generation code and start adding points.
2473 % To ensure that the generation code is called, we add a lot of hooks to lots of TikZ commands.
2474 % \begin{macrocode}
2475 \def\hobby@curveto@auto{%
2476 \hobbyinit\hobby@tikz@moveto\hobby@tikz@curveto\hobby@tikz@close
2477 \expandafter\gdef\expandafter\hobby@collected@onpath\expandafter{\expandafter{\tikz@collected@onpath} }%
2478 \let\tikz@collected@onpath=\pgfutil@empty
2479 \pgfmathsetmacro\hobby@x{\the\tikz@lastx/1cm}%
2480 \pgfmathsetmacro\hobby@y{\the\tikz@lasty/1cm}%
2481 \xdef\hobby@initial@pt{\hobby@x, \hobby@y}%
2482 \expandafter\hobbysetparams\expandafter{\hobby@next@opts}%
2483 \expandafter\hobbyaddpoint\expandafter{\hobby@point@options,%
2484 point={\hobby@x, \hobby@y} }%
2485 \hobby@init@tikz@commands
2486 \tikzset{designated Hobby path=this}%
2487 \let\tikz@scan@point@options=\pgfutil@empty
2488 \global\let\hobby@action=\pgfutil@empty
2489 \global\let\hobby@this@opts=\pgfutil@empty
2490 \global\let\hobby@next@opts=\pgfutil@empty
2491 \global\let\hobby@point@options=\pgfutil@empty
2492 \tikz@scan@one@point\hobby@addfromtikz%
2493 }
2494 % \end{macrocode}
2495 % \end{macro}
2496 %
2497 % \begin{macro}{\hobby@addfromtikz}
2498 % This adds our current point to the stack.
2499 % \begin{macrocode}
2500 \def\hobby@addfromtikz#1{%
2501 #1%
2502 \tikz@make@last@position{#1}%
2503 \pgfmathsetmacro\hobby@x{\the\pgf@x/1cm}%
2504 \pgfmathsetmacro\hobby@y{\the\pgf@y/1cm}%
2505 \expandafter\hobbysetparams\expandafter{\hobby@this@opts}%
2506 \expandafter\hobbyaddpoint\expandafter{\hobby@point@options,%
2507 point={\hobby@x, \hobby@y}}%
2508 \hobby@action
2509 \global\let\hobby@this@opts=\pgfutil@empty
2510 \global\let\hobby@action=\pgfutil@empty
2511 \global\let\hobby@point@options=\pgfutil@empty
2512 \tikz@scan@next@command%
2513 }
2514 % \end{macrocode}
2515 % \end{macro}
2516 %
2517 % \begin{macro}{\hobby@init@tikz@commands}
2518 % \begin{macrocode}
2519 \def\hobby@init@tikz@commands{%
2520 \hobby@init@tikz@modcmd\tikz@movetoabs
2521 \hobby@init@tikz@modcmd\tikz@movetorel
2522 \hobby@init@tikz@modcmd\tikz@lineto
2523 \hobby@init@tikz@modcmd\tikz@rect
2524 \hobby@init@tikz@modcmd\tikz@cchar
2525 \hobby@init@tikz@modcmd\tikz@finish
2526 \hobby@init@tikz@modcmd\tikz@arcA
2527 \hobby@init@tikz@modcmd\tikz@e@char
2528 \hobby@init@tikz@modcmd\tikz@g@char
2529 \hobby@init@tikz@modcmd\tikz@schar
2530 \hobby@init@tikz@modcmd\tikz@vh@lineto
2531 \hobby@init@tikz@modcmd\tikz@pchar
2532 \hobby@init@tikz@modcmd\tikz@to
2533 \hobby@init@tikz@modcmd\pgf@stop
2534 \hobby@init@tikz@modcmd\tikz@decoration
2535 \global\let\hobby@curveto@delegate=\hobby@midcurveto@auto
2536 }
2537 % \end{macrocode}
2538 % \end{macro}
2539 %
2540 % \begin{macro}{\hobby@restore@tikz@commands}
2541 % \begin{macrocode}
2542 \def\hobby@restore@tikz@commands{%
2543 \hobby@restore@tikz@modcmd\tikz@movetoabs
2544 \hobby@restore@tikz@modcmd\tikz@movetorel
2545 \hobby@restore@tikz@modcmd\tikz@lineto
2546 \hobby@restore@tikz@modcmd\tikz@rect
2547 \hobby@restore@tikz@modcmd\tikz@cchar
2548 \hobby@restore@tikz@modcmd\tikz@finish
2549 \hobby@restore@tikz@modcmd\tikz@arcA
2550 \hobby@restore@tikz@modcmd\tikz@e@char
2551 \hobby@restore@tikz@modcmd\tikz@g@char
2552 \hobby@restore@tikz@modcmd\tikz@schar
2553 \hobby@restore@tikz@modcmd\tikz@vh@lineto
2554 \hobby@restore@tikz@modcmd\tikz@pchar
2555 \hobby@restore@tikz@modcmd\tikz@to
2556 \hobby@restore@tikz@modcmd\pgf@stop
2557 \hobby@restore@tikz@modcmd\tikz@decoration
2558 \global\let\hobby@curveto@delegate=\hobby@curveto@auto
2559 }
2560 % \end{macrocode}
2561 % \end{macro}
2562 %
2563 % \begin{macro}{\hobby@init@tikz@modcmd}
2564 % \begin{macrocode}
2565 \def\hobby@init@tikz@modcmd#1{%
2566 \expandafter\global\expandafter\let\csname hobby@orig@\string#1\endcsname=#1%
2567 \gdef#1{\hobby@finish#1}%
2568 }
2569 % \end{macrocode}
2570 % \end{macro}
2571 %
2572 % \begin{macro}{\hobby@restore@tikz@modcmd}
2573 % \begin{macrocode}
2574 \def\hobby@restore@tikz@modcmd#1{%
2575 \expandafter\global\expandafter\let\expandafter#1\csname hobby@orig@\string#1\endcsname%
2576 }
2577 % \end{macrocode}
2578 % \end{macro}
2579 %
2580 % \begin{macro}{\hobby@midcurveto@auto}
2581 % \begin{macrocode}
2582 \def\hobby@midcurveto@auto{%
2583 \expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\hobby@collected@onpath\expandafter\expandafter\expandafter{\expandafter\hobby@collected@onpath\expandafter{\tikz@collected@onpath} }%
2584 \let\tikz@collected@onpath=\pgfutil@empty
2585 \let\tikz@scan@point@options=\pgfutil@empty
2586 \global\let\hobby@action=\pgfutil@empty
2587 \global\let\hobby@this@opts=\pgfutil@empty
2588 \global\let\hobby@point@options=\pgfutil@empty
2589 \tikz@scan@one@point\hobby@addfromtikz%
2590 }
2591 % \end{macrocode}
2592 % \end{macro}
2593 %
2594 % \begin{macro}{\hobby@finish}
2595 % \begin{macrocode}
2596 \def\hobby@finish{%
2597 \hobby@restore@tikz@commands
2598 \ifhobby@externalise
2599 \ifx\hobby@path@name\pgfutil@empty
2600 \hobbygenusepath
2601 \else
2602 \hobbygenuseifnecpath{\hobby@path@name}%
2603 \fi
2604 \else
2605 \hobbygenusepath
2606 \fi
2607 \ifx\hobby@path@name\pgfutil@empty
2608 \else
2609 \hobbysavepath{\hobby@path@name}%
2610 \fi
2611 \global\let\hobby@path@name=\pgfutil@empty
2612 \tikzset{designated Hobby path=next}%
2613 }
2614 % \end{macrocode}
2615 % \end{macro}
2616 %
2617 % \begin{macro}{quick curve through}
2618 % The \Verb+quick curve through+ is a \Verb+to path+ which does the ``quick'' version of Hobby's algorithm.
2619 % The syntax is as with the \Verb+curve through+: to pass the midpoints as the argument to the style.
2620 % We need to pass three points to the auxiliary macro.
2621 % These are passed as \Verb+\hobby@qpoints+, \Verb+\hobby@qpointa+, and the current point.
2622 % Then these get cycled round for the next triple.
2623 % The path gets built up and stored as \Verb+\hobby@quick@path+.
2624 % We also have to remember the angle computed for the next round.
2625 % \begin{macrocode}
2626 \tikzset{
2627 quick curve through/.style={%
2628 to path={%
2629 \pgfextra{%
2630 % \end{macrocode}
2631 % Scan the starting point and store the coordinates in \Verb+\hobby@qpointa+
2632 % \begin{macrocode}
2633 \let\hobby@next@qbreak=\relax
2634 \let\hobby@next@qblank=\relax
2635 \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)%
2636 \tikz@make@last@position{\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2637 \edef\hobby@qpoints{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2638 % \end{macrocode}
2639 % Blank the path and auxiliary macros.
2640 % \begin{macrocode}
2641 \def\hobby@qpointa{}%
2642 \def\hobby@quick@path{}%
2643 \def\hobby@angle{}%
2644 \let\hobby@quick@curveto=\hobby@quick@makepath
2645 % \end{macrocode}
2646 % Now start parsing the rest of the coordinates.
2647 % \begin{macrocode}
2648 \tikz@scan@one@point\hobby@quickfirst #1 (\tikztotarget)\relax
2649 }
2650 % \end{macrocode}
2651 % Invoke the path
2652 % \begin{macrocode}
2653 \hobby@quick@path
2654 }
2655 },
2656 quick hobby/blank curve/.is choice,
2657 quick hobby/blank curve/true/.code={%
2658 \gdef\hobby@next@qblank{%
2659 \qhobby@blanktrue
2660 \global\let\hobby@next@qblank=\relax
2661 }%
2662 },
2663 quick hobby/blank curve/false/.code={%
2664 \gdef\hobby@next@qblank{%
2665 \qhobby@blankfalse
2666 \global\let\hobby@next@qblank=\relax
2667 }%
2668 },
2669 quick hobby/blank curve/once/.code={%
2670 \gdef\hobby@next@qblank{%
2671 \qhobby@blanktrue
2672 \gdef\hobby@next@qblank{%
2673 \qhobby@blankfalse
2674 \global\let\hobby@next@qblank=\relax
2675 }%
2676 }%
2677 },
2678 quick hobby/blank curve/.default=true,
2679 quick hobby/break curve/.is choice,
2680 quick hobby/break curve/true/.code={%
2681 \gdef\hobby@next@qbreak{%
2682 \qhobby@breaktrue
2683 \global\let\hobby@next@qbreak=\relax
2684 }%
2685 },
2686 quick hobby/break curve/false/.code={%
2687 \gdef\hobby@next@qbreak{%
2688 \qhobby@breakfalse
2689 \global\let\hobby@next@qbreak=\relax
2690 }%
2691 },
2692 quick hobby/break curve/once/.code={%
2693 \gdef\hobby@next@qbreak{%
2694 \qhobby@breaktrue
2695 \gdef\hobby@next@qbreak{%
2696 \qhobby@breakfalse
2697 \global\let\hobby@next@qbreak=\relax
2698 }%
2699 }%
2700 },
2701 quick hobby/break curve/.default=true,
2702 }
2703 \newif\ifqhobby@break
2704 \newif\ifqhobby@blank
2705 % \end{macrocode}
2706 % \end{macro}
2707 %
2708 % Add plot handlers
2709 % \begin{macrocode}
2710 \tikzoption{hobby}[]{\let\tikz@plot@handler=\pgfplothandlerhobby}
2711 \tikzoption{quick hobby}[]{\let\tikz@plot@handler=\pgfplothandlerquickhobby}
2712 \tikzoption{closed hobby}[]{\let\tikz@plot@handler=\pgfplothandlerclosedhobby}
2713 % \end{macrocode}
2714 %
2715 % \begin{macro}{\hobby@quickfirst}
2716 % The first time around we just set the next point.
2717 % \begin{macrocode}
2718 \def\hobby@quickfirst#1{%
2719 #1%
2720 \xdef\hobby@qpointa{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2721 \tikz@make@last@position{\hobby@qpointa}%
2722 % \end{macrocode}
2723 % Now a check to ensure that we have more points.
2724 % \begin{macrocode}
2725 \pgfutil@ifnextchar\relax{%
2726 % \end{macrocode}
2727 % Ooops, no more points.
2728 % That's not good.
2729 % Bail-out.
2730 % \begin{macrocode}
2731 \xdef\hobby@quick@path{ -- (\the\pgf@x,\the\pgf@y)}%
2732 }{%
2733 % \end{macrocode}
2734 % Okay, have more points.
2735 % Phew.
2736 % Call the next round.
2737 % If we have dots, swallow them.
2738 % \begin{macrocode}
2739 \pgfutil@ifnextchar.{%
2740 \hobby@qswallowdots}{%
2741 \tikz@scan@one@point\hobby@quick}}}
2742 % \end{macrocode}
2743 % \end{macro}
2744 %
2745 % \begin{macro}{\hobby@qswallowdots}
2746 % Remove dots from the input stream.
2747 % \begin{macrocode}
2748 \def\hobby@qswallowdots.{%
2749 \pgfutil@ifnextchar.{%
2750 \hobby@qswallowdots}{%
2751 \tikz@scan@one@point\hobby@quick}}
2752 % \end{macrocode}
2753 % \end{macro}
2754 %
2755 % \begin{macro}{\hobby@quick}
2756 % This is our wrapper function that handles the loop.
2757 % \begin{macrocode}
2758 \def\hobby@quick#1{%
2759 \hobby@quick@compute{#1}%
2760 \tikz@make@last@position{\hobby@qpointa}%
2761 \pgfutil@ifnextchar\relax{%
2762 % \end{macrocode}
2763 % End of loop
2764 % \begin{macrocode}
2765 \hobby@quick@computeend%
2766 }{%
2767 % \end{macrocode}
2768 % More to go, scan in the next coordinate and off we go again.
2769 % \begin{macrocode}
2770 \pgfutil@ifnextchar.{%
2771 \hobby@qswallowdots}{%
2772 \tikz@scan@one@point\hobby@quick}}}
2773 % \end{macrocode}
2774 % \end{macro}
2775 %
2776 % \begin{macro}{\hobby@quick@makepath}
2777 % Path constructor for \Verb+to path+ use.
2778 % \begin{macrocode}
2779 \def\hobby@quick@makepath#1#2#3{%
2780 #1%
2781 \pgf@xa=\pgf@x\relax
2782 \pgf@ya=\pgf@y\relax
2783 #2%
2784 \pgf@xb=\pgf@x\relax
2785 \pgf@yb=\pgf@y\relax
2786 #3%
2787 \ifqhobby@blank
2788 \xdef\hobby@quick@path{\hobby@quick@path (\the\pgf@x,\the\pgf@y)}%
2789 \else
2790 \xdef\hobby@quick@path{\hobby@quick@path .. controls%
2791 (\the\pgf@xa,\the\pgf@ya) and (\the\pgf@xb,\the\pgf@yb) .. (\the\pgf@x,\the\pgf@y) }%
2792 \fi
2793 \ifqhobby@break
2794 \xdef\hobby@quick@path{\hobby@quick@path +(0,0)}%
2795 \fi
2796 \hobby@next@qbreak
2797 \hobby@next@qblank
2798 }
2799 % \end{macrocode}
2800 % \end{macro}
2801 %
2802 % \begin{macro}{\hobby@qcurveto@auto}
2803 % Uses the ``quick'' method for the shortcut syntax.
2804 % \begin{macrocode}
2805 \def\hobby@qcurveto@auto{%
2806 \global\let\hobby@next@qbreak=\relax
2807 \global\let\hobby@next@qblank=\relax
2808 \xdef\hobby@qpoints{\noexpand\pgfqpoint{\the\tikz@lastx}{\the\tikz@lasty}}%
2809 \gdef\hobby@qpointa{}%
2810 \gdef\hobby@quick@path{}%
2811 \gdef\hobby@angle{}%
2812 \global\let\hobby@quick@curveto=\hobby@quick@makepathauto
2813 \hobby@qinit@tikz@commands
2814 \let\tikz@scan@point@options=\pgfutil@empty
2815 \global\let\hobby@action=\pgfutil@empty
2816 \global\let\hobby@point@options=\pgfutil@empty
2817 \tikz@scan@one@point\hobby@qfirst@auto}
2818 % \end{macrocode}
2819 % \end{macro}
2820 %
2821 % \begin{macro}{\hobby@qmidcurveto@auto}
2822 % \begin{macrocode}
2823 \def\hobby@qmidcurveto@auto{%
2824 \let\tikz@scan@point@options=\pgfutil@empty
2825 \global\let\hobby@action=\pgfutil@empty
2826 \global\let\hobby@point@options=\pgfutil@empty
2827 \tikz@scan@one@point\hobby@qaddfromtikz}
2828 % \end{macrocode}
2829 % \end{macro}
2830 %
2831 % \begin{macro}{\hobby@qfirst@auto}
2832 % \begin{macrocode}
2833 \def\hobby@qfirst@auto#1{%
2834 #1%
2835 \xdef\hobby@qpointa{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2836 \tikz@make@last@position{\hobby@qpointa}%
2837 \tikz@scan@next@command%
2838 }
2839 % \end{macrocode}
2840 % \end{macro}
2841 %
2842 % \begin{macro}{\hobby@quick@makepathauto}
2843 % Path constructor for shortcut method to use.
2844 % \begin{macrocode}
2845 \def\hobby@quick@makepathauto#1#2#3{%
2846 #1%
2847 \pgf@xa=\pgf@x\relax
2848 \pgf@ya=\pgf@y\relax
2849 #2%
2850 \pgf@xb=\pgf@x\relax
2851 \pgf@yb=\pgf@y\relax
2852 #3%
2853 \ifqhobby@blank
2854 \edef\hobby@temp{%
2855 \noexpand\pgfpathmoveto{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2856 }%
2857 \hobby@temp
2858 \else
2859 \edef\hobby@temp{%
2860 \noexpand\pgfpathcurveto{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}%
2861 {\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}%
2862 {\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2863 }%
2864 \hobby@temp
2865 \fi
2866 \ifqhobby@break
2867 #3%
2868 \edef\hobby@temp{%
2869 \noexpand\pgfpathmoveto{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
2870 }%
2871 \hobby@temp
2872 \fi
2873 \hobby@next@qbreak
2874 \hobby@next@qblank
2875 }
2876 % \end{macrocode}
2877 % \end{macro}
2878 %
2879 % \begin{macro}{\hobby@qaddfromtikz}
2880 % This adds our current point to the stack.
2881 % \begin{macrocode}
2882 \def\hobby@qaddfromtikz#1{%
2883 \hobby@quick@compute{#1}%
2884 \tikz@make@last@position{\hobby@qpointa}%
2885 \tikz@scan@next@command%
2886 }
2887 % \end{macrocode}
2888 % \end{macro}
2889 %
2890 % \begin{macro}{\hobby@qinit@tikz@commands}
2891 % \begin{macrocode}
2892 \def\hobby@qinit@tikz@commands{%
2893 \hobby@qinit@tikz@modcmd\tikz@movetoabs
2894 \hobby@qinit@tikz@modcmd\tikz@movetorel
2895 \hobby@qinit@tikz@modcmd\tikz@lineto
2896 \hobby@qinit@tikz@modcmd\tikz@rect
2897 \hobby@qinit@tikz@modcmd\tikz@cchar
2898 \hobby@qinit@tikz@modcmd\tikz@finish
2899 \hobby@qinit@tikz@modcmd\tikz@arcA
2900 \hobby@qinit@tikz@modcmd\tikz@e@char
2901 \hobby@qinit@tikz@modcmd\tikz@g@char
2902 \hobby@qinit@tikz@modcmd\tikz@schar
2903 \hobby@qinit@tikz@modcmd\tikz@vh@lineto
2904 \hobby@qinit@tikz@modcmd\tikz@pchar
2905 \hobby@qinit@tikz@modcmd\tikz@to
2906 \hobby@qinit@tikz@modcmd\pgf@stop
2907 \hobby@qinit@tikz@modcmd\tikz@decoration
2908 \hobby@qinit@tikz@modcmd\tikz@@close
2909 \global\let\hobby@curveto@delegate=\hobby@qmidcurveto@auto
2910 }
2911 % \end{macrocode}
2912 % \end{macro}
2913 %
2914 % \begin{macro}{\hobby@qrestore@tikz@commands}
2915 % \begin{macrocode}
2916 \def\hobby@qrestore@tikz@commands{%
2917 \hobby@restore@tikz@modcmd\tikz@movetoabs
2918 \hobby@restore@tikz@modcmd\tikz@movetorel
2919 \hobby@restore@tikz@modcmd\tikz@lineto
2920 \hobby@restore@tikz@modcmd\tikz@rect
2921 \hobby@restore@tikz@modcmd\tikz@cchar
2922 \hobby@restore@tikz@modcmd\tikz@finish
2923 \hobby@restore@tikz@modcmd\tikz@arcA
2924 \hobby@restore@tikz@modcmd\tikz@e@char
2925 \hobby@restore@tikz@modcmd\tikz@g@char
2926 \hobby@restore@tikz@modcmd\tikz@schar
2927 \hobby@restore@tikz@modcmd\tikz@vh@lineto
2928 \hobby@restore@tikz@modcmd\tikz@pchar
2929 \hobby@restore@tikz@modcmd\tikz@to
2930 \hobby@restore@tikz@modcmd\pgf@stop
2931 \hobby@restore@tikz@modcmd\tikz@decoration
2932 \hobby@restore@tikz@modcmd\tikz@@close
2933 \global\let\hobby@curveto@delegate=\hobby@qcurveto@auto
2934 }
2935 % \end{macrocode}
2936 % \end{macro}
2937 %
2938 % \begin{macro}{\hobby@qinit@tikz@modcmd}
2939 % \begin{macrocode}
2940 \def\hobby@qinit@tikz@modcmd#1{%
2941 \expandafter\global\expandafter\let\csname hobby@orig@\string#1\endcsname=#1%
2942 \gdef#1{\hobby@qfinish#1}%
2943 }
2944 % \end{macrocode}
2945 % \end{macro}
2946 %
2947 % \begin{macro}{\hobby@qfinish}
2948 % \begin{macrocode}
2949 \def\hobby@qfinish{%
2950 \hobby@quick@computeend
2951 \hobby@qrestore@tikz@commands
2952 }
2953 % \end{macrocode}
2954 % \end{macro}
2955
2956 % \iffalse
2957 %</tikzlibrary>
2958 % \fi
2959 %
2960 % \subsection{Arrays}
2961 %
2962 % \iffalse
2963 %<*array>
2964 % \fi
2965 %
2966 %
2967 % A lot of our data structures are really arrays.
2968 % These are implemented as \LaTeX3 ``property lists''.
2969 % For ease of use, an array is a property list with numeric entries together with entries ``base'' and ``top'' which hold the lowest and highest indices that have been set.
2970 %
2971 % \begin{macrocode}
2972 \RequirePackage{expl3}
2973 \ExplSyntaxOn
2974 % \end{macrocode}
2975 % Some auxiliary variables.
2976 % \begin{macrocode}
2977 \tl_new:N \l_array_tmp_tl
2978 \tl_new:N \l_array_show_tl
2979 \int_new:N \l_array_base_int
2980 \int_new:N \l_array_top_int
2981 \int_new:N \l_array_tmp_int
2982 \int_new:N \g_array_map_int
2983 % \end{macrocode}
2984 % The global variable \Verb+\g_array_base_int+ says what index a blank array should start with when pushed or unshifted.
2985 % \begin{macrocode}
2986 \int_new:N \g_array_base_int
2987 \int_gset:Nn \g_array_base_int {0}
2988 % \end{macrocode}
2989 % \begin{macro}{\array_adjust_ends:Nn}
2990 % This ensures that the ``base'' and ``top'' are big enough to include the given index.
2991 % \begin{macrocode}
2992 \cs_new:Npn \array_adjust_ends:Nn #1#2 {
2993 \prop_get:NnNTF #1 {base} \l_tmpa_tl
2994 {
2995 \int_compare:nNnTF {\l_tmpa_tl} > {#2}
2996 {
2997 \prop_put:Nnx #1 {base} {\int_eval:n {#2}}
2998 }
2999 {}
3000 }
3001 {
3002 \prop_put:Nnx #1 {base} {\int_eval:n {#2}}
3003 }
3004 \prop_get:NnNTF #1 {top} \l_tmpa_tl
3005 {
3006 \int_compare:nNnTF {\l_tmpa_tl} < {#2}
3007 {
3008 \prop_put:Nnx #1 {top} {\int_eval:n {#2}}
3009 }
3010 {}
3011 }
3012 {
3013 \prop_put:Nnx #1 {top} {\int_eval:n {#2}}
3014 }
3015 }
3016 % \end{macrocode}
3017 % \end{macro}
3018 %
3019 % \begin{macro}{\array_gadjust_ends:Nn}
3020 % This ensures that the ``base'' and ``top'' are big enough to include the given index.
3021 % (Global version)
3022 % \begin{macrocode}
3023 \cs_new:Npn \array_gadjust_ends:Nn #1#2 {
3024 \prop_get:NnNTF #1 {base} \l_tmpa_tl
3025 {
3026 \int_compare:nNnTF {\l_tmpa_tl} > {#2}
3027 {
3028 \prop_gput:Nnx #1 {base} {\int_eval:n {#2}}
3029 }
3030 {}
3031 }
3032 {
3033 \prop_gput:Nnx #1 {base} {\int_eval:n {#2}}
3034 }
3035 \prop_get:NnNTF #1 {top} \l_tmpa_tl
3036 {
3037 \int_compare:nNnTF {\l_tmpa_tl} < {#2}
3038 {
3039 \prop_gput:Nnx #1 {top} {\int_eval:n {#2}}
3040 }
3041 {}
3042 }
3043 {
3044 \prop_gput:Nnx #1 {top} {\int_eval:n {#2}}
3045 }
3046 }
3047 % \end{macrocode}
3048 % \end{macro}
3049 %
3050 % \begin{macro}{\array_put:Nnn}
3051 % When adding a value to an array we have to adjust the ends.
3052 % \begin{macrocode}
3053 \cs_new:Npn \array_put:Nnn #1#2#3 {
3054 \exp_args:NNx \prop_put:Nnn #1 {\int_eval:n {#2}} {#3}
3055 \array_adjust_ends:Nn #1{#2}
3056 }
3057 \cs_generate_variant:Nn \array_put:Nnn {Nnx}
3058 % \end{macrocode}
3059 % \end{macro}
3060 %
3061 % \begin{macro}{\array_gput:Nnn}
3062 % When adding a value to an array we have to adjust the ends.
3063 % (Global version)
3064 % \begin{macrocode}
3065 \cs_new:Npn \array_gput:Nnn #1#2#3 {
3066 \exp_args:NNx \prop_gput:Nnn #1 {\int_eval:n {#2}} {#3}
3067 \array_gadjust_ends:Nn #1{#2}
3068 }
3069 \cs_generate_variant:Nn \array_gput:Nnn {Nnx}
3070 % \end{macrocode}
3071 % \end{macro}
3072 %
3073 % \begin{macro}{\array_get:NnN}
3074 % \begin{macrocode}
3075 \cs_new:Npn \array_get:NnN #1#2#3 {
3076 \exp_args:NNx \prop_get:NnN #1 {\int_eval:n {#2}} #3
3077 }
3078 % \end{macrocode}
3079 % \end{macro}
3080 %
3081 % \begin{macro}[EXP]{\array_get:Nn}
3082 % \begin{macrocode}
3083 \cs_new:Npn \array_get:Nn #1#2 {
3084 \exp_args:NNf \prop_item:Nn #1 { \int_eval:n {#2} }
3085 }
3086 % \end{macrocode}
3087 % \end{macro}
3088 %
3089 % \begin{macro}{\array_get:NnNTF}
3090 % \begin{macrocode}
3091 \cs_new:Npn \array_get:NnNTF #1#2#3#4#5 {
3092 \exp_args:NNx \prop_get:NnNTF #1 {\int_eval:n {#2}} #3 {#4}{#5}
3093 }
3094 % \end{macrocode}
3095 % \end{macro}
3096 %
3097 % \begin{macro}{\array_if_empty:NTF}
3098 % \begin{macrocode}
3099 \prg_new_conditional:Npnn \array_if_empty:N #1 { p, T, F, TF }
3100 {
3101 \if_meaning:w #1 \c_empty_prop
3102 \prg_return_true:
3103 \else:
3104 \prg_return_false:
3105 \fi:
3106 }
3107 % \end{macrocode}
3108 % \end{macro}
3109 %
3110 % \begin{macro}{\array_if_exist:NTF}
3111 % \begin{macrocode}
3112 \prg_new_eq_conditional:NNn \array_if_exist:N \cs_if_exist:N { p, T, F, TF }
3113 % \end{macrocode}
3114 % \end{macro}
3115 %
3116 % \begin{macro}{\array_new:N}
3117 % \begin{macrocode}
3118 \cs_new_eq:NN \array_new:N \prop_new:N
3119 % \end{macrocode}
3120 % \end{macro}
3121 %
3122 % \begin{macro}{\array_clear:N}
3123 % \begin{macrocode}
3124 \cs_new_eq:NN \array_clear:N \prop_clear:N
3125 % \end{macrocode}
3126 % \end{macro}
3127 %
3128 % \begin{macro}{\array_gclear:N}
3129 % \begin{macrocode}
3130 \cs_new_eq:NN \array_gclear:N \prop_gclear:N
3131 % \end{macrocode}
3132 % \end{macro}
3133 %
3134 % \begin{macro}{\array_map_function}
3135 % When stepping through an array, we want to iterate in order so a simple wrapper to \Verb+\prop_map_function+ is not enough.
3136 % This maps through every value from the base to the top so the function should be prepared to deal with a \Verb+\q_no_value+.
3137 % \begin{macrocode}
3138 \cs_new:Npn \array_map_function:NN #1#2
3139 {
3140 \array_if_empty:NTF #1 {} {
3141 \prop_get:NnNTF #1 {base} \l_array_tmp_tl {
3142 \int_set:Nn \l_array_base_int {\l_array_tmp_tl}
3143 }{
3144 \int_set:Nn \l_array_base_int {0}
3145 }
3146 \prop_get:NnNTF #1 {top} \l_array_tmp_tl {
3147 \int_set:Nn \l_array_top_int {\l_array_tmp_tl}
3148 }{
3149 \int_set:Nn \l_array_top_int {0}
3150 }
3151 \int_step_inline:nnnn {\l_array_base_int} {1} {\l_array_top_int} {
3152 \array_get:NnN #1 {##1} \l_array_tmp_tl
3153 \exp_args:NnV #2 {##1} \l_array_tmp_tl
3154 }
3155 } {}
3156 }
3157 \cs_generate_variant:Nn \array_map_function:NN { Nc }
3158 \cs_generate_variant:Nn \array_map_function:NN { c , cc }
3159 % \end{macrocode}
3160 % \end{macro}
3161 %
3162 % \begin{macro}{\array_reverse_map_function}
3163 % This steps through the array in reverse order.
3164 % \begin{macrocode}
3165 \cs_new:Npn \array_reverse_map_function:NN #1#2
3166 {
3167 \array_if_empty:NTF #1 {} {
3168 \prop_get:NnNTF #1 {base} \l_array_tmp_tl {
3169 \int_set:Nn \l_array_base_int {\l_array_tmp_tl}
3170 }{
3171 \int_set:Nn \l_array_base_int {0}
3172 }
3173 \prop_get:NnNTF #1 {top} \l_array_tmp_tl {
3174 \int_set:Nn \l_array_top_int {\l_array_tmp_tl}
3175 }{
3176 \int_set:Nn \l_array_top_int {0}
3177 }
3178 \int_step_inline:nnnn {\l_array_top_int} {-1} {\l_array_base_int} {
3179 \array_get:NnN #1 {##1} \l_array_tmp_tl
3180 \exp_args:Nno #2 {##1} \l_array_tmp_tl
3181 }
3182 } {}
3183 }
3184 \cs_generate_variant:Nn \array_reverse_map_function:NN { Nc }
3185 \cs_generate_variant:Nn \array_reverse_map_function:NN { c , cc }
3186 % \end{macrocode}
3187 % \end{macro}
3188 %
3189 % \begin{macro}{\array_map_inline:Nn}
3190 % Inline version of the above.
3191 % \begin{macrocode}
3192 \cs_new_protected:Npn \array_map_inline:Nn #1#2
3193 {
3194 \int_gincr:N \g_array_map_int
3195 \cs_gset:cpn { array_map_inline_ \int_use:N \g_array_map_int :nn }
3196 ##1##2 {#2}
3197 \exp_args:NNc \array_map_function:NN #1
3198 { array_map_inline_ \int_use:N \g_array_map_int :nn }
3199 \prg_break_point:Nn \array_map_break: { \int_gdecr:N \g_array_map_int }
3200 }
3201 \cs_generate_variant:Nn \array_map_inline:Nn { c }
3202 % \end{macrocode}
3203 % \end{macro}
3204 %
3205 % \begin{macro}{\array_reverse_map_inline:Nn}
3206 % Inline version of the above.
3207 % \begin{macrocode}
3208 \cs_new_protected:Npn \array_reverse_map_inline:Nn #1#2
3209 {
3210 \int_gincr:N \g_array_map_int
3211 \cs_gset:cpn { array_map_inline_ \int_use:N \g_array_map_int :nn }
3212 ##1##2 {#2}
3213 \exp_args:NNc \array_reverse_map_function:NN #1
3214 { array_map_inline_ \int_use:N \g_array_map_int :nn }
3215 \prg_break_point:Nn \array_map_break: { \int_gdecr:N \g_array_map_int }
3216 }
3217 \cs_generate_variant:Nn \array_reverse_map_inline:Nn { c }
3218 % \end{macrocode}
3219 % \end{macro}
3220 %
3221 % \begin{macro}{\array_map_break:}
3222 % \begin{macrocode}
3223 \cs_new_nopar:Npn \array_map_break:
3224 { \prg_map_break:Nn \array_map_break: { } }
3225 \cs_new_nopar:Npn \array_map_break:n
3226 { \prg_map_break:Nn \array_map_break: }
3227 % \end{macrocode}
3228 % \end{macro}
3229 %
3230 % For displaying arrays, we need some messages.
3231 % \begin{macrocode}
3232 \msg_new:nnn { kernel } { show-array }
3233 {
3234 The~array~\token_to_str:N #1~
3235 \array_if_empty:NTF #1
3236 { is~empty }
3237 { contains~the~items~(without~outer~braces): }
3238 }
3239 % \end{macrocode}
3240 %
3241 % \begin{macro}{\array_show:N}
3242 % Mapping through an array isn't expandable so we have to set a token list to its contents first before passing it to the message handler.
3243 % \begin{macrocode}
3244 \cs_new_protected:Npn \array_show:N #1
3245 {
3246 \__msg_show_variable:NNNnn
3247 #1
3248 \array_if_exist:NTF
3249 \array_if_empty:NTF
3250 { array }
3251 { \array_map_function:NN #1 \__msg_show_item:nn }
3252 }
3253 \cs_generate_variant:Nn \array_show:N { c }
3254 % \end{macrocode}
3255 % \end{macro}
3256 %
3257 % \begin{macro}{\array_push:Nn}
3258 % \begin{macrocode}
3259 \cs_new_protected:Npn \array_push:Nn #1#2
3260 {
3261 \prop_get:NnNTF #1 {top} \l_array_tmp_tl
3262 {
3263 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3264 \int_incr:N \l_array_tmp_int
3265 \array_put:Nnn #1 {\l_array_tmp_int} {#2}
3266 }
3267 {
3268 \array_put:Nnn #1 {\g_array_base_int} {#2}
3269 }
3270 }
3271 \cs_generate_variant:Nn \array_push:Nn {Nx}
3272 % \end{macrocode}
3273 % \end{macro}
3274 %
3275 % \begin{macro}{\array_gpush:Nn}
3276 % \begin{macrocode}
3277 \cs_new_protected:Npn \array_gpush:Nn #1#2
3278 {
3279 \prop_get:NnNTF #1 {top} \l_array_tmp_tl
3280 {
3281 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3282 \int_incr:N \l_array_tmp_int
3283 \array_gput:Nnn #1 {\l_array_tmp_int} {#2}
3284 }
3285 {
3286 \array_gput:Nnn #1 {\g_array_base_int} {#2}
3287 }
3288 }
3289 \cs_generate_variant:Nn \array_gpush:Nn {Nx}
3290 % \end{macrocode}
3291 % \end{macro}
3292 %
3293 % \begin{macro}{\array_unshift:Nn}
3294 % \begin{macrocode}
3295 \cs_new_protected:Npn \array_unshift:Nn #1#2
3296 {
3297 \prop_get:NnNTF #1 {base} \l_array_tmp_tl
3298 {
3299 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3300 \int_decr:N \l_array_tmp_int
3301 \array_put:Nnn #1 {\l_array_tmp_int} {#2}
3302 }
3303 {
3304 \array_put:Nnn #1 {\g_array_base_int} {#2}
3305 }
3306 }
3307 \cs_generate_variant:Nn \array_unshift:Nn {Nx}
3308 % \end{macrocode}
3309 % \end{macro}
3310 %
3311 % \begin{macro}{\array_gunshift:Nn}
3312 % \begin{macrocode}
3313 \cs_new_protected:Npn \array_gunshift:Nn #1#2
3314 {
3315 \prop_get:NnNTF #1 {base} \l_array_tmp_tl
3316 {
3317 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3318 \int_decr:N \l_array_tmp_int
3319 \array_gput:Nnn #1 {\l_array_tmp_int} {#2}
3320 }
3321 {
3322 \array_gput:Nnn #1 {\g_array_base_int} {#2}
3323 }
3324 }
3325 \cs_generate_variant:Nn \array_gunshift:Nn {Nx}
3326 % \end{macrocode}
3327 % \end{macro}
3328 %
3329 % \begin{macro}{\array_pop:NN}
3330 % \begin{macrocode}
3331 \cs_new_protected:Npn \array_pop:NN #1#2
3332 {
3333 \prop_get:NnN #1 {top} \l_array_tmp_tl
3334 \array_get:NnN #1 {\l_array_tmp_tl} #2
3335 \array_del:Nn #1 {\l_array_tmp_tl}
3336 }
3337 % \end{macrocode}
3338 % \end{macro}
3339 %
3340 % \begin{macro}{\array_gpop:NN}
3341 % \begin{macrocode}
3342 \cs_new_protected:Npn \array_gpop:NN #1#2
3343 {
3344 \prop_get:NnN #1 {top} \l_array_tmp_tl
3345 \array_get:NnN #1 {\l_array_tmp_tl} #2
3346 \array_gdel:Nn #1 {\l_array_tmp_tl}
3347 }
3348 % \end{macrocode}
3349 % \end{macro}
3350 %
3351 % \begin{macro}{\array_shift:NN}
3352 % \begin{macrocode}
3353 \cs_new_protected:Npn \array_shift:NN #1#2
3354 {
3355 \prop_get:NnN #1 {base} \l_array_tmp_tl
3356 \array_get:NnN #1 {\l_array_tmp_tl} #2
3357 \array_del:Nn #1 {\l_array_tmp_tl}
3358 }
3359 % \end{macrocode}
3360 % \end{macro}
3361 %
3362 % \begin{macro}{\array_gshift:NN}
3363 % \begin{macrocode}
3364 \cs_new_protected:Npn \array_gshift:NN #1#2
3365 {
3366 \prop_get:NnN #1 {base} \l_array_tmp_tl
3367 \array_get:NnN #1 {\l_array_tmp_tl} #2
3368 \array_gdel:Nn #1 {\l_array_tmp_tl}
3369 }
3370 % \end{macrocode}
3371 % \end{macro}
3372 %
3373 % \begin{macro}{\array_top:NN}
3374 % \begin{macrocode}
3375 \cs_new_protected:Npn \array_top:NN #1#2
3376 {
3377 \prop_get:NnN #1 {top} \l_array_tmp_tl
3378 \array_get:NnN #1 {\l_array_tmp_tl} #2
3379 }
3380 % \end{macrocode}
3381 % \end{macro}
3382 %
3383 % \begin{macro}{\array_base:NN}
3384 % \begin{macrocode}
3385 \cs_new_protected:Npn \array_base:NN #1#2
3386 {
3387 \prop_get:NnN #1 {base} \l_array_tmp_tl
3388 \array_get:NnN #1 {\l_array_tmp_tl} #2
3389 }
3390 % \end{macrocode}
3391 % \end{macro}
3392 %
3393 % \begin{macro}{\array_top:N}
3394 % \begin{macrocode}
3395 \cs_new:Npn \array_top:N #1
3396 {
3397 \array_get:Nn #1 {\prop_item:Nn #1 {top}}
3398 }
3399 % \end{macrocode}
3400 % \end{macro}
3401 %
3402 % \begin{macro}{\array_base:N}
3403 % \begin{macrocode}
3404 \cs_new:Npn \array_base:N #1
3405 {
3406 \array_get:Nn #1 {\prop_item:Nn #1 {base}}
3407 }
3408 % \end{macrocode}
3409 % \end{macro}
3410 %
3411 % \begin{macro}{\array_del:Nn}
3412 % \begin{macrocode}
3413 \cs_new_protected:Npn \array_del:Nn #1#2
3414 {
3415 \exp_args:NNx \prop_pop:Nn #1 {\int_eval:n {#2}}
3416 \int_set:Nn \l_array_tmp_int {0}
3417 \array_map_inline:Nn #1 {
3418 \tl_if_eq:NNTF {##2} {\q_no_value} {}
3419 {
3420 \int_incr:N \l_array_tmp_int
3421 }
3422 }
3423 \int_compare:nNnTF {\l_array_tmp_int} = {0}
3424 {
3425 \prop_clear:N #1
3426 }
3427 {
3428 \prop_get:NnN #1 {top} \l_array_tmp_tl
3429 \int_compare:nNnTF {#2} = {\l_array_tmp_tl} {
3430 \prop_get:NnN #1 {base} \l_array_tmp_tl
3431 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3432 \array_map_inline:Nn #1 {
3433 \tl_if_eq:NNTF {##2} {\q_no_value} {}
3434 {
3435 \int_compare:nNnTF {\l_array_tmp_int} < {##1} {
3436 \int_set:Nn \l_array_tmp_int {##1}
3437 }{}
3438 }
3439 }
3440 \prop_put:Nnx #1 {top} {\int_use:N \l_array_tmp_int}
3441 }{}
3442 \prop_get:NnN #1 {base} \l_array_tmp_tl
3443 \int_compare:nNnTF {#2} = {\l_array_tmp_tl} {
3444 \prop_get:NnN #1 {top} \l_array_tmp_tl
3445 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3446 \array_map_inline:Nn #1 {
3447 \tl_if_eq:NNTF {##2} {\q_no_value} {}
3448 {
3449 \int_compare:nNnTF {\l_array_tmp_int} > {##1} {
3450 \int_set:Nn \l_array_tmp_int {##1}
3451 }{}
3452 }
3453 }
3454 \prop_put:Nnx #1 {base} {\int_use:N \l_array_tmp_int}
3455 }{}
3456 }
3457 }
3458 % \end{macrocode}
3459 % \end{macro}
3460 %
3461 % \begin{macro}{\array_gdel:Nn}
3462 % \begin{macrocode}
3463 \cs_new_protected:Npn \array_gdel:Nn #1#2
3464 {
3465 \exp_args:NNx \prop_gremove:Nn #1 {\int_eval:n {#2}}
3466 \int_set:Nn \l_array_tmp_int {0}
3467 \array_map_inline:Nn #1 {
3468 \tl_if_eq:NNTF {##2} {\q_no_value} {}
3469 {
3470 \int_incr:N \l_array_tmp_int
3471 }
3472 }
3473 \int_compare:nNnTF {\l_array_tmp_int} = {0}
3474 {
3475 \prop_gclear:N #1
3476 }
3477 {
3478 \prop_get:NnN #1 {top} \l_array_tmp_tl
3479 \int_compare:nNnTF {#2} = {\l_array_tmp_tl} {
3480 \prop_get:NnN #1 {base} \l_array_tmp_tl
3481 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3482 \array_map_inline:Nn #1 {
3483 \tl_if_eq:NNTF {##2} {\q_no_value} {}
3484 {
3485 \int_compare:nNnTF {\l_array_tmp_int} < {##1} {
3486 \int_set:Nn \l_array_tmp_int {##1}
3487 }{}
3488 }
3489 }
3490 \prop_gput:Nnx #1 {top} {\int_use:N \l_array_tmp_int}
3491 }{}
3492 \prop_get:NnN #1 {base} \l_array_tmp_tl
3493 \int_compare:nNnTF {#2} = {\l_array_tmp_tl} {
3494 \prop_get:NnN #1 {top} \l_array_tmp_tl
3495 \int_set:Nn \l_array_tmp_int {\l_array_tmp_tl}
3496 \array_map_inline:Nn #1 {
3497 \tl_if_eq:NNTF {##2} {\q_no_value} {}
3498 {
3499 \int_compare:nNnTF {\l_array_tmp_int} > {##1} {
3500 \int_set:Nn \l_array_tmp_int {##1}
3501 }{}
3502 }
3503 }
3504 \prop_gput:Nnx #1 {base} {\int_use:N \l_array_tmp_int}
3505 }{}
3506 }
3507 }
3508 % \end{macrocode}
3509 % \end{macro}
3510 %
3511 % \begin{macro}{\array_length:N}
3512 % \begin{macrocode}
3513 \cs_new_protected:Npn \array_length:N #1
3514 {
3515 \int_eval:n {\prop_item:Nn #1 {top} - \prop_item:Nn #1 {base}}
3516 }
3517 % \end{macrocode}
3518 % \end{macro}
3519 % \begin{macrocode}
3520 \ExplSyntaxOff
3521 % \end{macrocode}
3522 %
3523 % \iffalse
3524 %</array>
3525 % \fi
3526 %
3527 % \iffalse
3528 %<*l3hobby>
3529 % \fi
3530 %
3531 % \begin{macrocode}
3532 \RequirePackage{expl3}
3533 % \end{macrocode}
3534 %
3535 % Load the hobby core
3536 % \begin{macrocode}
3537 \input{hobby.code.tex}
3538 % \end{macrocode}
3539 %
3540 % Register as an expl3 package
3541 % \begin{macrocode}
3542 \ProvidesExplPackage {hobby-l3draw} {2018/02/20} {1.0} {Interface for l3draw and hobby}
3543 % \end{macrocode}
3544 %
3545 % Load the l3draw package
3546 % \begin{macrocode}
3547 \RequirePackage{l3draw}
3548 % \end{macrocode}
3549 %
3550 %
3551 % \begin{macro}{\hobby_draw_moveto:nnn}
3552 % This provides our interface between hobby's moveto and l3draw's moveto
3553 % \begin{macrocode}
3554 \cs_new_protected:Npn \hobby_draw_moveto:nnn #1#2#3
3555 {
3556 \draw_path_canvas_moveto:n {#3}
3557 }
3558 % \end{macrocode}
3559 % \end{macro}
3560 %
3561 % \begin{macro}{\hobby_draw_curveto:nnn}
3562 % This provides our interface between hobby's curveto and l3draw's curveto
3563 % \begin{macrocode}
3564 \cs_set_eq:NN \hobby_draw_curveto:nnn \draw_path_canvas_curveto:nnn
3565 % \end{macrocode}
3566 % \end{macro}
3567 %
3568 % \begin{macro}{\hobby_draw_close:n}
3569 % This provides our interface between hobby's close and l3draw's close
3570 % \begin{macrocode}
3571 \cs_new_protected:Npn \hobby_draw_close:n #1
3572 {
3573 \draw_path_close:
3574 }
3575 % \end{macrocode}
3576 % \end{macro}
3577 %
3578 % \begin{macro}{\hobby_draw_addpoint:n}
3579 % This processes a point and passes it one more step towards the hobby algorithm
3580 % \begin{macrocode}
3581 \cs_new_protected:Npn \hobby_draw_addpoint:n #1
3582 {
3583 \__draw_point_process:nn
3584 { \__hobby_draw_addpoint:nn }
3585 { \draw_point_transform:n {#1} }
3586 }
3587 % \end{macrocode}
3588 % \end{macro}
3589 %
3590 % \begin{macro}{\__hobby_draw_curveto:nn}
3591 % This provides our interface between l3draw's points and hobby's syntax
3592 % \begin{macrocode}
3593 \cs_new_protected:Npn \__hobby_draw_addpoint:nn #1#2
3594 {
3595 \hobby_add_point:nnnnnn {#1} {#2} {1} {1} {0} {10}
3596 }
3597 % \end{macrocode}
3598 % \end{macro}
3599 %
3600 % \begin{macro}{\hobby_draw_init:}
3601 % This initialises hobby's algorithm with the l3draw commands
3602 % \begin{macrocode}
3603 \cs_new_protected:Npn \hobby_draw_init:
3604 {
3605 \hobby_set_cmds:NNN \hobby_draw_moveto:nnn \hobby_draw_curveto:nnn \hobby_draw_close:n
3606 \hobby_clear_path:
3607 }
3608 % \end{macrocode}
3609 % \end{macro}
3610 %
3611 % \iffalse
3612 %</l3hobby>
3613 % \fi
3614 %\Finale