Debian Codegraph @

texmf-dist/tex/generic/pgf/libraries/pgflibraryintersections.code.tex

raw
1 % Copyright 2019 by Mark Wibrow
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Free Documentation License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10 % Note: at the time of this writing, the library has quadratic runtime.
11 % Experimentally, it performed well while computing ~12 intersections of two
12 % plots, each with 600 samples. It failed when the number of samples exceeded 700.
13
14 \usepgflibrary{fpu}%
15
16 \newcount\pgf@intersect@solutions
17
18 \newif\ifpgf@intersect@sort
19 \newif\ifpgf@intersect@sort@by@second@path
20
21 \def\pgfintersectionsortbyfirstpath{%
22 \pgf@intersect@sorttrue%
23 \pgf@intersect@sort@by@second@pathfalse%
24 }%
25
26 \def\pgfintersectionsortbysecondpath{%
27 \pgf@intersect@sorttrue%
28 \pgf@intersect@sort@by@second@pathtrue%
29 }%
30
31 % #1: the index. It starts at 1 and ends with \pgfintersectionsolutions (inclusive).
32 % Invalid values will implicitly result in the origin.
33 \def\pgfpointintersectionsolution#1{%
34 \ifnum#1<1\relax%
35 \pgfpoint@intersect@solution@orgin%
36 \else%
37 \ifnum#1>\pgfintersectionsolutions\relax%
38 \pgfpoint@intersect@solution@orgin%
39 \else%
40 \csname pgfpoint@intersect@solution@#1\endcsname%
41 \fi%
42 \fi%
43 }%
44
45 % Gets the segment indices of solution #1.
46 %
47 % #1: the solution index (i.e. the same argument as in \pgfpointintersectionsolution)
48 % #2: [output] a macro name which will contain the segment index of the first path which contains the solution
49 % #3: [output] a macro name which will contain the segment index of the second path which contains the solution
50 %
51 % Example: \pgfintersectiongetsolutionsegmentindices{0}{\first}{\second}%
52 %
53 % -> \first may be 0 if point #0 is in the 0'th segment
54 % -> \second may be 42 if point #0 is in the 42'th segment
55 %
56 % The "segment index" is actually close to the "time" of the solution.
57 % If a solution is at "time" 42.2, it will have segment index 42.
58 \def\pgfintersectiongetsolutionsegmentindices#1#2#3{%
59 \ifnum#1<1\relax%
60 \let#2=\pgfutil@empty
61 \let#3=\pgfutil@empty
62 \else%
63 \ifnum#1>\pgfintersectionsolutions\relax%
64 \let#2=\pgfutil@empty
65 \let#3=\pgfutil@empty
66 \else%
67 \def\pgf@temp##1##2##3##4{%
68 \edef#2{##1}%
69 \edef#3{##2}%
70 }%
71 \expandafter\let\expandafter\pgf@tempb\csname pgf@intersect@solution@props@#1\endcsname
72 \expandafter\pgf@temp\pgf@tempb
73 \fi%
74 \fi%
75 }%
76
77 % Gets the time indices of solution #1.
78 %
79 % #1: the solution index (i.e. the same argument as in \pgfpointintersectionsolution)
80 % #2: [output] a macro name which will contain the time of the first path which contains the solution
81 % It will never be empty.
82 % #3: [output] a macro name which will contain the time of the second path which contains the solution
83 % It will never be empty.
84 %
85 % Example: \pgfintersectiongetsolutiontimes{0}{\first}{\second}%
86 %
87 % -> \first may be 0.5 if point #0 is in just in the middle of the path
88 % -> \second may be 42.8 if point #0 is in the 42'th segment (compare
89 % \pgfintersectiongetsolutionsegmentindices) and is at 80% of the
90 % 42'th segment
91 %
92 % Note that the precise time inside of a segment may be unavailable
93 % (currently, it is only computed for curveto paths and not
94 % necessarily for lineto). If the precise time is unavailable, this
95 % call will return the value of
96 % \pgfintersectiongetsolutionsegmentindices (which is a
97 % "coarse-grained" time).
98 \def\pgfintersectiongetsolutiontimes#1#2#3{%
99 \ifnum#1<1\relax%
100 \let#2=\pgfutil@empty
101 \let#3=\pgfutil@empty
102 \else%
103 \ifnum#1>\pgfintersectionsolutions\relax%
104 \let#2=\pgfutil@empty
105 \let#3=\pgfutil@empty
106 \else%
107 \def\pgf@temp##1##2##3##4{%
108 \edef#2{##3}%
109 \edef#3{##4}%
110 %
111 % check for fallback to segment indices:
112 \ifx#2\pgfutil@empty \edef#2{##1}\fi
113 \ifx#3\pgfutil@empty \edef#3{##2}\fi
114 }%
115 \expandafter\let\expandafter\pgf@tempb\csname pgf@intersect@solution@props@#1\endcsname
116 \expandafter\pgf@temp\pgf@tempb
117 \fi%
118 \fi%
119 }%
120
121 \def\pgfpoint@intersect@solution@orgin{%
122 \begingroup%
123 \pgftransforminvert%
124 \pgfpointorigin%
125 \pgf@pos@transform@glob
126 \global\pgf@x=\pgf@x%
127 \global\pgf@y=\pgf@y%
128 \endgroup%
129 }%
130
131 % #1 code which assigns the first path using \pgfsetpath.
132 % #2 code which assigns the second path using \pgfsetpath.
133 %
134 % On output, the points, their properties, and the number of points are set.
135 % Use \pgfintersectionsolutions which expands to the number of intersections
136 \long\def\pgfintersectionofpaths#1#2{%
137 \begingroup%
138 \pgfinterruptpath%
139 #1%
140 \pgfgetpath\pgf@intersect@path@a%
141 \global\let\pgf@intersect@path@temp=\pgf@intersect@path@a%
142 \endpgfinterruptpath%
143 \endgroup%
144 \let\pgf@intersect@path@a=\pgf@intersect@path@temp%
145 %
146 \begingroup%
147 \pgfinterruptpath%
148 #2%
149 \pgfgetpath\pgf@intersect@path@b%
150 \global\let\pgf@intersect@path@temp=\pgf@intersect@path@b%
151 \endpgfinterruptpath%
152 \endgroup%
153 \let\pgf@intersect@path@b=\pgf@intersect@path@temp%
154 %
155 \pgf@intersect@solutions=0\relax%
156 \pgf@intersect@path@reset@a
157 %
158 \ifpgf@intersect@sort@by@second@path%
159 \let\pgf@intersect@temp=\pgf@intersect@path@a%
160 \let\pgf@intersect@path@a=\pgf@intersect@path@b%
161 \let\pgf@intersect@path@b=\pgf@intersect@temp%
162 \fi%
163 %
164 \pgfprocessround\pgf@intersect@path@a\pgf@intersect@path@a%
165 \pgfprocessround\pgf@intersect@path@b\pgf@intersect@path@b%
166 %
167 \let\pgf@intersect@token@after=\pgf@intersect@path@process@a%
168 \expandafter\pgf@intersectionofpaths\pgf@intersect@path@a\pgf@stop%
169 \edef\pgfintersectionsolutions{\the\pgf@intersect@solutions}%
170 \pgfmathloop%
171 \ifnum\pgfmathcounter>\pgfintersectionsolutions\relax%
172 \else%
173 \pgfutil@namelet{pgfpoint@intersect@solution@\pgfmathcounter}%
174 {pgfpoint@g@intersect@solution@\pgfmathcounter}%
175 \edef\pgf@marshal{\noexpand\pgf@intersection@set@properties{\csname pgfpoint@g@intersect@solution@\pgfmathcounter @props\endcsname}}%
176 \pgf@marshal
177 \ifpgf@intersect@sort%
178 \pgfutil@namelet{pgf@intersect@solution@\pgfmathcounter @time@a}%
179 {pgf@g@intersect@solution@\pgfmathcounter @time@a}%
180 \fi%
181 \repeatpgfmathloop%
182 \ifpgf@intersect@sort%
183 \pgfintersectionsolutionsortbytime%
184 \fi%
185 }%
186
187 \def\pgf@intersection@set@properties#1{%
188 \pgfutil@namedef{pgf@intersect@solution@props@\pgfmathcounter}{#1}%
189 }%
190
191 % #1 a global name prefix to store properties.
192 \def\pgf@intersection@store@properties#1{%
193 % we store the time offsets as well and make them available programmatically:
194 % note that \pgf@intersect@time@a and \pgf@intersect@time@b may be empty.
195 %
196 % However, \pgf@intersect@time@offset and
197 % \pgf@intersect@time@offset@b are *always* valid. In fact,they
198 % resemble a part of the time: it holds
199 % 0 <= \pgf@intersect@time@a < 1
200 % and \pgf@intersect@time@offset > 0.
201 %
202 % If we have an intersection in segment 42 of path A,
203 % \pgf@intersect@time@offset will be 42. The time inside of that
204 % segment is given as number in the interval [0,1]. If it is 0.3,
205 % the total time will be 42.3 and that number will be stored as
206 % \pgf@intersect@time@a.
207 %
208 \expandafter\xdef\csname #1@props\endcsname{{\pgf@intersect@time@offset}{\pgf@intersect@time@offset@b}{\pgf@intersect@time@a}{\pgf@intersect@time@b}}%
209 }%
210
211 \def\pgf@intersectionofpaths#1{%
212 \ifx#1\pgf@stop%
213 \let\pgf@intersect@next=\relax%
214 \else%
215 \ifx#1\pgfsyssoftpath@movetotoken%
216 \let\pgf@intersect@next=\pgf@intersect@token@moveto%
217 \else%
218 \ifx#1\pgfsyssoftpath@linetotoken%
219 \let\pgf@intersect@next=\pgf@intersect@token@lineto%
220 \else%
221 \ifx#1\pgfsyssoftpath@closepathtoken%
222 \let\pgf@intersect@next=\pgf@intersect@token@lineto%
223 \else%
224 \ifx#1\pgfsyssoftpath@curvetosupportatoken%
225 \let\pgf@intersect@next=\pgf@intersect@token@curveto%
226 \else%
227 \ifx#1\pgfsyssoftpath@rectcornertoken%
228 \let\pgf@intersect@next=\pgf@intersect@token@rect%
229 \fi%
230 \fi%
231 \fi%
232 \fi%
233 \fi%
234 \fi%
235 \pgf@intersect@next}%
236
237 \def\pgf@intersect@token@moveto#1#2{%
238 \def\pgfpoint@intersect@start{\pgfqpoint{#1}{#2}}%
239 \pgf@intersectionofpaths%
240 }%
241
242 \def\pgf@intersect@token@lineto#1#2{%
243 \def\pgfpoint@intersect@end{\pgfqpoint{#1}{#2}}%
244 \def\pgf@intersect@type{line}%
245 \pgf@intersect@token@after%
246 }%
247 \def\pgf@intersect@token@curveto#1#2\pgfsyssoftpath@curvetosupportbtoken#3#4\pgfsyssoftpath@curvetotoken#5#6{%
248 \def\pgfpoint@intersect@firstsupport{\pgfqpoint{#1}{#2}}%
249 \def\pgfpoint@intersect@secondsupport{\pgfqpoint{#3}{#4}}%
250 \def\pgfpoint@intersect@end{\pgfqpoint{#5}{#6}}%
251 \def\pgf@intersect@type{curve}%
252 \pgf@intersect@token@after%
253 }%
254
255 \def\pgf@intersect@token@rect#1#2\pgfsyssoftpath@rectsizetoken#3#4{%
256 \pgf@xa=#1\relax%
257 \advance\pgf@xa by#3\relax%
258 \pgf@ya=#2\relax%
259 \advance\pgf@ya by#4\relax%
260 \edef\pgf@marshal{%
261 \noexpand\pgfsyssoftpath@movetotoken{#1}{#2}%
262 \noexpand\pgfsyssoftpath@linetotoken{#1}{\the\pgf@ya}%
263 \noexpand\pgfsyssoftpath@linetotoken{\the\pgf@xa}{\the\pgf@ya}%
264 \noexpand\pgfsyssoftpath@linetotoken{\the\pgf@xa}{#2}%
265 \noexpand\pgfsyssoftpath@closepathtoken{#1}{#2}%
266 }%
267 \expandafter\pgf@intersectionofpaths\pgf@marshal%
268 }%
269
270 \def\pgf@intersect@path@process@a{%
271 \pgf@intersect@path@getpoints@a%
272 \let\pgf@intersect@token@after=\pgf@intersect@path@process@b%
273 \pgf@intersect@path@reset@b
274 \expandafter\pgf@intersectionofpaths\pgf@intersect@path@b\pgf@stop%
275 \let\pgfpoint@intersect@start=\pgfpoint@intersect@end@a%
276 \let\pgf@intersect@token@after=\pgf@intersect@path@process@a%
277 \c@pgf@counta=\pgf@intersect@time@offset\relax%
278 \advance\c@pgf@counta by1\relax%
279 \edef\pgf@intersect@time@offset{\the\c@pgf@counta}%
280 \pgf@intersectionofpaths%
281 }%
282
283 \def\pgf@intersect@path@reset@a{%
284 \def\pgf@intersect@time@offset{0}%
285 \def\pgf@intersect@time@a{}%
286 }%
287
288 \def\pgf@intersect@path@reset@b{%
289 \def\pgf@intersect@time@offset@b{0}%
290 \def\pgf@intersect@time@b{}%
291 }%
292
293 \def\pgf@intersect@path@getpoints@a{%
294 \let\pgfpoint@intersect@start@a=\pgfpoint@intersect@start%
295 \let\pgfpoint@intersect@end@a=\pgfpoint@intersect@end%
296 \let\pgfpoint@intersect@firstsupport@a=\pgfpoint@intersect@firstsupport%
297 \let\pgfpoint@intersect@secondsupport@a=\pgfpoint@intersect@secondsupport%
298 \let\pgf@intersect@type@a=\pgf@intersect@type%
299 }%
300
301 \def\pgf@intersect@path@process@b{%
302 \pgf@intersect@path@getpoints@b%
303 \csname pgf@intersect@\pgf@intersect@type@a @and@\pgf@intersect@type@b\endcsname%
304 \let\pgfpoint@intersect@start=\pgfpoint@intersect@end@b%
305 \c@pgf@counta=\pgf@intersect@time@offset@b\relax%
306 \advance\c@pgf@counta by1\relax%
307 \edef\pgf@intersect@time@offset@b{\the\c@pgf@counta}%
308 \pgf@intersectionofpaths}%
309
310 \def\pgf@intersect@path@getpoints@b{%
311 \let\pgfpoint@intersect@start@b=\pgfpoint@intersect@start%
312 \let\pgfpoint@intersect@end@b=\pgfpoint@intersect@end%
313 \let\pgfpoint@intersect@firstsupport@b=\pgfpoint@intersect@firstsupport%
314 \let\pgfpoint@intersect@secondsupport@b=\pgfpoint@intersect@secondsupport%
315 \let\pgf@intersect@type@b=\pgf@intersect@type%
316 }%
317
318 \def\pgf@intersect@line@and@line{%
319 \pgf@intersectionoflines{\pgfpoint@intersect@start@a}{\pgfpoint@intersect@end@a}%
320 {\pgfpoint@intersect@start@b}{\pgfpoint@intersect@end@b}%
321 }%
322
323 \def\pgf@intersect@line@and@curve{%
324 \pgf@intersectionoflineandcurve%
325 {\pgf@process{\pgfpoint@intersect@start@a}}{\pgf@process{\pgfpoint@intersect@end@a}}%
326 {\pgf@process{\pgfpoint@intersect@start@b}}{\pgf@process{\pgfpoint@intersect@firstsupport@b}}%
327 {\pgf@process{\pgfpoint@intersect@secondsupport@b}}{\pgf@process{\pgfpoint@intersect@end@b}}%
328 }%
329
330 \def\pgf@intersect@curve@and@line{%
331 \pgf@intersectionofcurveandline%
332 {\pgf@process{\pgfpoint@intersect@start@a}}{\pgf@process{\pgfpoint@intersect@firstsupport@a}}%
333 {\pgf@process{\pgfpoint@intersect@secondsupport@a}}{\pgf@process{\pgfpoint@intersect@end@a}}%
334 {\pgf@process{\pgfpoint@intersect@start@b}}{\pgf@process{\pgfpoint@intersect@end@b}}%
335 }%
336
337 \def\pgf@intersect@curve@and@curve{%
338 \pgf@intersectionofcurves%
339 {\pgf@process{\pgfpoint@intersect@start@a}}{\pgf@process{\pgfpoint@intersect@firstsupport@a}}%
340 {\pgf@process{\pgfpoint@intersect@secondsupport@a}}{\pgf@process{\pgfpoint@intersect@end@a}}%
341 {\pgf@process{\pgfpoint@intersect@start@b}}{\pgf@process{\pgfpoint@intersect@firstsupport@b}}%
342 {\pgf@process{\pgfpoint@intersect@secondsupport@b}}{\pgf@process{\pgfpoint@intersect@end@b}}%
343 }%
344
345
346 \def\pgfintersectionoflines#1#2#3#4{%
347 \pgf@intersect@solutions=0\relax%
348 \pgf@intersectionoflines{#1}{#2}{#3}{#4}%
349 }%
350
351 \def\pgf@intersectionoflines#1#2#3#4{%
352 \pgf@iflinesintersect{#1}{#2}{#3}{#4}%
353 {%
354 \pgfextract@process\pgf@intersect@solution@candidate{%
355 % pgf@x and pgf@y are already assigned by \pgf@iflinesintersect
356 }%
357 \pgf@ifsolution@duplicate{\pgf@intersect@solution@candidate}{%
358 % ah - we a duplicate. Apparently, we have a hit on an
359 % endpoint.
360 }{%
361 \global\advance\pgf@intersect@solutions by1\relax%
362 \expandafter\global\expandafter\let\csname pgfpoint@g@intersect@solution@\the\pgf@intersect@solutions\endcsname=\pgf@intersect@solution@candidate
363 \ifpgf@intersect@sort%
364 % save coordinates of the intersection
365 \pgf@process{\pgf@intersect@solution@candidate}%
366 \pgf@xc=\pgf@x%
367 \pgf@yc=\pgf@y%
368 % determine length of path
369 \pgf@process{\pgfpointdiff{\pgfpoint@intersect@start@a}{\pgfpoint@intersect@end@a}}%
370 \edef\pgf@marshal{%
371 \noexpand\pgfmathveclen@{\pgfmath@tonumber{\pgf@x}}{\pgfmath@tonumber{\pgf@y}}%
372 }%
373 \pgf@marshal%
374 \let\pgf@intersect@length@a=\pgfmathresult%
375 % determine distance of intersection from start
376 \pgf@process{\pgfpointdiff{\pgfpoint@intersect@start@a}{\pgfqpoint{\pgf@xc}{\pgf@yc}}}%
377 \edef\pgf@marshal{%
378 \noexpand\pgfmathveclen@{\pgfmath@tonumber{\pgf@x}}{\pgfmath@tonumber{\pgf@y}}%
379 }%
380 \pgf@marshal%
381 % scale the distance to the path length (path time)
382 \pgfmathdivide@{\pgfmathresult}{\pgf@intersect@length@a}%
383 \pgf@x=\pgfmathresult pt\relax%
384 % advance by the path segment (see definition of \pgf@intersection@store@properties)
385 \advance\pgf@x by\pgf@intersect@time@offset pt\relax%
386 \edef\pgf@intersect@time@a{\pgfmath@tonumber{\pgf@x}}%
387 % save numbered
388 \expandafter\global\expandafter\let\csname pgf@g@intersect@solution@\the\pgf@intersect@solutions @time@a\endcsname=
389 \pgf@intersect@time@a
390 \else
391 \let\pgf@intersect@time@a=\pgfutil@empty
392 \fi%
393 \let\pgf@intersect@time@b=\pgfutil@empty
394 \pgf@intersection@store@properties{pgfpoint@g@intersect@solution@\the\pgf@intersect@solutions}%
395 }%
396 %
397 }{%
398 }%
399 }%
400
401 % Test if two lines L1 and L2 intersect.
402 %
403 % #1 - first point P1 on L1.
404 % #2 - second point P2 on L1.
405 % #3 - first point P3 on L2.
406 % #2 - second point P4 on L2.
407 % #5 - code executed if intersection occurs.
408 % #6 - code executed if intersection does no occur.
409 %
410 % Let L1 be represented by P1+(P2-P1)s where 0<=s<=1
411 % Let L2 be represented by P3+(P4-P3)t where 0<=t<=1
412 %
413 % Then L1 and L2 intersect at
414 %
415 % s = |x2-x1 x3-x1| / |x4-x3 x2-x1|
416 % |y2-y1 y3-y1| |y4-y3 y2-y1|
417 %
418 % t = |x4-x3 x3-x1| / |x4-x3 x2-x1|
419 % |y4-y3 y3-y1| |y4-y3 y2-y1|
420 %
421 % with 0<=s,t<=1
422 %
423 % s and t do not need to be calculated:
424 %
425 % Let s = A / C and t = B / C
426 %
427 % Then 0<=s<=1 if !(C=0) && ((A=0) || ((A>0) && !(C<A)) || ((A<0) && !(C>A)))
428 % 0<=t<=1 if !(C=0) && ((B=0) || ((B>0) && !(C<B)) || ((B<0) && !(C>B)))
429 %
430 \newif\ifpgf@s
431 \newif\ifpgf@t
432 \def\pgfiflinesintersect#1#2#3#4{%
433 \begingroup%
434 \pgf@iflinesintersect{\pgf@process{#1}}{\pgf@process{#2}}{\pgf@process{#3}}{\pgf@process{#4}}%
435 {\aftergroup\pgfutil@firstoftwo}{\aftergroup\pgfutil@secondoftwo}%
436 \endgroup%
437 }%
438
439 % queried by pgfplots. Do not delete, only increase.
440 \def\pgf@intersections@version{2}%
441
442 % #1,#2: line 1
443 % #3,#4: line 2
444 \def\pgf@iflinesintersect#1#2#3#4{%
445 % first: check bounding boxes -- but somewhat increased such that we do not
446 % exclude "visible" hits due to rounding issues (i.e. use an upper bound):
447 \pgf@intersect@boundingbox@reset%
448 \pgf@intersect@boundingbox@update{#1}%
449 \pgf@intersect@boundingbox@update{#2}%
450 \pgf@intersect@boundingbox@assign@b%
451 %
452 \pgf@intersect@boundingbox@reset%
453 \pgf@intersect@boundingbox@update{#3}%
454 \pgf@intersect@boundingbox@update{#4}%
455 \pgf@intersect@boundingbox@assign@a%
456 %
457 \pgf@intersect@boundingbox@a%
458 \pgf@intersect@boundingbox@b%
459 %
460 \pgf@intersect@boundingbox@ifoverlap@upperbound{%
461 \pgf@iflinesintersect@{#1}{#2}{#3}{#4}%
462 }{%
463 \let\pgf@intersect@next=\pgfutil@secondoftwo%
464 }%
465 \pgf@intersect@next%
466 }%
467
468 % a helper routine which simply defines \pgf@intersect@next.
469 %
470 % In principle, this routine is capable of computing the entire intersection... but we only invoke it after checking for bounding box overlaps. This has two reasons:
471 % 1. robustness. almost-parallel lines could cause "dimension too large" when solving the linear equation system
472 % XXX : I still needed to replace the linear solver by one using the FPU. Perhaps I do not need the BB check anymore?
473 % 2. performance. I hope it is faster to first check for BB (but this is not sure in TeX)
474 %
475 % #1,#2: line 1
476 % #3,#4: line 2
477 \def\pgf@iflinesintersect@#1#2#3#4{%
478 % we have two lines of sorts
479 % l_1(s) := #1 + s * (#2 - #1), 0<= s <= 1
480 % and
481 % l_2(t) := #3 + t * (#4 - #3), 0<= t <= 1
482 % ->
483 % set up LGS
484 % ( #2 - #1 ) *s + (#3-#4) * t = #3-#1
485 % we have a hit if 0<= s,t <= 1 .
486 #1\relax%
487 \pgf@xa=\pgf@x%
488 \pgf@ya=\pgf@y%
489 #2\relax%
490 \pgf@xb=\pgf@x%
491 \pgf@yb=\pgf@y%
492 #3\relax%
493 \pgf@xc=\pgf@x%
494 \pgf@yc=\pgf@y%
495 #4\relax%
496 %
497 % will be overwritten, remember it:
498 \edef\pgf@intersect@A{%
499 \pgf@xa=\the\pgf@xa\space
500 \pgf@ya=\the\pgf@ya\space
501 }%
502 %
503 % B := (2-1)
504 \advance\pgf@xb by-\pgf@xa
505 \advance\pgf@yb by-\pgf@ya
506 %
507 % A := (3-1)
508 \advance\pgf@xa by-\pgf@xc
509 \advance\pgf@ya by-\pgf@yc
510 \pgf@xa=-\pgf@xa
511 \pgf@ya=-\pgf@ya
512 %
513 % C := (3-4)
514 \advance\pgf@xc by-\pgf@x
515 \advance\pgf@yc by-\pgf@y
516 %
517 \begingroup
518 % compute the |.|_1 norm of each of lines. We need to compute
519 % tolerance factors in order to decide if we have an intersection.
520 % line 1: compute |#2 - #1|_1 :
521 \ifdim\pgf@xb<0sp \pgf@xb=-\pgf@xb\fi
522 \ifdim\pgf@yb<0sp \pgf@yb=-\pgf@yb\fi
523 \advance\pgf@xb by\pgf@yb
524 \xdef\pgf@intersect@len@a{\pgf@sys@tonumber\pgf@xb}%
525 %
526 % line 2: compute |#3 - #4|_1 :
527 \ifdim\pgf@xc<0sp \pgf@xc=-\pgf@xc\fi
528 \ifdim\pgf@yc<0sp \pgf@yc=-\pgf@yc\fi
529 \advance\pgf@xc by\pgf@yc
530 \xdef\pgf@intersect@len@b{\pgf@sys@tonumber\pgf@xc}%
531 \endgroup
532 %
533 \edef\pgf@marshal{%
534 \noexpand\pgfutilsolvetwotwoleqfloat{%
535 {\pgf@sys@tonumber\pgf@xb}{\pgf@sys@tonumber\pgf@xc}%
536 {\pgf@sys@tonumber\pgf@yb}{\pgf@sys@tonumber\pgf@yc}%
537 }{%
538 {\pgf@sys@tonumber\pgf@xa}%
539 {\pgf@sys@tonumber\pgf@ya}%
540 }%
541 }%
542 \pgf@marshal
543 %
544 \let\pgf@intersect@next=\pgfutil@secondoftwo%
545 \ifx\pgfmathresult\pgfutil@empty
546 % matrix was singular.
547 \else
548 \def\pgf@marshal##1##2{%
549 \global\pgf@x=##1pt %
550 \global\pgf@y=##2pt %
551 }%
552 \expandafter\pgf@marshal\pgfmathresult
553 %
554 \def\pgf@marshal{XXXX}% this should never be read
555 % FIRST: check line 1:
556 \ifdim\pgf@x<0sp
557 % let it count as hit if
558 % || l_1(s) - l_1(0) || < eps
559 % <=> |s| * ||#2 - #1|| < eps
560 % and, since s< 0 here:
561 % <=> -s * ||#2 - #1|| < eps
562 \pgf@xa=-\pgf@intersect@len@a\pgf@x
563 \ifdim\pgf@xa<\pgfintersectiontolerance\relax
564 % close enough to first endpoint of line 1:
565 \def\pgf@marshal{1}%
566 \else
567 \def\pgf@marshal{0}%
568 \fi
569 \else
570 \ifdim\pgf@x>1pt
571 % let it count as hit if
572 % || l_1(s) - l_1(1) || < eps
573 % <=> |s-1| * ||#2 - #1|| < eps
574 % and, since s > 1 here:
575 % <=> s * ||#2 - #1|| - ||#2 - #1|| < eps
576 \pgf@xa=\pgf@intersect@len@a\pgf@x
577 \advance\pgf@xa by-\pgf@intersect@len@a pt %
578 \ifdim\pgf@xa<\pgfintersectiontolerance\relax
579 % close enough to second endpoint of line 1:
580 \def\pgf@marshal{1}%
581 \else
582 \def\pgf@marshal{0}%
583 \fi
584 \else
585 % 0<= s <= 1: we have an intersection within line 1.
586 \def\pgf@marshal{1}%
587 \fi
588 \fi
589 %
590 % SECOND: check line 2:
591 \if1\pgf@marshal
592 \ifdim\pgf@y<0sp
593 % see remarks for line 1. same applies here.
594 \pgf@xa=-\pgf@intersect@len@b\pgf@y
595 \ifdim\pgf@xa<\pgfintersectiontolerance\relax
596 % close enough to first endpoint of line 2:
597 \def\pgf@marshal{1}%
598 \else
599 \def\pgf@marshal{0}%
600 \fi
601 \else
602 \ifdim\pgf@y>1pt
603 % see remarks for line 1. same applies here.
604 \pgf@xa=\pgf@intersect@len@b\pgf@y
605 \advance\pgf@xa by-\pgf@intersect@len@b pt %
606 \ifdim\pgf@xa<\pgfintersectiontolerance\relax
607 % close enough to second endpoint of line 2:
608 \def\pgf@marshal{1}%
609 \else
610 \def\pgf@marshal{0}%
611 \fi
612 \else
613 % 0<= t <= 1: we have an intersection within line 2.
614 \def\pgf@marshal{1}%
615 \fi
616 \fi
617 \fi
618 %
619 \if1\pgf@marshal
620 % Ok, compute the intersection point and return it:
621 % we use (x,y) = A + s * (B-A)
622 % keep in mind that (s,t) == (\pgf@x,\pgf@y)
623 \pgf@intersect@A
624 \pgf@yc=\pgf@x
625 \global\pgf@x=\pgf@sys@tonumber\pgf@xb\pgf@yc
626 \global\pgf@y=\pgf@sys@tonumber\pgf@yb\pgf@yc
627 \global\advance\pgf@x by \pgf@xa
628 \global\advance\pgf@y by \pgf@ya
629 \let\pgf@intersect@next=\pgfutil@firstoftwo%
630 \fi
631 \fi
632 }%
633
634
635
636
637 \def\pgfintersectionoflineandcurve#1#2#3#4#5#6{%
638 \pgf@intersect@solutions=0\relax%
639 \pgf@intersectionoflineandcurve{#1}{#2}{#3}{#4}{#5}{#6}%
640 }%
641
642 \def\pgf@intersectionoflineandcurve#1#2#3#4#5#6{%
643 \pgf@intersectionofcurves%
644 {\pgf@process{#1}}%
645 {%
646 \pgf@process{%
647 \pgfpointadd{#1\relax\pgf@x=0.666666\pgf@x\pgf@y=0.666666\pgf@y}%
648 {#2\relax\pgf@x=0.333333\pgf@x\pgf@y=0.333333\pgf@y}%
649 }%
650 }%
651 {%
652 \pgf@process{%
653 \pgfpointadd{#1\relax\pgf@x=0.333333\pgf@x\pgf@y=0.333333\pgf@y}%
654 {#2\relax\pgf@x=0.666666\pgf@x\pgf@y=0.666666\pgf@y}%
655 }%
656 }%
657 {\pgf@process{#2}}%
658 {\pgf@process{#3}}%
659 {\pgf@process{#4}}%
660 {\pgf@process{#5}}%
661 {\pgf@process{#6}}%
662 }%
663
664 \def\pgf@intersectionofcurveandline#1#2#3#4#5#6{%
665 \pgf@intersectionofcurves%
666 {\pgf@process{#1}}%
667 {\pgf@process{#2}}%
668 {\pgf@process{#3}}%
669 {\pgf@process{#4}}%
670 {\pgf@process{#5}}%
671 {%
672 \pgf@process{%
673 \pgfpointadd{#5\relax\pgf@x=0.666666\pgf@x\pgf@y=0.666666\pgf@y}%
674 {#6\relax\pgf@x=0.333333\pgf@x\pgf@y=0.333333\pgf@y}%
675 }%
676 }%
677 {%
678 \pgf@process{%
679 \pgfpointadd{#5\relax\pgf@x=0.333333\pgf@x\pgf@y=0.333333\pgf@y}%
680 {#6\relax\pgf@x=0.666666\pgf@x\pgf@y=0.666666\pgf@y}%
681 }%
682 }%
683 {\pgf@process{#6}}%
684 }%
685
686
687
688
689 \def\pgfintersectiontolerance{0.1pt}%
690 \def\pgfintersectiontoleranceupperbound{1pt}%
691 \def\pgfintersectiontolerancefactor{0.1}%
692
693
694
695 % Find the intersections of two bezier curves.
696 %
697 % #1 - #4 = curve 1.
698 % #5 - #8 = curve 2.
699 % #9 = the solution number.
700 %
701 % There is no guarantee of ordering of solutions. If there are
702 % no solutions, the origin is returned.
703 %
704 \def\pgfpointintersectionofcurves#1#2#3#4#5#6#7#8#9{%
705 \pgf@intersect@solutions=0\relax%
706 \pgf@intersectionofcurves%
707 {\pgf@process{#1}}{\pgf@process{#2}}{\pgf@process{#3}}{\pgf@process{#4}}%
708 {\pgf@process{#5}}{\pgf@process{#6}}{\pgf@process{#7}}{\pgf@process{#8}}%
709 \pgfpointintersectionsolution{#9}%
710 }%
711
712 % Return any intersection points of two curves C1 and C2.
713 % No order can be guaranteed for the solutions.
714 %
715 % #1, #2, #3, #4 - the points on C1
716 % #5, #6, #7, #8 - the points on C2
717 %
718 % Returns:
719 %
720 % \pgf@intersect@solutions - the number of solutions.
721 % \pgfpointintersectionsolution{<S>} - the point for solution S.
722 %
723 % (Sort of) use:
724 %
725 % intersection(C1,C2)
726 % S = {};
727 % intersection'(C1,C2);
728 % return S;
729 %
730 % intersection'(C1,C2)
731 % B1 = boundingbox(C1);
732 % B2 = boundingbox(C2);
733 % if intersect(B1,B2)
734 % if (B1.width < q) and (B1.height < q) and
735 % (B2.width < q) and (B2.height < q)
736 % S = S + {average_of_all_points(B1,B2)}; \\ is there a better choice?
737 % else
738 % Q = subdivideLeft(C1);
739 % R = subdivideRight(C1);
740 % intersection'(C2,Q);
741 % intersection'(C2,R);
742 %
743 % where q is a small value (tolerance).
744 %
745 \def\pgfintersectionofcurves#1#2#3#4#5#6#7#8{%
746 \pgf@intersect@solutions=0\relax%
747 \pgf@intersectionofcurves%
748 {\pgf@process{#1}}{\pgf@process{#2}}{\pgf@process{#3}}{\pgf@process{#4}}%
749 {\pgf@process{#5}}{\pgf@process{#6}}{\pgf@process{#7}}{\pgf@process{#8}}%
750 }%
751 \def\pgf@intersectionofcurves#1#2#3#4#5#6#7#8{%
752 \begingroup%
753 \dimendef\pgf@time@a=2\relax%
754 \dimendef\pgf@time@aa=4\relax%
755 \dimendef\pgf@time@b=6\relax%
756 \dimendef\pgf@time@bb=8\relax%
757 \pgf@time@a=0pt\relax%
758 \pgf@time@aa=1pt\relax%
759 \pgf@time@b=0pt\relax%
760 \pgf@time@bb=1pt\relax%
761 \let\pgf@intersect@subdivide@curve=\pgf@intersect@subdivide@curve@b%
762 \let\pgf@curve@subdivde@after=\pgf@@intersectionofcurves%
763 \pgf@@intersectionofcurves{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}%
764 \endgroup%
765 }%
766
767 \def\pgf@intersect@boundingbox@assign@a{%
768 \edef\pgf@intersect@boundingbox@a{%
769 % lower left:
770 \noexpand\pgf@xb=\the\pgf@xa\space%
771 \noexpand\pgf@yb=\the\pgf@ya\space%
772 % upper right:
773 \noexpand\pgf@xc=\the\pgf@xb\space%
774 \noexpand\pgf@yc=\the\pgf@yb\space%
775 }%
776 }%
777 \def\pgf@intersect@boundingbox@assign@b{%
778 \edef\pgf@intersect@boundingbox@b{%
779 % lower left:
780 \noexpand\global\noexpand\pgf@x=\the\pgf@xa\space%
781 \noexpand\global\noexpand\pgf@y=\the\pgf@ya\space%
782 % upper right:
783 \noexpand\pgf@xa=\the\pgf@xb\space%
784 \noexpand\pgf@ya=\the\pgf@yb\space%
785 }%
786 }%
787
788 % see \pgf@intersect@boundingbox@assign@a and \pgf@intersect@boundingbox@assign@b for the naming conventions
789 \def\pgf@intersect@boundingbox@ifoverlap{%
790 \def\pgf@intersect@next{\pgfutil@secondoftwo}%
791 %
792 \ifdim\pgf@xa<\pgf@xb%
793 \else%
794 \ifdim\pgf@x>\pgf@xc%
795 \else%
796 \ifdim\pgf@ya<\pgf@yb%
797 \else%
798 \ifdim\pgf@y>\pgf@yc%
799 \else%
800 \def\pgf@intersect@next{\pgfutil@firstoftwo}%
801 \fi
802 \fi
803 \fi
804 \fi
805 \pgf@intersect@next
806 }%
807 \def\pgf@intersect@boundingbox@ifoverlap@upperbound{%
808 \begingroup
809 \def\pgf@intersect@next{\pgfutil@secondoftwo}%
810 %
811 \advance\pgf@xa by+\pgfintersectiontolerance\relax
812 \ifdim\pgf@xa<\pgf@xb%
813 \else%
814 \global\advance\pgf@x by-\pgfintersectiontolerance\relax
815 \ifdim\pgf@x>\pgf@xc%
816 \else%
817 \advance\pgf@ya by\pgfintersectiontolerance\relax
818 \ifdim\pgf@ya<\pgf@yb%
819 \else%
820 \global\advance\pgf@y by-\pgfintersectiontolerance\relax
821 \ifdim\pgf@y>\pgf@yc%
822 \else%
823 \def\pgf@intersect@next{\pgfutil@firstoftwo}%
824 \fi
825 \fi
826 \fi
827 \fi
828 \expandafter
829 \endgroup
830 \pgf@intersect@next
831 }%
832 \def\pgf@intersect@boundingbox@ifoverlap@UNUSED{%
833 \let\pgf@intersect@next=\pgfutil@secondoftwo%
834 \ifdim\pgf@xa<\pgf@xb%
835 \else%
836 \ifdim\pgf@x>\pgf@xc%
837 \else%
838 \ifdim\pgf@ya<\pgf@yb%
839 \else%
840 \ifdim\pgf@y>\pgf@yc%
841 \else%
842 \let\pgf@intersect@next=\pgfutil@firstoftwo%
843 \fi
844 \fi
845 \fi
846 \fi
847 \pgf@intersect@next
848 }%
849 \def\pgf@@intersectionofcurves#1#2#3#4#5#6#7#8{%
850 \pgf@intersect@boundingbox@reset%
851 \pgf@intersect@boundingbox@update{#1}%
852 \pgf@intersect@boundingbox@update{#2}%
853 \pgf@intersect@boundingbox@update{#3}%
854 \pgf@intersect@boundingbox@update{#4}%
855 \pgf@intersect@boundingbox@assign@b%
856 %
857 \pgf@intersect@boundingbox@reset%
858 \pgf@intersect@boundingbox@update{#5}%
859 \pgf@intersect@boundingbox@update{#6}%
860 \pgf@intersect@boundingbox@update{#7}%
861 \pgf@intersect@boundingbox@update{#8}%
862 \pgf@intersect@boundingbox@assign@a%
863 %
864 \pgf@intersect@boundingbox@a%
865 \pgf@intersect@boundingbox@b%
866 %
867 \pgf@intersect@boundingbox@ifoverlap{%
868 \pgf@@@intersectionofcurves{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}%
869 }{%
870 % no overlap -- no intersection.
871 }%
872 }%
873
874 \def\pgf@@@intersectionofcurves#1#2#3#4#5#6#7#8{%
875 % compute DIFFERENCE vectors:
876 \advance\pgf@xc by-\pgf@xb%
877 \advance\pgf@yc by-\pgf@yb%
878 \advance\pgf@xa by-\pgf@x%
879 \advance\pgf@ya by-\pgf@y%
880 \let\pgf@intersect@subdivde=\relax%
881 % check if both difference vectors are point wise
882 % less than tolerance (i.e. |v|_infty < eps ).
883 % That means that both bounding boxes are "small enough"
884 \ifdim\pgf@xc<\pgfintersectiontolerance\relax%
885 \ifdim\pgf@xa<\pgfintersectiontolerance\relax%
886 \ifdim\pgf@yc<\pgfintersectiontolerance\relax%
887 \ifdim\pgf@ya<\pgfintersectiontolerance\relax%
888 \pgfextract@process\pgf@intersect@solution@candidate{%
889 % set (x,y) = mean(the 4 points of the two bounding boxes):
890 \pgf@intersect@boundingbox@a%
891 \pgf@intersect@boundingbox@b%
892 \pgf@x=0.25\pgf@x%
893 \advance\pgf@x by0.25\pgf@xa%
894 \advance\pgf@x by0.25\pgf@xb%
895 \advance\pgf@x by0.25\pgf@xc%
896 \pgf@y=0.25\pgf@y%
897 \advance\pgf@y by0.25\pgf@ya%
898 \advance\pgf@y by0.25\pgf@yb%
899 \advance\pgf@y by0.25\pgf@yc%
900 }%
901 % We must avoid duplicate solutions.
902 \let\pgf@intersect@subdivde=\pgf@stop%
903 \pgf@ifsolution@duplicate\pgf@intersect@solution@candidate{}%
904 {%
905 \global\advance\pgf@intersect@solutions by1\relax%
906 \begingroup
907 \advance\pgf@time@a by\pgf@time@aa%
908 \divide\pgf@time@a by2\relax%
909 \advance\pgf@time@a by\pgf@intersect@time@offset pt\relax%
910 \edef\pgf@intersect@time@a{\pgfmath@tonumber{\pgf@time@a}}%
911 %
912 \advance\pgf@time@b by\pgf@time@bb%
913 \divide\pgf@time@b by2\relax%
914 \advance\pgf@time@b by\pgf@intersect@time@offset@b pt\relax%
915 \edef\pgf@intersect@time@b{\pgfmath@tonumber{\pgf@time@b}}%
916 %
917 \pgf@intersection@store@properties{pgfpoint@g@intersect@solution@\the\pgf@intersect@solutions}%
918 \expandafter\global\expandafter\let%
919 \csname pgfpoint@g@intersect@solution@\the\pgf@intersect@solutions\endcsname=%
920 \pgf@intersect@solution@candidate%
921 \ifpgf@intersect@sort%
922 \expandafter\xdef%
923 \csname pgf@g@intersect@solution@\the\pgf@intersect@solutions @time@a\endcsname%
924 {\pgf@intersect@time@a}%
925 \fi%
926 \endgroup
927 }%
928 \fi%
929 \fi%
930 \fi%
931 \fi%
932 \ifx\pgf@intersect@subdivde\pgf@stop%
933 \else%
934 \pgf@intersect@subdivide@curve{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}%
935 \fi%
936 }%
937
938 \def\pgf@intersect@subdivide@curve@b#1#2#3#4#5#6#7#8{%
939 \begingroup%
940 \advance\pgf@time@bb by\pgf@time@b\relax%
941 \divide\pgf@time@bb by2\relax%
942 \let\pgf@intersect@subdivide@curve=\pgf@intersect@subdivide@curve@a%
943 \pgf@curve@subdivide@left{#5}{#6}{#7}{#8}{#1}{#2}{#3}{#4}%
944 \endgroup%
945 \begingroup%
946 \advance\pgf@time@b by\pgf@time@bb\relax%
947 \divide\pgf@time@b by2\relax%
948 \let\pgf@intersect@subdivide@curve=\pgf@intersect@subdivide@curve@a%
949 \pgf@curve@subdivide@right{#5}{#6}{#7}{#8}{#1}{#2}{#3}{#4}%
950 \endgroup%
951 }%
952
953 \def\pgf@intersect@subdivide@curve@a#1#2#3#4#5#6#7#8{%
954 \begingroup%
955 \advance\pgf@time@aa by\pgf@time@a\relax%
956 \divide\pgf@time@aa by2\relax%
957 \let\pgf@intersect@subdivide@curve=\pgf@intersect@subdivide@curve@b%
958 \pgf@curve@subdivide@left{#5}{#6}{#7}{#8}{#1}{#2}{#3}{#4}%
959 \endgroup%
960 \begingroup%
961 \advance\pgf@time@a by\pgf@time@aa\relax%
962 \divide\pgf@time@a by2\relax%
963 \let\pgf@intersect@subdivide@curve=\pgf@intersect@subdivide@curve@b%
964 \pgf@curve@subdivide@right{#5}{#6}{#7}{#8}{#1}{#2}{#3}{#4}%
965 \endgroup%
966 }%
967
968 \def\pgf@intersect@boundingbox@reset{%
969 \pgf@xa=16000pt\relax%
970 \pgf@ya=16000pt\relax%
971 \pgf@xb=-16000pt\relax%
972 \pgf@yb=-16000pt\relax%
973 }%
974
975 \def\pgf@intersect@boundingbox@update#1{%
976 #1\relax%
977 \ifdim\pgf@x<\pgf@xa\pgf@xa=\pgf@x\fi%
978 \ifdim\pgf@y<\pgf@ya\pgf@ya=\pgf@y\fi%
979 \ifdim\pgf@x>\pgf@xb\pgf@xb=\pgf@x\fi%
980 \ifdim\pgf@y>\pgf@yb\pgf@yb=\pgf@y\fi%
981 }%
982
983 % The following subroutines are part of a conversion from pgfbasic
984 % math to FPU. This transition is necessary due to the restricted
985 % accuracy of pgfbasic. In order to limit the error rate of the
986 % transition pgfbasic -> FPU, I chose to
987 % keep the old "pattern" of sorts \advance\pgf@xa by0.5\pgf@y etc and
988 % simply adapt to some FPU call.
989 %
990 % The following routines constitute the "adapter":
991
992 \def\pgf@float@adapter@setxy{%
993 \pgfmathfloatparsenumber{\pgf@sys@tonumber\pgf@x}\let\pgf@fpu@x=\pgfmathresult
994 \pgfmathfloatparsenumber{\pgf@sys@tonumber\pgf@y}\let\pgf@fpu@y=\pgfmathresult
995 }%
996 \def\pgf@float@adapter@mult#1=#2*#3{%
997 \pgfmathfloatmultiplyfixed@{#3}{#2}%
998 \let#1=\pgfmathresult
999 }%
1000 \def\pgf@float@adapter@advance#1by#2*#3{%
1001 \pgfmathfloatmultiplyfixed@{#3}{#2}%
1002 \let\pgfutil@temp=\pgfmathresult
1003 \pgfmathfloatadd@{#1}{\pgfutil@temp}%
1004 \let#1=\pgfmathresult
1005 }%
1006
1007 \def\pgf@float@adapter@tostring#1{%
1008 \pgfmathfloattofixed{#1}\edef#1{\pgfmathresult pt }%
1009 }%
1010
1011 \def\pgf@curve@subdivide@left#1#2#3#4{%
1012 %
1013 % The left curve (from t=0 to t=.5)
1014 %
1015 \begingroup
1016 #1\relax%
1017 \pgfutil@tempdima=\pgf@x%
1018 \pgfutil@tempdimb=\pgf@y%
1019 \pgf@float@adapter@setxy
1020 \pgf@float@adapter@mult\pgf@fpu@xa=.5*\pgf@fpu@x \pgf@float@adapter@mult\pgf@fpu@ya=.5*\pgf@fpu@y%
1021 \pgf@float@adapter@mult\pgf@fpu@xb=.25*\pgf@fpu@x \pgf@float@adapter@mult\pgf@fpu@yb=.25*\pgf@fpu@y%
1022 \pgf@float@adapter@mult\pgf@fpu@xc=.125*\pgf@fpu@x\pgf@float@adapter@mult\pgf@fpu@yc=.125*\pgf@fpu@y%
1023 #2\relax%
1024 \pgf@float@adapter@setxy
1025 \pgf@float@adapter@advance\pgf@fpu@xa by.5*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@ya by.5*\pgf@fpu@y%
1026 \pgf@float@adapter@advance\pgf@fpu@xb by.5*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@yb by.5*\pgf@fpu@y%
1027 \pgf@float@adapter@advance\pgf@fpu@xc by.375*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@yc by.375*\pgf@fpu@y%
1028 #3\relax%
1029 \pgf@float@adapter@setxy
1030 \pgf@float@adapter@advance\pgf@fpu@xb by.25*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@yb by.25*\pgf@fpu@y%
1031 \pgf@float@adapter@advance\pgf@fpu@xc by.375*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@yc by.375*\pgf@fpu@y%
1032 #4\relax%
1033 \pgf@float@adapter@setxy
1034 \pgf@float@adapter@advance\pgf@fpu@xc by.125*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@yc by.125*\pgf@fpu@y%
1035 %
1036 \pgf@float@adapter@tostring\pgf@fpu@xa
1037 \pgf@float@adapter@tostring\pgf@fpu@ya
1038 \pgf@float@adapter@tostring\pgf@fpu@xb
1039 \pgf@float@adapter@tostring\pgf@fpu@yb
1040 \pgf@float@adapter@tostring\pgf@fpu@xc
1041 \pgf@float@adapter@tostring\pgf@fpu@yc
1042 \edef\pgf@marshal{%
1043 \noexpand\pgf@curve@subdivde@after%
1044 {\noexpand\pgf@x=\the\pgfutil@tempdima\noexpand\pgf@y=\the\pgfutil@tempdimb}%
1045 {\noexpand\pgf@x=\pgf@fpu@xa\noexpand\pgf@y=\pgf@fpu@ya}%
1046 {\noexpand\pgf@x=\pgf@fpu@xb\noexpand\pgf@y=\pgf@fpu@yb}%
1047 {\noexpand\pgf@x=\pgf@fpu@xc\noexpand\pgf@y=\pgf@fpu@yc}%
1048 }%
1049 \expandafter
1050 \endgroup
1051 \pgf@marshal%
1052 }%
1053
1054 \def\pgf@curve@subdivide@right#1#2#3#4{%
1055 %
1056 % The right curve (from t=0.5 to t=1)
1057 %
1058 \begingroup
1059 #1\relax%
1060 \pgf@float@adapter@setxy
1061 \pgf@float@adapter@mult\pgf@float@tmpa=.125*\pgf@fpu@x\pgf@float@adapter@mult\pgf@float@tmpb=.125*\pgf@fpu@y%
1062 #2\relax%
1063 \pgf@float@adapter@setxy
1064 \pgf@float@adapter@advance\pgf@float@tmpa by.375*\pgf@fpu@x\pgf@float@adapter@advance\pgf@float@tmpb by.375*\pgf@fpu@y%
1065 \pgf@float@adapter@mult\pgf@fpu@xa=.25*\pgf@fpu@x\pgf@float@adapter@mult\pgf@fpu@ya=.25*\pgf@fpu@y%
1066 #3\relax%
1067 \pgf@float@adapter@setxy
1068 \pgf@float@adapter@advance\pgf@float@tmpa by.375*\pgf@fpu@x\pgf@float@adapter@advance\pgf@float@tmpb by.375*\pgf@fpu@y%
1069 \pgf@float@adapter@advance\pgf@fpu@xa by.5*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@ya by.5*\pgf@fpu@y%
1070 \pgf@float@adapter@mult\pgf@fpu@xb=.5*\pgf@fpu@x\pgf@float@adapter@mult\pgf@fpu@yb=.5*\pgf@fpu@y%
1071 #4\relax%
1072 \pgf@float@adapter@setxy
1073 \pgf@float@adapter@advance\pgf@float@tmpa by.125*\pgf@fpu@x\pgf@float@adapter@advance\pgf@float@tmpb by.125*\pgf@fpu@y%
1074 \pgf@float@adapter@advance\pgf@fpu@xa by.25*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@ya by.25*\pgf@fpu@y%
1075 \pgf@float@adapter@advance\pgf@fpu@xb by.5*\pgf@fpu@x\pgf@float@adapter@advance\pgf@fpu@yb by.5*\pgf@fpu@y%
1076 \let\pgf@fpu@xc=\pgf@fpu@x\let\pgf@fpu@yc=\pgf@fpu@y%
1077 %
1078 \pgf@float@adapter@tostring\pgf@float@tmpa
1079 \pgf@float@adapter@tostring\pgf@float@tmpb
1080 \pgf@float@adapter@tostring\pgf@fpu@xa
1081 \pgf@float@adapter@tostring\pgf@fpu@ya
1082 \pgf@float@adapter@tostring\pgf@fpu@xb
1083 \pgf@float@adapter@tostring\pgf@fpu@yb
1084 \pgf@float@adapter@tostring\pgf@fpu@xc
1085 \pgf@float@adapter@tostring\pgf@fpu@yc
1086 \edef\pgf@marshal{%
1087 \noexpand\pgf@curve@subdivde@after%
1088 {\noexpand\pgf@x=\pgf@float@tmpa\noexpand\pgf@y=\pgf@float@tmpb}%
1089 {\noexpand\pgf@x=\pgf@fpu@xa\noexpand\pgf@y=\pgf@fpu@ya}%
1090 {\noexpand\pgf@x=\pgf@fpu@xb\noexpand\pgf@y=\pgf@fpu@yb}%
1091 {\noexpand\pgf@x=\pgf@fpu@xc\noexpand\pgf@y=\pgf@fpu@yc}%
1092 }%
1093 \expandafter
1094 \endgroup
1095 \pgf@marshal%
1096 }%
1097
1098
1099 % A solution S1 is considered a duplicate of S2, if
1100 %
1101 % |x1 - x2|f < q and |y1 - y2|f < q
1102 %
1103 % where q is a small value (tolerance).
1104 %
1105 % #1 - the solution.
1106 %
1107 \def\pgf@ifsolution@duplicate#1{%
1108 #1%
1109 \pgf@xa=\pgf@x%
1110 \pgf@ya=\pgf@y%
1111 \let\pgf@intersect@next=\pgfutil@secondoftwo%
1112 \pgfmathloop%
1113 \ifnum\pgfmathcounter>\pgf@intersect@solutions\relax%
1114 \else%
1115 \pgf@ifsolution@duplicate@{\pgfmathcounter}%
1116 \repeatpgfmathloop%
1117 \pgf@intersect@next%
1118 }%
1119
1120 \def\pgf@ifsolution@duplicate@#1{%
1121 \pgf@process{\csname pgfpoint@g@intersect@solution@#1\endcsname}%
1122 \advance\pgf@x by-\pgf@xa%
1123 \advance\pgf@y by-\pgf@ya%
1124 \ifdim\pgf@x<0pt\relax\pgf@x=-\pgf@x\fi%
1125 \ifdim\pgf@y<0pt\relax\pgf@y=-\pgf@y\fi%
1126 %
1127 \pgf@x=\pgfintersectiontolerancefactor\pgf@x%
1128 \pgf@y=\pgfintersectiontolerancefactor\pgf@y%
1129 \ifdim\pgf@x<\pgfintersectiontolerance\relax%
1130 \ifdim\pgf@y<\pgfintersectiontolerance\relax%
1131 \let\pgf@intersect@next=\pgfutil@firstoftwo%
1132 \fi%
1133 \fi%
1134 }%
1135
1136 \newif\ifpgf@intersect@solutions@sortfinish
1137
1138 % Sort solutions according to their time index.
1139 %
1140 \def\pgfintersectionsolutionsortbytime{%
1141 \pgf@intersect@solutions@sortfinishtrue%
1142 \pgfmathloop%
1143 \ifnum\pgfmathcounter<\pgfintersectionsolutions\relax%
1144 \pgfutil@tempcnta=\pgfmathcounter%
1145 \advance\pgfutil@tempcnta by1\relax%
1146 \ifdim\csname pgf@intersect@solution@\pgfmathcounter @time@a\endcsname pt>%
1147 \csname pgf@intersect@solution@\the\pgfutil@tempcnta @time@a\endcsname pt\relax%
1148 \pgf@intersect@solutions@sortfinishfalse%
1149 %
1150 \pgfintersectionsolutionsortbytime@swap{pgfpoint@intersect@solution@\pgfmathcounter}%
1151 {pgfpoint@intersect@solution@\the\pgfutil@tempcnta}%
1152 %
1153 \pgfintersectionsolutionsortbytime@swap{pgf@intersect@solution@\pgfmathcounter @time@a}%
1154 {pgf@intersect@solution@\the\pgfutil@tempcnta @time@a}%
1155 %
1156 \pgfintersectionsolutionsortbytime@swap{pgf@intersect@solution@props@\pgfmathcounter}%
1157 {pgf@intersect@solution@props@\the\pgfutil@tempcnta}%
1158 \fi%
1159 \repeatpgfmathloop%
1160 \ifpgf@intersect@solutions@sortfinish%
1161 \else%
1162 \expandafter\pgfintersectionsolutionsortbytime%
1163 \fi%
1164 }%
1165
1166 \def\pgfintersectionsolutionsortbytime@swap#1#2{%
1167 \pgfutil@namelet{pgf@intersect@temp}{#1}%
1168 \pgfutil@namelet{#1}{#2}%
1169 \pgfutil@namelet{#2}{pgf@intersect@temp}%
1170 }%
1171
1172 \endinput