summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryintersections.code.tex
blob: f3e4f94144b25bf80513c1acedefcdff8864aaa4 (plain)
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
% Copyright 2008 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\usepgflibrary{intersections}%

% FIXME : the 'name path global' is not reset properly - and some
% following 'name path' will append to it rather that reset it.

\pgfkeys{%
  /tikz/name path global/.code={%
    % reset any "old" variables inherited from some other path
    % - we do not accidentally want to append to an old path with the
    % same name (or if someone provided the option twice)...
    \expandafter\global\expandafter\let\csname tikz@intersect@path@name@#1\endcsname=\relax
    \tikz@key@name@path{#1}{\gdef}%
  },
  /tikz/name path local/.code={%
    \pgfkeys{/tikz/name path={#1}}%
  },
  /tikz/name path/.code={%
    % hm. Do we need this "reset old option" as in 'name path global'
    % for this case as well?
    \tikz@key@name@path{#1}{\def}%
  },
}%

% #1: the name to assign.
% #2: one of '\def' or '\gdef'.
\def\tikz@key@name@path#1#2{%
    \tikz@addmode{%
      \pgfsyssoftpath@getcurrentpath\tikz@intersect@temppath@round%
      \pgfprocessround\tikz@intersect@temppath@round\tikz@intersect@temppath%
      \ifx\tikz@intersect@namedpaths\pgfutil@empty%
      \else%
        \tikz@intersect@namedpaths%
      \fi%
      \tikz@intersect@addto@path@names{#1}{#2}%
    }%
}%

% #1: the name to assign.
% #2: one of '\def' or '\gdef'.
\def\tikz@intersect@addto@path@names#1#2{%
  \edef\tikz@marshal{#2\expandafter\noexpand\csname tikz@intersect@path@name@#1\endcsname}%
  \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@marshal%
    \expandafter\expandafter\expandafter{\expandafter\tikz@marshal\expandafter{\tikz@intersect@temppath}}%
  \expandafter\pgfutil@g@addto@macro\expandafter\tikz@intersect@namedpaths\expandafter{\tikz@marshal}%
}%


\let\tikz@finish@orig=\tikz@finish
\def\tikz@finish{%
  \tikz@finish@orig%
  \tikz@intersect@finish%
}%

\def\tikz@intersect@finish{%
  \ifx\tikz@intersect@namedpaths\pgfutil@empty%
  \else%
    \tikz@intersect@namedpaths%
    % FIXME : it is reasonable to reset this globally as it is global
    % in its nature. But the reset instruction should be moved to
    % \endscope or something related. Resetting it here breaks the
    % manual
    \let\tikz@intersect@namedpaths=\pgfutil@empty%
  \fi%
}%

\let\tikz@intersect@namedpaths=\pgfutil@empty
\let\tikz@intersect@path@a=\pgfutil@empty
\let\tikz@intersect@path@b=\pgfutil@empty
\let\tikz@intersect@name=\pgfutil@empty
\let\tikz@intersect@total=\pgfutil@empty

\tikzset{
  name intersections/.code={%
    \tikzset{/tikz/intersection/.cd,
      by=,
      #1}%
    \pgfutil@ifundefined{tikz@intersect@path@name@\tikz@intersect@path@a}{%
      \tikzerror{I do not know the path named `\tikz@intersect@path@a'. Perhaps you misspelt it}}%
    {\pgfutil@ifundefined{tikz@intersect@path@name@\tikz@intersect@path@b}{%
        \tikzerror{I do not know the path named `\tikz@intersect@path@b'. Perhaps you misspelt it}}%
      {%
        \pgfintersectionofpaths%
        {%
          \expandafter\pgfsetpath\csname tikz@intersect@path@name@\tikz@intersect@path@a\endcsname%
        }%
        {%
          \expandafter\pgfsetpath\csname tikz@intersect@path@name@\tikz@intersect@path@b\endcsname%
        }%
        \ifx\tikz@intersect@total\pgfutil@empty%
        \else%
          \expandafter\let\tikz@intersect@total=\pgfintersectionsolutions%
        \fi%
        \ifx\tikz@intersect@name\pgfutil@empty%
          \def\tikz@intersect@@name{intersection}%
        \else%
          \let\tikz@intersect@@name=\tikz@intersect@name%
        \fi%
        \pgfmathloop%
        \ifnum\pgfmathcounter>\pgfintersectionsolutions\relax%
        \else%
          \path[reset cm]\pgfextra{\pgftransformshift{\pgfpointintersectionsolution{\pgfmathcounter}}}%
            coordinate (\tikz@intersect@@name-\pgfmathcounter);
        \repeatpgfmathloop%
        \ifx\tikz@intersect@by\pgfutil@empty%
        \else%
          \foreach\tikz@intersect@name[count=\tikz@intersection@count] in \tikz@intersect@by%
          {\expandafter\tikz@intersect@name@parse\tikz@intersect@name\pgf@stop}%
        \fi%
      }%
    }%
  },%
  intersection/.cd,
  of/.code=\tikz@intersect@path@names@parse#1\tikz@stop,%
  name/.store in=\tikz@intersect@name,
  total/.store in=\tikz@intersect@total,
  by/.store in=\tikz@intersect@by,
  sort by/.code=\edef\tikz@intersect@sort@by{#1}\tikz@intersect@check@sort@by%
}%


\def\tikz@intersect@check@sort@by{%
    \ifx\tikz@intersect@sort@by\tikz@intersect@path@a%
        \pgfintersectionsortbyfirstpath%
    \else%
        \ifx\tikz@intersect@sort@by\tikz@intersect@path@b%
            \pgfintersectionsortbysecondpath%
        \else%
            \pgf@intersect@sortfalse%
        \fi%
    \fi%
}%
\def\tikz@intersect@path@names@parse#1 and #2\tikz@stop{%
  \def\tikz@intersect@path@a{#1}%
  \def\tikz@intersect@path@b{#2}%
}%

\def\tikz@intersect@name@parse{\pgfutil@ifnextchar[\tikz@intersect@name@parse@opt{\tikz@intersect@name@parse@opt[]}}%}
\def\tikz@intersect@name@parse@opt[#1]#2\pgf@stop{%
  \coordinate [#1] (#2) at (\tikz@intersect@@name-\tikz@intersection@count);
}%

\endinput