[Tex/LaTex] Tikz: Scale coordinates perhaps using the calc package

calculationstikz-pgf

I've drawn a rectangle like this:

\draw[color=orange, fill=orange, opacity=0.1] (Q3term1) rectangle (Q3term2);

and essentially, what I want is to do is stretch the rectangle by 10% horizontally and stretch is vertically by 5%, so something like:

\draw[color=orange, fill=orange, opacity=0.1] ($(1.1,1.05)*(Q3term1)$) rectangle ($(0.9,0.95)*(Q3term1)$);

If there is a way to do it without the calc library, that'd be good too.

Thanks.

Best Answer

If you don’t need to specify the corners of the rectangle relatively, you can use a rectangle node for this.

The \pgfpointlineattime{<factor>}{<p1>}{<p2>} macro is the low-level PGF version of ($(<p1>)!<factor>!(<p2>)$) which is also used in the placement of nodes along a straight line with the pos=<factor> option.

If you want to add a specific padding length, you can use the fit library with inner xsep and inner ysep.

The backgrounds library is only used to add a simply grid to the picture.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc,backgrounds,fit}
\makeatletter
\tikzset{
  from/.code args={#1 to #2}{%
    \pgfextract@process\tikz@fromto@first{\tikz@scan@one@point\pgfutil@firstofone#1\relax}%
    \pgfextract@process\tikz@fromto@second{\tikz@scan@one@point\pgfutil@firstofone#2\relax}%
    % The next line sets "at".
    \pgfextract@process\tikz@node@at
      {\pgfpointlineattime{.5}{\tikz@fromto@first}{\tikz@fromto@second}}%
    \pgfpointdiff{\tikz@fromto@first}{\tikz@fromto@second}%
    \tikzset{
      shape=rectangle,
      anchor=center,
      inner sep=+0pt,
      outer sep=+0pt,
      minimum width/.expanded={abs(\the\pgf@x)},
      minimum height/.expanded={abs(\the\pgf@y)}}%
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}[gridded,nodes={orange, draw, fill, fill opacity=.5}]
\path (1,1) coordinate (a)
      (3,4) coordinate (b)
      (4,1) coordinate (a')
      (6,4) coordinate (b');

\node[from=(a) to (b), xscale=1.1, yscale=1.05] {};
\node[fit=(a')(b'), inner xsep=.1cm, inner ysep=.07cm] {};
\end{tikzpicture}
\end{document}

Output

enter image description here

Related Question