% 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}}

\newif\iftikzext@tikz@lib@place@postransform
\tikzset{node distance transformation/.is if=tikzext@tikz@lib@place@postransform}
\def\tikz@lib@place@handle@@#1#2#3#4{%
  \pgfutil@in@{of }{#1}%
  \ifpgfutil@in@
    \tikz@lib@place@of#1\tikz@stop{#4}%
  \else
    \edef\tikz@lib@place@nums{#1}%
  \fi
  \ifx\tikz@lib@place@nums\pgfutil@empty
    % Ok, nothing to do, we have set the anchor and we are happy...
  \else
    \expandafter\tikz@lib@place@parse@nums\expandafter{\tikz@lib@place@nums}%
    \pgf@x=#2\pgf@x
    \pgf@y=#3\pgf@y
    \iftikzext@tikz@lib@place@postransform
      \pgf@pos@transform{\pgf@x}{\pgf@y}% change from original
    \fi
    \edef\tikz@lib@pos@call{\noexpand\pgftransformshift{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
  \fi
}

\def\tikz@lib@place@handle@#1#2#3#4#5#6{%
  \pgfutil@in@{:}{#1}%
  \ifpgfutil@in@
    \tikzext@lib@place@handle@#1\tikz@stop
    \ifx\pgf@temp\pgfutil@empty
      \let\pgf@temp\tikz@node@distance
    \fi
    \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\tikzext@lib@place@handle@#1:#2\tikz@stop{%
  \pgfutil@in@{and}{#1}%
  \ifpgfutil@in@
    \tikzext@lib@place@handle@@#1\tikz@stop
  \else
    \tikzext@lib@place@handle@@#1and#1\tikz@stop
  \fi
  \def\pgf@temp{#2}%
}
\def\tikzext@lib@place@handle@@#1and#2\tikz@stop{%
  \def\pgf@tempa{#1}%
  \def\pgf@tempb{#2}%
}
\def\tikzext@xyshift@starred#1#2#3{%
  \edef\tikz@marshal{\noexpand\pgfutil@in@{and}{\tikz@node@distance}}%
  \tikz@marshal
  \ifpgfutil@in@
    \expandafter\tikzext@lib@place@handle@@\tikz@node@distance\tikz@stop
  \else
    \let#3\tikz@node@distance
  \fi
  \pgfmathparse{(#1)*#3}%
  \ifpgfmathunitsdeclared
    \if#2x%
      \pgftransformshift{\pgfqpoint{\pgfmathresult pt}{0pt}}%
    \else
      \pgftransformshift{\pgfqpoint{0pt}{\pgfmathresult pt}}%
    \fi
  \else
    \ifx#2x%
      \pgf@process{\pgfqpointxy{\pgfmathresult}{0}}%
    \else
      \pgf@process{\pgfqpointxy{0}{\pgfmathresult}}%
    \fi
    \pgftransformshift{\pgfqpoint{\pgf@x}{\pgf@y}}%
  \fi}
\tikzset{
  xshift*/.code=\tikzext@xyshift@starred{#1}x\pgf@tempb,
  yshift*/.code=\tikzext@xyshift@starred{#1}y\pgf@tempa
}

\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\expandafter\endcsname\expandafter{\pgfpictureid}%
     \ifcase#2\relax
     \or % 1 = vertical
       \pgf@y=\pgf@yb%
       \advance\pgf@y by-\pgf@ya%
       \edef\pgf@marshal{\noexpand\pgfset{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\pgfset{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\pgfset{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\tikzext@lib@place@handle@@\tikz@node@distance\tikz@stop
    \else
      \let\pgf@tempb\tikz@node@distance
    \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\tikzext@lib@place@handle@@\tikz@node@distance\tikz@stop
    \else
      \let\pgf@tempa\tikz@node@distance
    \fi
    \pgfmathparse{\pgf@tempa}%
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}
%\tikzdeclarecoordinatesystem{node distance}{%
%  \tikz@cs@nd#1\tikz@stop}
%\def\tikz@cs@nd#1,#2\tikz@stop{%
%  \def\tikzext@xyshift@starred@action##1{\pgftransformshift{##1}}%
%  \tikzext@xyshift@starred{#1}x\pgf@tempb
%  \tikzext@xyshift@starred{#2}y\pgf@tempa}
\endinput
