% This is the TikZ library positioning-plus
% Load with \usetikzlibrary{positioning-plus}
%
% This small library extends TikZ options like 'above', 'left' or 'below right'
% so that they can be used with an optional prefixed factor seperated by ':' (colon)
%
% The option 'left=.5:of somenode' will place
% a new node .5cm (default 'node distance' is '1cm and 1cm') left to (somenode).
% The option 'above right=.2 and .7:of someothernode' will place
% a new node .2cm above and .7cm right of (someothernode).
%
% Additional the options 'xshift*' and 'yshift*' add an additional shift
% as a factor of 'node distance'
% Inspired by http://tex.stackexchange.com/a/117610/16595


\usetikzlibrary{positioning}
\usetikzlibrary{fit}


\pgfdeclaregenericanchor{corner south east}{%
  \pgf@sh@reanchor{#1}{south}%
  \pgf@ya\pgf@y
  \pgf@process{\pgf@sh@reanchor{#1}{east}}%
  \pgf@y\pgf@ya
}
\pgfdeclaregenericanchor{corner north east}{%
  \pgf@sh@reanchor{#1}{north}%
  \pgf@ya\pgf@y
  \pgf@process{\pgf@sh@reanchor{#1}{east}}%
  \pgf@y\pgf@ya
}
\pgfdeclaregenericanchor{corner south west}{%
  \pgf@sh@reanchor{#1}{south}%
  \pgf@ya\pgf@y
  \pgf@process{\pgf@sh@reanchor{#1}{west}}%
  \pgf@y\pgf@ya
}
\pgfdeclaregenericanchor{corner north west}{%
  \pgf@sh@reanchor{#1}{north}%
  \pgf@ya\pgf@y
  \pgf@process{\pgf@sh@reanchor{#1}{west}}%
  \pgf@y\pgf@ya
}


\tikzset{corner above left/.code=\tikz@lib@place@handle@{#1}{corner south east}{-1}{1}{corner north west}{0.707106781}}
\tikzset{corner above right/.code=\tikz@lib@place@handle@{#1}{corner south west}{1}{1}{corner north east}{0.707106781}}
\tikzset{corner below left/.code=\tikz@lib@place@handle@{#1}{corner north east}{-1}{-1}{corner south west}{0.707106781}}
\tikzset{corner below right/.code=\tikz@lib@place@handle@{#1}{corner north west}{1}{-1}{corner south east}{0.707106781}}


\tikzset{corner north left/.code =\tikz@lib@place@handle@{#1}{corner north east}{-1}{0}{corner north west}{1}}
\tikzset{corner north right/.code=\tikz@lib@place@handle@{#1}{corner north west}{1}{0}{corner north east}{1}}
\tikzset{corner south left/.code =\tikz@lib@place@handle@{#1}{corner south east}{-1}{0}{corner south west}{1}}
\tikzset{corner south right/.code=\tikz@lib@place@handle@{#1}{corner south west}{1}{0}{corner south east}{1}}


\tikzset{corner west above/.code =\tikz@lib@place@handle@{#1}{corner south west}{0}{1}{corner north west}{1}}
\tikzset{corner west below/.code=\tikz@lib@place@handle@{#1}{corner north west}{0}{-1}{corner south west}{1}}
\tikzset{corner east above/.code =\tikz@lib@place@handle@{#1}{corner south east}{0}{1}{corner north east}{1}}
\tikzset{corner east below/.code=\tikz@lib@place@handle@{#1}{corner north east}{0}{-1}{corner south east}{1}}


\tikzset{north left/.code =\tikz@lib@place@handle@{#1}{north east}{-1}{0}{north west}{1}}
\tikzset{north right/.code=\tikz@lib@place@handle@{#1}{north west}{1}{0}{north east}{1}}
\tikzset{south left/.code =\tikz@lib@place@handle@{#1}{south east}{-1}{0}{south west}{1}}
\tikzset{south right/.code=\tikz@lib@place@handle@{#1}{south west}{1}{0}{south east}{1}}


\tikzset{west above/.code =\tikz@lib@place@handle@{#1}{south west}{0}{1}{north west}{1}}
\tikzset{west below/.code=\tikz@lib@place@handle@{#1}{north west}{0}{-1}{south west}{1}}
\tikzset{east above/.code =\tikz@lib@place@handle@{#1}{south east}{0}{1}{north east}{1}}
\tikzset{east below/.code=\tikz@lib@place@handle@{#1}{north east}{0}{-1}{south east}{1}}


\def\tikz@lib@place@handle@#1#2#3#4#5#6{%
  \pgfutil@in@{:}{#1}%
  \ifpgfutil@in@
    \tikz@lib@place@handle@qrr@#1\tikz@stop
    \let\tikz@lib@temp\pgf@tempa
    \pgfmathsetmacro\pgf@tempa{(\pgf@tempa)*#4}%
    \pgfmathsetmacro\pgf@tempb{(\pgf@tempb)*#3}%
    \edef\pgf@marshal{\noexpand\tikz@lib@place@handle@{\pgf@temp}{#2}{\pgf@tempb}{\pgf@tempa}{#5}{#6}}%
    \pgf@marshal
  \else
    \def\tikz@anchor{#2}%
    \let\tikz@do@auto@anchor=\relax%
    \edef\tikz@temp{#1}%
    \def\tikz@lib@place@single@factor{#6}%
    \expandafter\tikz@lib@place@handle@@\expandafter{\tikz@temp}{#3}{#4}{#5}%
  \fi
}
\def\tikz@lib@place@handle@qrr@#1:#2\tikz@stop{%
  \pgfutil@in@{and}{#1}%
  \ifpgfutil@in@
    \tikz@lib@place@handle@qrr@@#1\tikz@stop
  \else
    \tikz@lib@place@handle@qrr@@#1and#1\tikz@stop
  \fi
  \def\pgf@temp{#2}%
}
\def\tikz@lib@place@handle@qrr@@#1and#2\tikz@stop{%
  \def\pgf@tempa{#1}%
  \def\pgf@tempb{#2}%
}
\def\qrr@xyshift@starred#1#2#3{
  \edef\pgf@temp{\noexpand\pgfutil@in@{and}{\tikz@node@distance}}%
  \pgf@temp
  \ifpgfutil@in@
    \expandafter\tikz@lib@place@handle@qrr@@\tikz@node@distance\tikz@stop
  \else
    \let#3\tikz@node@distance
  \fi
  \pgfmathparse{#3}%
  \ifpgfmathunitsdeclared
    \pgf@xa=\pgfmathresult pt%
  \else
    \let\tikz@lib@temp=\pgfmathresult
    \ifx#2x
      \pgf@process{\pgfpointxy{\tikz@lib@temp}{0}}%
      \pgf@xa=\pgf@x
    \else
      \pgf@process{\pgfpointxy{0}{\tikz@lib@temp}}%
      \pgf@xa=\pgf@y
    \fi
  \fi
  \pgfkeys{/tikz/#2shift/.expanded=(#1)*\the\pgf@xa}
}
\tikzset{
  xshift*/.code=\qrr@xyshift@starred{#1}x\pgf@tempb,
  xshift*/.default=1,
  yshift*/.code=\qrr@xyshift@starred{#1}y\pgf@tempa,
  yshift*/.default=1
}


\def\pgfutil@firstofmany#1#2\pgf@stop{#1}
\def\pgfutil@secondofmany#1#2\pgf@stop{#2}
\def\tikz@lib@place@of@#1#2#3{%
  \def\pgf@tempa{fit bounding box}%
  \edef\pgf@temp{\expandafter\pgfutil@firstofmany#2\pgf@stop}
  \if\pgf@temp(%
    \tikz@lib@place@fit@scan{#2}{0}%
  \else\if\pgf@temp|
      \expandafter\tikz@lib@place@fit@scan\expandafter{\pgfutil@secondofmany#2\pgf@stop}{1}%
    \else\ifx\pgf@temp\tikz@activebar
        \expandafter\tikz@lib@place@fit@scan\expandafter{\pgfutil@secondofmany#2\pgf@stop}{1}%
      \else\if\pgf@temp-
          \expandafter\tikz@lib@place@fit@scan\expandafter{\pgfutil@secondofmany#2\pgf@stop}{2}%
        \else\if\pgf@temp+
            \expandafter\tikz@lib@place@fit@scan\expandafter{\pgfutil@secondofmany#2\pgf@stop}{3}%
          \else
            \def\pgf@tempa{#2}%
          \fi
        \fi
      \fi
    \fi
  \fi
  \expandafter\tikz@scan@one@point\expandafter\tikz@lib@place@remember\expandafter(\pgf@tempa)%
  \iftikz@shapeborder%
    % Ok, this is relative to a border.
    \iftikz@lib@ignore@size%
      \edef\tikz@node@at{\noexpand\pgfpointanchor{\tikz@shapeborder@name}{center}}%
      \def\tikz@anchor{center}%
    \else%
      \edef\tikz@node@at{\noexpand\pgfpointanchor{\tikz@shapeborder@name}{#3}}%
    \fi%
  \fi%
  \edef\tikz@lib@place@nums{#1}%
}
\def\tikz@lib@place@fit@scan#1#2{
  \pgf@xb=-16000pt\relax%
  \pgf@xa=16000pt\relax%
  \pgf@yb=-16000pt\relax%
  \pgf@ya=16000pt\relax%
  \if\pgfutil@firstofmany#1\pgf@stop(%
    \tikz@lib@fit@scan#1\pgf@stop%
  \else
    \tikz@lib@fit@scan(#1)\pgf@stop
  \fi
  \ifdim\pgf@xa>\pgf@xa
    % shouldn't happen
  \else
     \expandafter\def\csname pgf@sh@ns@fit bounding box\endcsname{rectangle}%
     \expandafter\edef\csname pgf@sh@np@fit bounding box\endcsname{%
       \def\noexpand\southwest{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}%
       \def\noexpand\northeast{\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}%
     }%
     \expandafter\def\csname pgf@sh@nt@fit bounding box\endcsname{{1}{0}{0}{1}{0pt}{0pt}}%
     \expandafter\def\csname pgf@sh@pi@fit bounding box\endcsname{\pgfpictureid}%
     \ifcase#2\relax
     \or % 1 = vertical
       \pgf@y=\pgf@yb%
       \advance\pgf@y by-\pgf@ya%
       \edef\pgf@marshal{\noexpand\tikzset{minimum height={\the\pgf@y-2*(\noexpand\pgfkeysvalueof{/pgf/outer ysep})}}}%
       \pgf@marshal
     \or % 2 = horizontal
       \pgf@x=\pgf@xb%
       \advance\pgf@x by-\pgf@xa%
       \edef\pgf@marshal{\noexpand\tikzset{minimum width={\the\pgf@x-2*(\noexpand\pgfkeysvalueof{/pgf/outer xsep})}}}%
       \pgf@marshal
     \or % 3 = both directions
       \pgf@y=\pgf@yb%
       \advance\pgf@y by-\pgf@ya%
       \pgf@x=\pgf@xb%
       \advance\pgf@x by-\pgf@xa%
       \edef\pgf@marshal{\noexpand\tikzset{minimum height={\the\pgf@y-2*(\noexpand\pgfkeysvalueof{/pgf/outer ysep})},minimum width={\the\pgf@x-2*(\noexpand\pgfkeysvalueof{/pgf/outer xsep})}}}%
       \pgf@marshal
     \fi
  \fi
}
\tikzset{
  fit bounding box/.code={\tikz@lib@place@fit@scan{#1}{0}},
  span vertical/.code={\tikz@lib@place@fit@scan{#1}{1}},
  span horizontal/.code={\tikz@lib@place@fit@scan{#1}{2}},
  span/.code={\tikz@lib@place@fit@scan{#1}{3}}}


\pgfmathdeclarefunction{x_node_dist}{0}{%
  \begingroup
    \edef\pgfmath@temp{\noexpand\pgfutil@in@{and}{\tikz@node@distance}}%
    \pgfmath@temp\ifpgfutil@in@
      \expandafter\tikz@lib@place@handle@qrr@@\tikz@node@distance\tikz@stop
    \else
      \edef\pgfmath@temp{\tikz@node@distance and\tikz@node@distance}%
      \expandafter\tikz@lib@place@handle@qrr@@\pgfmath@temp\tikz@stop
    \fi
    \pgfmathparse{\pgf@tempb}%
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}
\pgfmathdeclarefunction{y_node_dist}{0}{%
  \begingroup
    \edef\pgfmath@temp{\noexpand\pgfutil@in@{and}{\tikz@node@distance}}%
    \pgfmath@temp\ifpgfutil@in@
      \expandafter\tikz@lib@place@handle@qrr@@\tikz@node@distance\tikz@stop
    \else
      \edef\pgfmath@temp{\tikz@node@distance and\tikz@node@distance}%
      \expandafter\tikz@lib@place@handle@qrr@@\pgfmath@temp\tikz@stop
    \fi
    \pgfmathparse{\pgf@tempa}%
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}
\endinput

