Search code examples
latextikzpgf

pgf/tikz: How to calculate half distance between the x coordinates?


I am trying to create a new node type like this:

http://www.live-wtr.ru/leo/Tikz9.png

but don't understand, how to calculate x3 = x1 + (x2 - x1) /2

Here I tried to do it, but in vain.

    \pgfdeclareshape{variant}{ \inheritsavedanchors[from=rectangle]
     % this is nearly a rectangle
     \inheritanchorborder[from=rectangle] \inheritanchor[from=rectangle]{center}
     \inheritanchor[from=rectangle]{north} \inheritanchor[from=rectangle]{south}
     \inheritanchor[from=rectangle]{west} \inheritanchor[from=rectangle]{east}

     \backgroundpath{
       %{{{ store lower left in xa/ya and upper right in xb/yb
       \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
       \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
   %}}}

       % compute x3 --- here is the problem!
       \pgf@yc = \pgf@xb - \pgf@xa
       \pgf@yc = 0.5\pgf@yc
       \pgf@xc = \pgf@xa
       \advance \pgf@xc by \pgf@yc

   %end of the problem

       \pgf@yc = \pgf@ya
       \advance\pgf@yc by - 10pt % this should be a parameter

       \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
       %{{{ construct 2 bottom lines
       \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
       \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
       \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
   %}}}

       % Rectangle box
       \pgfpathrectanglecorners{\southwest}{\northeast}

     }
   }

Solution

  • You can use the PGF math engine to do your calculation.

    % compute x3 --- here is the problem!
    
    \pgfmathparse{(\pgf@xb-\pgf@xa)/2}
    \pgf@xc=\pgf@xa
    \advance\pgf@xc by \pgfmathresult pt
    
    %end of the problem