Debian Codegraph @

texmf-dist/tex/generic/pgf/math/pgfmathcalc.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 Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10 % This file loads all the parsing, functions and operator stuff
11 %
12 % Version 1.414213 29/9/2007
13
14 % \pgfmathsetlength, \pgfmathaddtolength
15 %
16 % #1 = dimension register
17 % #2 = expression
18 %
19 % Description:
20 %
21 % These functions work similar to \setlength and \addtolength. Only,
22 % they allow #2 to contain an expression, which is evaluated before
23 % assignment. Furthermore, the font is setup before the assignment is
24 % done, so that dimensions like 1em are evaluated correctly.
25 %
26 % If #2 starts with "+", then a simple assignment is done (but the
27 % font is still setup). This is orders of magnitude faster than a
28 % parsed assignment.
29
30 \def\pgfmathsetlength#1#2{%
31 \expandafter\pgfmath@onquick#2\pgfmath@%
32 {%
33 % Ok, quick version:
34 \begingroup%
35 \pgfmath@selectfont%
36 #1#2\unskip%
37 \expandafter\endgroup%
38 \expandafter#1\the#1\relax%
39 }%
40 {%
41 \pgfmathparse{#2}%
42 \ifpgfmathmathunitsdeclared%
43 #1\pgfmathresult mu\relax%
44 \else%
45 #1\pgfmathresult pt\relax%
46 \fi%
47 }%
48 \ignorespaces%
49 }
50
51 % \pgfmathaddtolength
52 %
53 % Add to #1 the result of evaluating #2.
54 %
55 % #1 - a dimension register
56 % #2 - an expression
57 %
58 \def\pgfmathaddtolength#1#2{%
59 \expandafter\pgfmath@onquick#2\pgfmath@%
60 {%
61 \begingroup%
62 \pgfmath@selectfont%
63 \advance#1#2\unskip%
64 \expandafter%
65 \endgroup%
66 \expandafter#1\the#1\relax%
67 }%
68 {%
69 \pgfmathparse{#2}%
70 \ifpgfmathmathunitsdeclared%
71 \advance#1\pgfmathresult mu\relax%
72 \else%
73 \advance#1\pgfmathresult pt\relax%
74 \fi%
75 }%
76 \ignorespaces%
77 }
78
79 % \pgfmathsetcount
80 %
81 % Assign #1 the truncated evaluation of #2.
82 %
83 % #1 - a count register
84 % #2 - an expression
85 %
86 \def\pgfmathsetcount#1#2{%
87 \expandafter\pgfmath@onquick#2\pgfmath@%
88 {%
89 \afterassignment\pgfmath@gobbletilpgfmath@%
90 #1#2\relax\pgfmath@%
91 }%
92 {%
93 \pgfmathparse{#2}%
94 \afterassignment\pgfmath@gobbletilpgfmath@%
95 #1\pgfmathresult\relax\pgfmath@%
96 }%
97 \ignorespaces%
98 }
99
100 % \pgfmathaddtocount
101 %
102 % Add to #1 the truncated evaluation of #2.
103 %
104 % #1 - a count register
105 % #2 - an expression
106 %
107 \def\pgfmathaddtocount#1#2{%
108 \expandafter\pgfmath@onquick#2\pgfmath@%
109 {%
110 \edef\pgfmath@addtocounttemp{\the#1}%
111 \afterassignment\pgfmath@gobbletilpgfmath@%
112 #1#2\relax\pgfmath@%
113 \advance#1\pgfmath@addtocounttemp\relax%
114 }%
115 {%
116 \edef\pgfmath@addtocounttemp{\the#1}%
117 \pgfmathparse{#2}%
118 \afterassignment\pgfmath@gobbletilpgfmath@%
119 #1\pgfmathresult\relax\pgfmath@%
120 \advance#1\pgfmath@addtocounttemp\relax%
121 }%
122 \ignorespaces%
123 }
124
125 % \pgfmathnewcounter
126 %
127 % LaTeX style counter which also works in plain TeX. Defines
128 % \c@<name> as a count register and also defines \the<name>.
129 %
130 % #1 the name of the counter.
131 %
132 % Example:
133 %
134 % \pgfmathnewcounter{counter}
135 % \pgfmathsetcounter{counter}{12}
136 % \thecounter (same as \the\c@counter)
137 %
138 \def\pgfmathnewcounter#1{%
139 \expandafter\ifx\csname c@#1\endcsname\relax%
140 \def\pgfmath@marshal{\csname newcount\endcsname}% Ha! Who cares about \outer?
141 \expandafter\pgfmath@marshal\csname c@#1\endcsname%
142 \expandafter\def\csname the#1\endcsname{\expandafter\the\csname c@#1\endcsname}%
143 \fi%
144 }
145
146 % \pgfmathsetcounter
147 %
148 % Set the counter #1 to the evaluation of #2.
149 %
150 % #1 - a counter name
151 % #2 - an expression
152 %
153 \def\pgfmathsetcounter#1#2{%
154 \expandafter\pgfmathsetcount\csname c@#1\endcsname{#2}%
155 }
156
157 % \pgfmathaddtocounter
158 %
159 % Add the counter #1 to the evaluation of #2.
160 %
161 % #1 - a counter name
162 % #2 - an expression
163 %
164 \def\pgfmathaddtocounter#1#2{%
165 \expandafter\pgfmathaddtocount\csname c@#1\endcsname{#2}%
166 }
167
168 % \pgfmathmakecounterglobal
169 %
170 % Make the current value of a counter globally defined.
171 %
172 % #1 - a (valid) counter name.
173 %
174 \def\pgfmath@pgftext{pgf}
175 \def\pgfmath@tikztext{tikz}
176 \def\pgfmathmakecounterglobal#1{%
177 \pgfmath@ifundefined{c@#1}{}{%
178 \expandafter\pgfmath@in@\expandafter{\pgfmath@pgftext}{#1}%
179 \ifpgfmath@in@%
180 \else%
181 \expandafter\pgfmath@in@\expandafter{\pgfmath@tikztext}{#1}%
182 \ifpgfmath@in@%
183 \else%
184 \expandafter\global\csname c@#1\endcsname\csname c@#1\endcsname\relax%
185 \fi%
186 \fi%
187 }%
188 }
189
190 % \pgfmathsetmacro
191 %
192 % \edef#1 as the result of evaluating #2.
193 %
194 % #1 - a macro
195 % #2 - an expression
196 %
197 \def\pgfmathsetmacro#1#2{%
198 \begingroup%
199 \pgfmathparse{#2}%
200 \let#1=\pgfmathresult
201 \pgfmath@smuggleone{#1}%
202 \endgroup%
203 }
204
205 % \pgfmathsetlengthmacro
206 %
207 % \edef#1 as the result of evaluating #2 with pt.
208 %
209 % #1 - a macro
210 % #2 - an expression
211 %
212 \def\pgfmathsetlengthmacro#1#2{%
213 \begingroup%
214 \pgfmathsetlength\pgfmath@x{#2}%
215 \edef#1{\the\pgfmath@x}%
216 \pgfmath@smuggleone{#1}%
217 \endgroup%
218 }
219
220 % \pgfmathtruncatemacro
221 %
222 % \edef#1 as the truncated result of evaluating #2.
223 %
224 % #1 - a macro
225 % #2 - an expression
226 %
227 \def\pgfmathtruncatemacro#1#2{%
228 \begingroup%
229 \pgfmathsetcount\c@pgfmath@counta{#2}%
230 \edef#1{\the\c@pgfmath@counta}%
231 \pgfmath@smuggleone{#1}%
232 \endgroup%
233 }
234
235 % Check whether a given parameter starts with quick.
236 %
237 % The command should be followed by nonempty text, ending with
238 % \pgfmath@ as a stop-token. Then should follow
239 %
240 % #1 - code to execute if text starts with +
241 % #2 - code to execute if text does not
242 %
243 % Example:
244 %
245 % \pgfmath@onquick+0pt\pgfmath@{is quick}{is slow}
246
247 \def\pgfmath@onquick{%
248 \futurelet\pgfmath@next%
249 \pgfmath@@onquick}
250 \def\pgfmath@@onquick#1\pgfmath@{%
251 \ifx\pgfmath@next+%
252 \let\pgfmath@next=\pgfmath@firstoftwo%
253 \else%
254 \let\pgfmath@next=\pgfmath@secondoftwo%
255 \fi%
256 \pgfmath@next%
257 }
258
259 % *** The following commands DO NOT WORK without the rest of PGF ***
260 %
261 % (a dumping ground for stuff that doesn't really belong anywhere else)
262
263 % \pgfmathanglebetweenpoints
264 %
265 % Define \pgfmathresult as the angle between points #1 and #2
266 % Should get the quadrants right as well.
267 %
268 \def\pgfmathanglebetweenpoints#1#2{%
269 \begingroup%
270 \pgf@process{\pgfpointdiff{#1}{#2}}%
271 \edef\pgf@marshall{\expandafter\noexpand\csname pgfmathatan2@\endcsname{\expandafter\Pgf@geT\the\pgf@y}{\expandafter\Pgf@geT\the\pgf@x}}%
272 \pgf@marshall%
273 \ifdim\pgfmathresult pt<0pt%
274 \pgfmathparse{\pgfmathresult+360}%
275 \fi%
276 \expandafter%
277 \endgroup\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult}%
278 }
279
280 %
281 % \pgfmathrotatepointaround
282 %
283 % Rotate point #1 about point #2 by #3 degrees.
284 %
285 \def\pgfmathrotatepointaround#1#2#3{%
286 \pgf@process{%
287 \pgf@process{#1}%
288 \pgf@xc=\pgf@x%
289 \pgf@yc=\pgf@y%
290 \pgf@process{#2}%
291 \pgf@xa\pgf@x%
292 \pgf@ya\pgf@y%
293 \pgf@xb\pgf@x%
294 \pgf@yb\pgf@y%
295 \pgf@x=\pgf@xc%
296 \pgf@y=\pgf@yc%
297 \advance\pgf@x-\pgf@xa%
298 \advance\pgf@y-\pgf@ya%
299 \pgfmathsetmacro\angle{#3}%
300 \pgfmathsin@{\angle}%
301 \let\sineangle\pgfmathresult%
302 \pgfmathcos@{\angle}%
303 \let\cosineangle\pgfmathresult%
304 \pgf@xa\cosineangle\pgf@x%
305 \advance\pgf@xa-\sineangle\pgf@y%
306 \pgf@ya\sineangle\pgf@x%
307 \advance\pgf@ya\cosineangle\pgf@y%
308 \pgf@x\pgf@xb%
309 \pgf@y\pgf@yb%
310 \advance\pgf@x\pgf@xa%
311 \advance\pgf@y\pgf@ya%
312 }%
313 }
314
315 % \pgfmathanglebetweenlines
316 %
317 % Calculate the clockwise angle between a line from point #1
318 % to point #2 and a line from #3 to point #4.
319 %
320 \def\pgfmathanglebetweenlines#1#2#3#4{%
321 \begingroup%
322 \pgfmathanglebetweenpoints{#1}{#2}%
323 \let\firstangle\pgfmathresult%
324 \pgfmathanglebetweenpoints{#3}{#4}%
325 \let\secondangle\pgfmathresult%
326 \ifdim\firstangle pt>\secondangle pt\relax%
327 \pgfmathadd@{\secondangle}{360}%
328 \let\secondangle\pgfmathresult%
329 \fi%
330 \pgfmathsubtract@{\secondangle}{\firstangle}%
331 \pgfmath@smuggleone\pgfmathresult%
332 \endgroup%
333 }
334
335 % \pgfmathpointreflectalongaxis
336 %
337 % Reflects point #1 around an axis centered on #2 at an angle #3.
338 %
339 \def\pgfmathreflectpointalongaxis#1#2#3{%
340 \pgf@process{%
341 \pgfmathanglebetweenpoints{#2}{#1}%
342 \pgfmath@tempdima\pgfmathresult pt\relax%
343 \pgfmathparse{#3}%
344 \advance\pgfmath@tempdima-\pgfmathresult pt\relax%
345 \[email protected]\pgfmath@tempdima%
346 \pgfmathrotatepointaround{#1}{#2}{\pgfmath@tonumber{\pgfmath@tempdima}}%
347 }%
348 }
349
350
351
352 % \pgfmathpointintersectionoflineandarc
353 %
354 % A bit experimental at the moment:
355 %
356 % Locates the point where a line crosses an elliptical arc. If the line
357 % does not cross the arc, a meaningless point will result.
358 %
359 % #1 the point of the line on the "convex" side of the arc.
360 % #2 the point of the line on the "concave" side of the arc.
361 % #3 the center of the elliptical arc.
362 % #4 start angle of the arc.
363 % #5 end angle of the arc.
364 % #6 radii of the arc.
365 %
366 \def\pgfmathpointintersectionoflineandarc#1#2#3#4#5#6{%
367 \pgf@process{%
368 %
369 % Get the required angle.
370 %
371 \pgfmathanglebetweenpoints{#2}{#1}%
372 \let\x\pgfmathresult%
373 %
374 % Get the radii of the arc.
375 %
376 \pgfmath@in@{and }{#6}%
377 \ifpgfmath@in@%
378 \pgf@polar@#6\@@%
379 \else%
380 \pgf@polar@#6 and #6\@@%
381 \fi%
382 \edef\xarcradius{\the\pgf@x}%
383 \edef\yarcradius{\the\pgf@y}%
384 %
385 % Get the start and end angles of the arc...
386 %
387 \pgfmathsetmacro\s{#4}%
388 \pgfmathsetmacro\e{#5}%
389 %
390 % ...and also with rounding.
391 %
392 \pgfmathmod@{\s}{360}%
393 \ifdim\pgfmathresult pt<0pt\relax%
394 \pgfmathadd@{\pgfmathresult}{360}%
395 \fi%
396 \let\ss\pgfmathresult%
397 \pgfmathmod@{\e}{360}%
398 \ifdim\pgfmathresult pt<0pt\relax%
399 \pgfmathadd@{\pgfmathresult}{360}%
400 \fi%
401 \let\ee\pgfmathresult%
402 %
403 % Hackery for when arc straddles zero.
404 %
405 \ifdim\ee pt<\ss pt\relax%
406 \pgfmathadd@{\x}{180}%
407 \pgfmathmod@{\pgfmathresult}{360}%
408 \let\x\pgfmathresult%
409 \fi%
410 \def\m{360}% Measure of nearness.
411 \pgfmathadd@{\s}{\e}%
412 \pgfmathdivide@{\pgfmathresult}{2}%
413 \let\n\pgfmathresult% The best estimate (default to middle of arc).
414 \pgfmathloop%
415 \pgfmathadd@{\s}{\e}%
416 \pgfmathdivide@{\pgfmathresult}{2}%
417 \let\p\pgfmathresult%
418 \ifdim\p pt=\s pt\relax%
419 \else%
420 \pgfmathanglebetweenpoints{#2}{%
421 \pgfpointadd{#3}{%
422 \pgf@x\xarcradius\relax%
423 \pgfmathcos@{\p}%
424 \pgf@x\pgfmathresult\pgf@x%
425 \pgf@y\yarcradius\relax%
426 \pgfmathsin@{\p}%
427 \pgf@y\pgfmathresult\pgf@y%
428 }%
429 }%
430 %
431 % Hackery for when arc straddles zero.
432 %
433 \ifdim\ee pt<\ss pt\relax%
434 \pgfmathadd@{\pgfmathresult}{180}%
435 \pgfmathmod@{\pgfmathresult}{360}%
436 \fi%
437 \let\q\pgfmathresult%
438 %
439 % More hackery...
440 %
441 \ifdim\x pt>335pt\relax%
442 \ifdim\q pt<45pt\relax%
443 \pgfmathadd@{\q}{360}%
444 \let\q\pgfmathresult%
445 \fi%
446 \fi%
447 \ifdim\x pt=\q pt% Found it!
448 \pgfmathbreakloop% Breaks after current iteration is complete.
449 \else
450 \ifdim\x pt<\q pt\relax%
451 \let\e\p%
452 \else%
453 \let\s\p%
454 \fi%
455 \fi%
456 \pgfmathsubtract@{\x}{\q}%
457 \pgfmathabs@{\pgfmathresult}%
458 %
459 % Save the estimate if it is better than any previous estimate.
460 %
461 \ifdim\pgfmathresult pt<\m pt\relax%
462 \let\m\pgfmathresult%
463 \let\n\p%
464 \fi%
465 \repeatpgfmathloop%
466 \pgfpointadd{#3}{\pgfpointpolar{\n}{\xarcradius and \yarcradius}}%
467 }%
468 }
469
470 % \pgfmathangleonellipse
471 %
472 % Find the angle corresponding to a point on the border of an ellispe.
473 %
474 % #1 - the point on the border.
475 % #2 - the radii of the ellipse.
476 %
477 \def\pgfmathangleonellipse#1#2{%
478 \begingroup%
479 \pgfmath@in@{and }{#2}%
480 \ifpgfmath@in@%
481 \pgf@polar@#2\@@%
482 \else%
483 \pgf@polar@#2 and #2\@@%
484 \fi%
485 \pgf@xa\pgf@x%
486 \pgf@ya\pgf@y%
487 \pgf@process{#1}%
488 \ifdim\pgf@x=0pt\relax%
489 \pgfutil@tempdima1pt\relax%
490 \else%
491 \pgfutil@tempdima\pgf@x%
492 \pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgfutil@tempdima}}%
493 \pgfutil@tempdima\pgfmathresult pt\relax%
494 \fi%
495 \ifdim\pgf@y=0pt\relax%
496 \pgfutil@tempdima1pt\relax%
497 \else%
498 \pgfmathdivide@{\pgfmath@tonumber{\pgf@y}}{\pgfmath@tonumber{\pgf@ya}}%
499 \pgfutil@tempdima\pgfmathresult\pgfutil@tempdima%
500 \pgfmathatan@{\pgfmath@tonumber{\pgfutil@tempdima}}%
501 \fi%
502 %
503 \pgfutil@tempdima\pgfmathresult pt\relax%
504 \ifdim\pgfutil@tempdima<0pt\relax%
505 \advance\pgfutil@tempdima360pt\relax%
506 \fi%
507 \ifdim\pgf@x<0pt\relax%
508 \ifdim\pgf@y=0pt\relax%
509 \pgfutil@tempdima180pt\relax%
510 \else%
511 \ifdim\pgf@y<0pt\relax%
512 \advance\pgfutil@tempdima180pt\relax%
513 \else%
514 \advance\pgfutil@tempdima-180pt\relax%
515 \fi%
516 \fi%
517 \else%
518 \ifdim\pgf@x=0pt\relax%
519 \ifdim\pgf@y<0pt\relax%
520 \pgfutil@tempdima270pt\relax%
521 \else%
522 \pgfutil@tempdima90pt\relax%
523 \fi%
524 \else%
525 \ifdim\pgf@y=0pt\relax%
526 \pgfutil@tempdima0pt\relax%
527 \fi%
528 \fi%
529 \fi%
530 \pgfmath@returnone\pgfutil@tempdima%
531 \endgroup%
532 }
533
534
535 % Local Variables:
536 % tab-width: 2
537 % End: