How can the code be improved? Can I make the variables local?
\documentclass[12pt,tikz,border=5pt]{standalone}
\usetikzlibrary{calc}
% this is from
% http://tex.stackexchange.com/questions/66125/extract-x-value-from-coordinate-in-tikz
\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%
% Makro for rectangle specified by two focal points
% (like an ellipse)
\newcommand\rectfoca[4][rounded corners=2mm, ultra thick]{
% #1 optional argument
% default for #1:
% rounded corners=2mm, ultra thick
% gets overwritten by given argument
%
% #2 first focal point A: \coordinate A (x,y)
% #3 second focal point B: \coordinate B (x,y)
%
% #4 offset of the corners of the rectangle relative to A and B
% 1. Corner of rectangle (A - (#6, #6))
% 2. Corner of rectangle (B + (#6, #6))
%
% Getting Coordinates of A
\ExtractCoordinate{#2}
\pgfmathsetmacro\Acoordx{\XCoord/28.45274}
\pgfmathsetmacro\Acoordy{\YCoord/28.45274};
% Uncomment to print x, y coordinates of A
% \node (xx) at (5,8) {\pgfmathparse{\Acoordx}\pgfmathprintnumber[precision=2]{\pgfmathresult}};
% \node (yy) at (6,8) {\pgfmathparse{\Acoordy}\pgfmathprintnumber[precision=2]{\pgfmathresult}};
%
% Getting Coordinates of B
\ExtractCoordinate{#3}
\pgfmathsetmacro\Bcoordx{\XCoord/28.45274}
\pgfmathsetmacro\Bcoordy{\YCoord/28.45274};
% Uncomment to print x, y coordinates of B
% \node (xxa) at (5,7) {\pgfmathparse{\Bcoordx}\pgfmathprintnumber[precision=2]{\pgfmathresult}};
% \node (yya) at (6,7) {\pgfmathparse{\Bcoordy}\pgfmathprintnumber[precision=2]{\pgfmathresult}};
%
% Getting offset
\pgfmathsetmacro{\OffAB}{#4};
%
% Getting angle (uncomment next comment to print value)
\pgfmathsetmacro{\ABangle}{atan2(\Bcoordy-\Acoordy,\Bcoordx-\Acoordx)};
% \node (ang) at (6,6) {\pgfmathparse{\ABangle}\pgfmathprintnumber[precision=2]{\pgfmathresult}};
%
% Getting distance between A and B
\pgfmathsetmacro{\distAB}{veclen(\Bcoordx-\Acoordx,\Bcoordy-\Acoordy)};
%
% Getting the focus point BS for an angle = 0
\pgfmathsetmacro{\BScoordx}{\Acoordx + \distAB};
\pgfmathsetmacro{\BScoordy}{\Acoordy};
%
% Getting the corners of the horizontal (angle = 0) rectangle
\pgfmathsetmacro\AAcoordx{\Acoordx - \OffAB};
\pgfmathsetmacro\AAcoordy{\Acoordy - \OffAB};
\pgfmathsetmacro\BBcoordx{\BScoordx + \OffAB};
\pgfmathsetmacro\BBcoordy{\BScoordy + \OffAB};
%
% rotate the rectangle around A with the calculated angle
\draw[#1,rotate around={\ABangle:(\Acoordx,\Acoordy)}]
(\AAcoordx,\AAcoordy) rectangle (\BBcoordx,\BBcoordy);
}
\begin{document}
\begin{tikzpicture}
% grid to see the coordinates
\draw[help lines, step=1] (0,0) grid (10,8);
% Point A with circle and label
\coordinate [label={[label distance=3mm]below:A}] (A) at (1,3);
\draw (A) circle [radius=3pt];
% Point B with circle and label
\coordinate [label={[label distance=3mm]right:B}](B) at (6,6);
\draw (B) circle [radius=3pt];
% first rectangle with default options: rounded corners=2mm, ultra thick
% offset = 0.3
\rectfoca{A}{B}{0.3};
% second rectangle with specified options
% offset = 0.8
\rectfoca[red, rounded corners=1mm, thick]{A}{B}{0.8};
\end{tikzpicture}
\end{document}
Result:



