summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryintersections.code.tex
blob: dd842033fc88913b94ac187e55110ab99acea619 (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
152
153
154
155
156
% 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}

\let\tikz@intersect@def=\def

\pgfkeys{%
  /tikz/name path global/.code={%
    % it might have been more efficient to eliminate the storage macro
	% \tikz@intersect@namedpaths. But I keep it maintain compatibility
	% with mixed occurances of local/global names.
    \let\tikz@intersect@def=\gdef
    \pgfkeys{/tikz/name path={#1}}%
  },
  /tikz/name path local/.code={%
    \let\tikz@intersect@def=\def
    \pgfkeys{/tikz/name path={#1}}%
  },
  /tikz/name path/.code={%
    \tikz@addmode{%
      \pgfsyssoftpath@getcurrentpath\tikz@intersect@temppath%
      \ifx\tikz@intersect@namedpaths\pgfutil@empty%
      \else%
        \tikz@intersect@namedpaths%
        \pgfutil@ifundefined{tikz@intersect@path@name@#1}{}%
        {%
          \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@intersect@@temppath%
          \expandafter\expandafter\expandafter{\csname tikz@intersect@path@name@#1\endcsname}%
          \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@intersect@temppath%
          \expandafter\expandafter\expandafter{\expandafter\tikz@intersect@temppath\tikz@intersect@temppath}%
        }%
      \fi%
      \tikz@intersect@addto@path@names{#1}%
    }%
  }
}

\def\tikz@intersect@addto@path@names#1{%
  % FIXME : I have the impression that something is not correct
  % here... the \tikz@intersect@def is not used correctly! 
  % if I am not mistaken, the \tikz@intersect@def will NOT be
  % invoked until \tikz@intersect@namedpaths. CHECK IT
  % It seems it works, but only partially.
  \def\tikz@marshal{\expandafter\tikz@intersect@def\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}}%
  \ifx\tikz@intersect@def\gdef
  	% This here is to *ensure* that the \gdef is invoked. 
	%
	% I think this solves the problem.
	%
	% Please revise this routine such that it works better.
  	\tikz@marshal
  \fi
  \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%
		\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}{%
      \PackageError{tikz}{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}{%
        \PackageError{tikz}{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