-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgflibrarybezieroffset.code.tex
484 lines (456 loc) · 18 KB
/
pgflibrarybezieroffset.code.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
%% tikz-nfold.sty
%% Copyright 2023 Jonathan Schulz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% This work has the LPPL maintenance status 'maintained'.
%
% The Current Maintainer of this work is Jonathan Schulz.
%
% This work consists of the files pgflibrarybezieroffset.code.tex,
% tikzlibrarynfold.code.tex, tikz-nfold-doc.tex, and tikz-nfold-doc.pdf.
% Don't delete this code in case we need it in the future
%
% Split a Bezier curve (de Casteljau's algorithm)
% #1 = time (between 0 and 1)
% #2-#5: control points
% Outputs the first part into \pgf@splitbezier@i@i, ... , \pgf@splitbezier@i@iv,
% and the second part into \pgf@splitbezier@ii@i, ... , \pgf@splitbezier@ii@iv.
%\def\pgf@splitbezier#1#2#3#4#5{%
% % based on pgfcorepoints.code.tex, \pgfpointcurveattime
% \edef\pgf@time@s{#1}%
% \pgf@x=-\pgf@time@s pt%
% \advance\pgf@x by 1pt%
% \edef\pgf@time@t{\pgf@sys@tonumber{\pgf@x}}%
% % P^0_3
% \pgfextract@process\pgf@splitbezier@ii@iv{#5}%
% \pgf@xc=\pgf@x%
% \pgf@yc=\pgf@y%
% % P^0_2
% \pgf@process{#4}%
% \pgf@xb=\pgf@x%
% \pgf@yb=\pgf@y%
% % P^0_1
% \pgf@process{#3}%
% \pgf@xa=\pgf@x%
% \pgf@ya=\pgf@y%
% % P^0_0
% \pgfextract@process\pgf@splitbezier@i@i{#2}%
% % First iteration:
% % P^1_0
% \pgf@x=\pgf@time@t\pgf@x\advance\pgf@x by\pgf@time@s\pgf@xa%
% \pgf@y=\pgf@time@t\pgf@y\advance\pgf@y by\pgf@time@s\pgf@ya%
% \pgfextract@process\pgf@splitbezier@i@ii{}%
% % P^1_1
% \pgf@xa=\pgf@time@t\pgf@xa\advance\pgf@xa by\pgf@time@s\pgf@xb%
% \pgf@ya=\pgf@time@t\pgf@ya\advance\pgf@ya by\pgf@time@s\pgf@yb%
% % P^1_2
% \pgf@xb=\pgf@time@t\pgf@xb\advance\pgf@xb by\pgf@time@s\pgf@xc%
% \pgf@yb=\pgf@time@t\pgf@yb\advance\pgf@yb by\pgf@time@s\pgf@yc%
% \edef\pgf@splitbezier@ii@iii{\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}%
% % P^2_0
% \pgf@x=\pgf@time@t\pgf@x\advance\pgf@x by\pgf@time@s\pgf@xa%
% \pgf@y=\pgf@time@t\pgf@y\advance\pgf@y by\pgf@time@s\pgf@ya%
% \pgfextract@process\pgf@splitbezier@i@iii{}%
% % P^2_1
% \pgf@xa=\pgf@time@t\pgf@xa\advance\pgf@xa by\pgf@time@s\pgf@xb%
% \pgf@ya=\pgf@time@t\pgf@ya\advance\pgf@ya by\pgf@time@s\pgf@yb%
% \edef\pgf@splitbezier@ii@ii{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}%
% % P^3_0
% \pgf@x=\pgf@time@t\pgf@x\advance\pgf@x by\pgf@time@s\pgf@xa%
% \pgf@y=\pgf@time@t\pgf@y\advance\pgf@y by\pgf@time@s\pgf@ya%
% \pgfextract@process\pgf@splitbezier@i@iv{}%
% \let\pgf@splitbezier@ii@i\pgf@splitbezier@i@iv
%}
% Split the Bezier curve defined by #1-#4 at t=0.5 using de Casteljau's algorithm
\def\pgf@halfsplitbezier#1#2#3#4{%
% based on pgfcorepoints.code.tex, \pgfpointcurveattime
% P^0_3
\pgfextract@process\pgf@splitbezier@ii@iv{#4}%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
% P^0_2
\pgf@process{#3}% removing these does not yield a significant speedup
\pgf@xb=\pgf@x%
\pgf@yb=\pgf@y%
% P^0_1
\pgf@process{#2}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
% P^0_0
\pgfextract@process\pgf@splitbezier@i@i{#1}%
% First iteration:
% P^1_0
\pgf@x=.5\pgf@x\advance\pgf@x by.5\pgf@xa%
\pgf@y=.5\pgf@y\advance\pgf@y by.5\pgf@ya%
\edef\pgf@splitbezier@i@ii{\pgf@x\the\pgf@x\pgf@y\the\pgf@y}%
% P^1_1
\pgf@xa=.5\pgf@xa\advance\pgf@xa by.5\pgf@xb%
\pgf@ya=.5\pgf@ya\advance\pgf@ya by.5\pgf@yb%
% P^1_2
\pgf@xb=.5\pgf@xb\advance\pgf@xb by.5\pgf@xc%
\pgf@yb=.5\pgf@yb\advance\pgf@yb by.5\pgf@yc%
\edef\pgf@splitbezier@ii@iii{\pgf@x\the\pgf@xb\pgf@y\the\pgf@yb}%
% P^2_0
\pgf@x=.5\pgf@x\advance\pgf@x by.5\pgf@xa%
\pgf@y=.5\pgf@y\advance\pgf@y by.5\pgf@ya%
\edef\pgf@splitbezier@i@iii{\pgf@x\the\pgf@x\pgf@y\the\pgf@y}%
% P^2_1
\pgf@xa=.5\pgf@xa\advance\pgf@xa by.5\pgf@xb%
\pgf@ya=.5\pgf@ya\advance\pgf@ya by.5\pgf@yb%
\edef\pgf@splitbezier@ii@ii{\pgf@x\the\pgf@xa\pgf@y\the\pgf@ya}%
% P^3_0
\pgf@x=.5\pgf@x\advance\pgf@x by.5\pgf@xa%
\pgf@y=.5\pgf@y\advance\pgf@y by.5\pgf@ya%
\edef\pgf@splitbezier@i@iv{\pgf@x\the\pgf@x\pgf@y\the\pgf@y}%
\let\pgf@splitbezier@ii@i\pgf@splitbezier@i@iv
}
% computes the cross product and puts it into \pgfmathresult
\def\pgfmathcrossproduct#1#2{%
\begingroup
\pgf@process{#1}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{#2}%
\pgf@y=\pgf@sys@tonumber\pgf@xa\pgf@y
\advance\pgf@y by -\pgf@sys@tonumber\pgf@ya\pgf@x
\pgfmath@returnone\pgf@y
\endgroup
}
\def\pgfmathdotproduct#1#2{%
\begingroup
\pgf@process{#1}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{#2}%
\pgf@x=\pgf@sys@tonumber\pgf@xa\pgf@x
\advance\pgf@x by \pgf@sys@tonumber\pgf@ya\pgf@y
\pgfmath@returnone\pgf@x
\endgroup
}
\def\pgfmathcrossdot#1#2{%
\begingroup
\pgf@process{#1}%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{#2}%
\pgf@xb=\pgf@sys@tonumber\pgf@xa\pgf@x
\pgf@yb=\pgf@sys@tonumber\pgf@xa\pgf@y
\advance\pgf@xb by \pgf@sys@tonumber\pgf@ya\pgf@y
\advance\pgf@yb by -\pgf@sys@tonumber\pgf@ya\pgf@x
\edef\pgf@temp{%
\edef\noexpand\pgf@tmp@dot{\pgf@sys@tonumber\pgf@xb}%
\edef\noexpand\pgf@tmp@cross{\pgf@sys@tonumber\pgf@yb}%
}%
\expandafter
\endgroup\pgf@temp
}
% Calculates abs(\pgf@x) + abs(\pgf@y) in #1
\def\pgfpointtaxicabnorm#1{%
\ifdim\pgf@x<0pt
#1=-\pgf@x
\else
#1=\pgf@x
\fi
\ifdim\pgf@y<0pt
\advance#1 by -\pgf@y
\else
\advance#1 by \pgf@y
\fi
}
% Computes the normalised tangents of a given Bezier curve and stores them in \pgf@tmp@tang@i and \pgf@tmp@tang@ii.
% Also computes the angles and stores them in \pgf@tmp@angle@i and \pgf@tmp@angle@ii.
% All degenerate cases are covered. For a triple degenerate curve (all points equal), the vector (1,0) is returned.
\def\pgf@offset@compute@tangents#1#2#3#4{%
\pgf@process{\pgfpointdiff{#1}{#2}}% unintuitively, this is PTii - PTi
\pgfpointtaxicabnorm\pgf@xa
\ifdim\pgf@xa<0.1pt\relax
% edge case: first point and first control point are equal
\pgf@process{\pgfpointdiff{#1}{#3}}%
\pgfpointtaxicabnorm\pgf@xa
\ifdim\pgf@xa<0.1pt\relax
% edge case: first three points are equal
\pgf@process{\pgfpointdiff{#1}{#4}}%
\fi
\fi
\pgfextract@process\pgf@tmp@tang@i{%
\pgfpointnormalised{}%
% \pgfpointnormalised stores the angle of the vector in \pgf@tmp
\global\let\pgf@nfold@tmp\pgf@tmp%
}%
\let\pgf@tmp@angle@i\pgf@nfold@tmp%
\pgf@process{\pgfpointdiff{#3}{#4}}%
\pgfpointtaxicabnorm\pgf@xa
\ifdim\pgf@xa<0.1pt\relax
\pgf@process{\pgfpointdiff{#2}{#4}}%
\pgfpointtaxicabnorm\pgf@xa
\ifdim\pgf@xa<0.1pt\relax
\pgf@process{\pgfpointdiff{#1}{#4}}%
\fi
\fi
\pgfextract@process\pgf@tmp@tang@ii{\pgfpointnormalised{}\global\let\pgf@nfold@tmp\pgf@tmp}%
\let\pgf@tmp@angle@ii\pgf@nfold@tmp%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Offsetting a simple section %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\pgf@offset@bezier@segment#1#2#3#4#5{%
% normalise tangents and normals; this avoids overflow issues later, and we need
% the normal vector to be of length 1 anyway
\pgf@offset@compute@tangents{#1}{#2}{#3}{#4}%
\pgf@offset@bezier@segment@{#1}{#2}{#3}{#4}{#5}%
}
% this version assumes that the tangents have been precomputed in \pgf@tmp@tang@i and @ii
\def\pgf@offset@bezier@segment@#1#2#3#4#5{%
% offset A1
% compute the normal
\pgf@tmp@tang@i
\edef\pgf@tmp@normal@i{\noexpand\pgfqpoint{-\the\pgf@y}{\the\pgf@x}}%
\pgfextract@process\pgf@bezier@offset@i
{\pgfpointadd{\pgfpointscale{#5}{\pgf@tmp@normal@i}}{#1}}%
% offset A4
\pgf@tmp@tang@ii
\edef\pgf@tmp@normal@ii{\noexpand\pgfqpoint{-\the\pgf@y}{\the\pgf@x}}%
\pgfextract@process\pgf@bezier@offset@iv
{\pgfpointadd{\pgfpointscale{#5}{\pgf@tmp@normal@ii}}{#4}}%
% now compute A'_2 and A'_3
\pgf@process{\pgfpointdiff{#1}{#4}}%
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
\let\pgf@tmp@secantlen\pgfmathresult
\ifdim\pgf@tmp@secantlen pt<0.1pt\relax
% Edge case: Either the curve is degenerate to a point or it is not simple.
% Either way we offset A1 and A4, and preserve the vectors A1A2 and A3A4.
\pgfutil@packagewarning{tikz-nfold}{first and last point are too close, expect glitches}%
\pgfextract@process\pgf@bezier@offset@ii
{\pgfpointadd{\pgf@bezier@offset@i}{\pgfpointdiff{#1}{#2}}}%
\pgfextract@process\pgf@bezier@offset@iii
{\pgfpointadd{\pgf@bezier@offset@iv}{\pgfpointdiff{#4}{#3}}}%
\else
\pgfextract@process\pgf@tmp@secant{\pgfpointnormalised{}}%
\pgfmathcrossdot{}{\pgf@tmp@tang@ii}%
\ifdim\pgf@tmp@dot pt<.5pt\relax%
% this can only happen in non-simple curves
\pgfutil@packagewarning{tikz-nfold}{cosine of \pgf@tmp@dot\space clamped to 0.5 in non-simple segment}%
\def\pgf@tmp@dot{.5}%
\fi%
\pgfmathdivide@{\pgf@tmp@cross}{\pgf@tmp@dot}%
\let\pgf@tmp@tanbeta\pgfmathresult
\pgfmathcrossdot{\pgf@tmp@secant}{\pgfpointnormalised{\pgfpointdiff{#1}{#2}}}
% There are cases where we want #5/secantlen to be quite large, so we should not clamp the value here
% \pgfmathparse{1 + #5/\pgf@tmp@secantlen*(\pgf@tmp@cross - \pgf@tmp@dot*\pgf@tmp@tanbeta)}%
\pgfmath@offset@calculate@scale{\pgf@tmp@secantlen}{\pgf@tmp@cross}{\pgf@tmp@dot}{\pgf@tmp@tanbeta}{#5}%
\pgfextract@process\pgf@bezier@offset@ii{%
\pgfpointadd
{\pgf@bezier@offset@i}
{\pgfqpointscale{\pgfmathresult}{\pgfpointdiff{#1}{#2}}}%
}%
% third control point
\pgfmathcrossdot{\pgf@tmp@secant}{\pgf@tmp@tang@i}%
\ifdim\pgf@tmp@dot pt<.5pt\relax
\pgfutil@packagewarning{tikz-nfold}{cosine of \pgf@tmp@dot\space clamped to 0.5 in non-simple segment}%
\def\pgf@tmp@dot{.5}%
\fi
\pgfmathdivide@{\pgf@tmp@cross}{\pgf@tmp@dot}%
\let\pgf@tmp@tanbeta\pgfmathresult
\pgfmathcrossdot{\pgf@tmp@secant}{\pgfpointnormalised{\pgfpointdiff{#4}{#3}}}%
% \pgfmathparse{1 + #5/\pgf@tmp@secantlen*(\pgf@tmp@cross - \pgf@tmp@dot*\pgf@tmp@tanbeta)}%
\pgfmath@offset@calculate@scale{\pgf@tmp@secantlen}{\pgf@tmp@cross}{\pgf@tmp@dot}{\pgf@tmp@tanbeta}{#5}%
\pgfextract@process\pgf@bezier@offset@iii{%
\pgfpointadd
{\pgf@bezier@offset@iv}
{\pgfqpointscale{\pgfmathresult}{\pgfpointdiff{#4}{#3}}}%
}%
\fi
}
% calculates 1+#5/#1*(#2-#3*#4)
% #1 = secantlen
% #2 = cross
% #3 = dot
% #4 = tanbeta
% #5 = #5 (offset)
\def\pgfmath@offset@calculate@scale#1#2#3#4#5{%
\begingroup
\pgfmathmultiply@{#3}{#4}%
\pgfmathsubtract@{#2}{\pgfmathresult}%
\let\pgfmath@temp\pgfmathresult
\pgfmathreciprocal@{#1}%
\pgfmathmultiply@{\pgfmathresult}{\pgfmath@temp}%
\pgfmathmultiply{\pgfmathresult}{#5}%
\pgfmathadd@{\pgfmathresult}{1}%
\pgfmath@smuggleone\pgfmathresult
\endgroup
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subdividing and offsetting %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Maximum level of recursion. The theoretical limit to the number of subdivisions in the final curve
% is given by 2^\pgf@offset@max@recursion
\newcount\pgf@offset@max@recursion
\pgf@offset@max@recursion=4
%
% Subdivides a Bezier curve into "simple" segments (according to the definition below),
% offsets the segments, and draws them. Because offsetting also involves relocating
% the starting points, these macros come in two variants: with and without a \pgfpathmoveto{}
% to the new starting point.
%
% Interface:
% #1-#4: control points of the whole Bezier curve
% #5: offset
\def\pgfoffsetcurve#1#2#3#4#5{%
\pgfoffsetcurvecallback{#1}{#2}{#3}{#4}{#5}{\pgf@nfold@callback@move}%
}
\def\pgfoffsetcurvenomove#1#2#3#4#5{%
\pgfoffsetcurvecallback{#1}{#2}{#3}{#4}{#5}{\pgf@nfold@callback@nomove}%
}
% Arguments:
% #1-#4: control points of the segment
% #5: =0 if this is the first segment of the curve, =1 otherwise
% (checking for #5=0 allows us to draw the curve without interruptions)
\def\pgf@nfold@callback@move#1#2#3#4#5{%
\if#50\pgfpathmoveto{#1}\fi%
\pgfpathcurveto{#2}{#3}{#4}%
}
% this version never does a moveto at the start. Useful for drawing a path consisting of
% multiple Bezier curves.
\def\pgf@nfold@callback@nomove#1#2#3#4#5{\pgfpathcurveto{#2}{#3}{#4}}
% This macro subdivides and offsets a curve and executes a given callback on every offset simple segment.
% #1-#4: control points of the segment
% #5: =0 if this is the first segment of the curve, =1 otherwise
% #6: callback (see \pgf@nfold@callback@move) for the parameters
\def\pgfoffsetcurvecallback#1#2#3#4#5#6{%
\edef\pgf@offset@tmp@callback##1##2##3##4##5{%
\noexpand\pgf@offset@bezier@segment{##1}{##2}{##3}{##4}{#5}%
\noexpand#6{\noexpand\pgf@bezier@offset@i}{\noexpand\pgf@bezier@offset@ii}{\noexpand\pgf@bezier@offset@iii}{\noexpand\pgf@bezier@offset@iv}{##5}%
}
\pgf@subdividecurve{#1}{#2}{#3}{#4}{\pgf@offset@max@recursion}{0}{\pgf@offset@tmp@callback}%
}
% This macro subdivides (but does not offset) a curve and executes a callback on every simple segment.
% #1-#4: control points
% #5: recursion limit (decreases on every recursive call)
% #6: =0 if this is the start of the curve, =1 otherwise
% #7: callback for output (see above)
\newif\ifpgf@offset@subdivide
\def\pgf@subdividecurve#1#2#3#4#5#6#7{%
\begingroup%
\pgfextract@process\pgf@ctrl@i{#1}%
\pgfextract@process\pgf@ctrl@ii{#2}%
\pgfextract@process\pgf@ctrl@iii{#3}%
\pgfextract@process\pgf@ctrl@iv{#4}%
\pgf@offset@compute@tangents{\pgf@ctrl@i}{\pgf@ctrl@ii}{\pgf@ctrl@iii}{\pgf@ctrl@iv}%
\pgf@subdividecurve@{#5}{#6}{#7}%
\endgroup%
}
% This macro assumes that the curve is defined in \pgf@ctrl@i-\pgf@ctrl@iv
% and that its tangents have already been computed.
\def\pgf@subdividecurve@#1#2#3{%
\pgf@offset@subdividefalse%
\c@pgf@counta=#1\relax
\advance\c@pgf@counta by -1
% Use the non-degenerate tangents for the simplicity check
\pgfextract@process\pgf@itoiv{\pgfpointdiff{\pgf@ctrl@i}{\pgf@ctrl@iv}}%
\pgfmathcrossproduct{\pgf@itoiv}{\pgf@tmp@tang@i}%
\let\firstcross\pgfmathresult
\pgfmathcrossproduct{\pgf@itoiv}{\pgf@tmp@tang@ii}%
% First simplicity check: Are A2 and A3 on the same side of the A1-A4 line?
% -> compute the sign of the cross products, use the sign function to avoid overflows
% just give it a pass if one of them is zero, hence 2 and 3 at the end
\ifnum
\ifdim \firstcross pt<0pt -1\else\ifdim \firstcross pt>0pt 1\else 2\fi\fi
=\ifdim\pgfmathresult pt<0pt -1\else\ifdim\pgfmathresult pt>0pt 1\else 3\fi\fi
\relax % the \relax is important!
\pgf@offset@subdividetrue%
\else%
% Second simplicity check: How large is the angle between the tangents in A1 and A4?
\pgfmathdotproduct{\pgf@tmp@tang@i}{\pgf@tmp@tang@ii}%
\ifdim\pgfmathresult pt<.5pt\relax%
\pgf@offset@subdividetrue%
\else
% Third simplicity check: Put a limit on the lengths of the i-ii and iii-iv vectors combined
\pgf@itoiv
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
\pgf@xa=\pgfmathresult pt
\pgf@process{\pgfpointdiff{\pgf@ctrl@i}{\pgf@ctrl@ii}}%
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
\pgf@xb=\pgfmathresult pt
\pgf@process{\pgfpointdiff{\pgf@ctrl@iii}{\pgf@ctrl@iv}}%
\pgfmathveclen@{\pgf@sys@tonumber\pgf@x}{\pgf@sys@tonumber\pgf@y}%
\advance\pgf@xb by \pgfmathresult pt
% veclen(iv-i) < veclen(ii-i) + veclen(iv-iii)
\ifdim\pgf@xa<\pgf@xb
\pgf@offset@subdividetrue
\fi
\fi%
\fi%
\ifpgf@offset@subdivide%
\ifnum\c@pgf@counta<0%
% We hit the recursion limit but the segment is not simple
\pgfutil@packagewarning{tikz-nfold}{Recursion limit reached, glitches may occur. %
Consider increasing \string\pgf@offset@max@recursion}%
% Try to offset the curve anyway. The result will not be precise,
% but the code is sufficiently robust to not crash
#3{\pgf@ctrl@i}{\pgf@ctrl@ii}{\pgf@ctrl@iii}{\pgf@ctrl@iv}{#2}%
\else
% split the non-simple segment and execute recursive calls
\pgf@halfsplitbezier{\pgf@ctrl@i}{\pgf@ctrl@ii}{\pgf@ctrl@iii}{\pgf@ctrl@iv}%
% compute the tangent at the spitting point; this vector is only zero if the curve
% is degenerate to a point, in which case we can't compute the tangent anyway
\pgfextract@process\pgf@middletangent{%
\pgfpointnormalised{\pgfpointdiff{\pgf@splitbezier@i@iii}{\pgf@splitbezier@i@iv}}}%
\begingroup%
% we need a group to avoid overwriting variables in recursive calls
\let\pgf@tmp@tang@ii\pgf@middletangent%
\let\pgf@ctrl@i\pgf@splitbezier@i@i%
\let\pgf@ctrl@ii\pgf@splitbezier@i@ii%
\let\pgf@ctrl@iii\pgf@splitbezier@i@iii%
\let\pgf@ctrl@iv\pgf@splitbezier@i@iv%
% pass on the "start of the curve flag" only to the first term
\pgf@subdividecurve@{\c@pgf@counta}{#2}{#3}%
\endgroup%
\begingroup%
\let\pgf@tmp@tang@i\pgf@middletangent%
\let\pgf@ctrl@i\pgf@splitbezier@ii@i%
\let\pgf@ctrl@ii\pgf@splitbezier@ii@ii%
\let\pgf@ctrl@iii\pgf@splitbezier@ii@iii%
\let\pgf@ctrl@iv\pgf@splitbezier@ii@iv%
\pgf@subdividecurve@{\c@pgf@counta}{1}{#3}%
\endgroup%
\fi%
\else%
% curve is simple
#3{\pgf@ctrl@i}{\pgf@ctrl@ii}{\pgf@ctrl@iii}{\pgf@ctrl@iv}{#2}%
\fi%
}
%
% Offsetting straight lines
% -------------------------
%
% For convenience we also provide macros that offset straight lines. These also come in two variants
% similar to the macros for curves.
%
\def\pgfoffsetline#1#2#3{%
\pgfmathparse{#3}%
\pgfoffsetline@{#1}{#2}{\pgfmathresult}{\pgfpointnormalised{\pgfpointdiff{#1}{#2}}}%
}
% a quicker version in case we already know the tangent and #3 is a number without unit
\def\pgfoffsetline@#1#2#3#4{%
\pgfqpointscale{#3}{#4}%
\pgf@xc=-\pgf@y
\pgf@yc=\pgf@x
\pgfpathmoveto{\pgfpointadd{#1}{\pgfqpoint{\pgf@xc}{\pgf@yc}}}%
\pgfpathlineto{\pgfpointadd{#2}{\pgfqpoint{\pgf@xc}{\pgf@yc}}}%
}
\def\pgfoffsetlinenomove#1#2#3{%
\pgfoffsetlinenomove@{#1}{#2}{#3}{\pgfpointnormalised{\pgfpointdiff{#1}{#2}}}%
}
\def\pgfoffsetlinenomove@#1#2#3#4{%
\pgfqpointscale{#3}{#4}%
\pgf@xc=-\pgf@y
\pgf@yc=\pgf@x
\pgfpathlineto{\pgfpointadd{#2}{\pgfqpoint{\pgf@xc}{\pgf@yc}}}%
}