I am using TikZ spy
library to magnify part of my plot, and I would like to connect the spy point and the magnifying glass with two tangents to both circles (see second example). I found and implemented an algorithm to compute such lines (or, better, the four interesting points in the two circles), but I cannot understand how to pass to it the coordinates and radii of spy
's circles. It seems to me that there is a mismatch between numbers, point and centimeters.
Is there a way to get what I described?
\documentclass[crop,tikz,margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{spy}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
spy using outlines = {circle,size=3cm,magnification=5,connect spies},
]
\begin{axis}
\addplot+[domain = 0:2*pi] expression {sin(deg(x))};
\coordinate (spy point) at (axis cs: 0, 0);
\coordinate (magnifying glass) at (rel axis cs: -0.4, 0.2);
\end{axis}
\spy on (spy point) in node at (magnifying glass);
\end{tikzpicture}
\begin{tikzpicture}
\def\radiusa{0.3}
\def\radiusb{3}
\def\xa{5}
\def\ya{3}
\def\xb{0}
\def\yb{0}
\coordinate (magnifying glass) at (\xa, \ya);
\coordinate (spy point) at (\xb, \yb);
\pgfmathsetmacro\xp{(\xb * \radiusa - \xa * \radiusb) / (\radiusa - \radiusb)}
\pgfmathsetmacro\yp{(\yb * \radiusa - \ya * \radiusb) / (\radiusa - \radiusb)}
\pgfmathsetmacro\distancea{sqrt((\xp - \xa) * (\xp - \xa) + (\yp - \ya) * (\yp - \ya) - \radiusa * \radiusa))}
\pgfmathsetmacro\distanceb{sqrt((\xp - \xb) * (\xp - \xb) + (\yp - \yb) * (\yp - \yb) - \radiusb * \radiusb))}
\pgfmathsetmacro\denoma{(\xp - \xa)*(\xp - \xa) + (\yp - \ya)*(\yp - \ya)}
\pgfmathsetmacro\denomb{(\xp - \xb)*(\xp - \xb) + (\yp - \yb)*(\yp - \yb)}
\pgfmathsetmacro\xc{(\radiusa * \radiusa * (\xp - \xa) + \radiusa * (\yp - \ya) * \distancea) / \denoma + \xa}
\pgfmathsetmacro\yc{(\radiusa * \radiusa * (\yp - \ya) - \radiusa * (\xp - \xa) * \distancea) / \denoma + \ya}
\pgfmathsetmacro\xe{(\radiusa * \radiusa * (\xp - \xa) - \radiusa * (\yp - \ya) * \distancea) / \denoma + \xa}
\pgfmathsetmacro\ye{(\radiusa * \radiusa * (\yp - \ya) + \radiusa * (\xp - \xa) * \distancea) / \denoma + \ya}
\pgfmathsetmacro\xd{(\radiusb * \radiusb * (\xp - \xb) + \radiusb * (\yp - \yb) * \distanceb) / \denomb + \xb}
\pgfmathsetmacro\yd{(\radiusb * \radiusb * (\yp - \yb) - \radiusb * (\xp - \xb) * \distanceb) / \denomb + \yb}
\pgfmathsetmacro\xf{(\radiusb * \radiusb * (\xp - \xb) - \radiusb * (\yp - \yb) * \distanceb) / \denomb + \xb}
\pgfmathsetmacro\yf{(\radiusb * \radiusb * (\yp - \yb) + \radiusb * (\xp - \xb) * \distanceb) / \denomb + \yb}
\draw (magnifying glass) circle(\radiusa);
\draw (spy point) circle(\radiusb);
% \draw (\xa, \ya) node[scale=3, green] {.};
% \draw (\xb, \yb) node[scale=3, green] {.};
% \draw (\xp, \yp) node[scale=3, blue] {.};
% \draw (\xc, \yc) node[scale=3, red] {.};
% \draw (\xd, \yd) node[scale=3, red] {.};
% \draw (\xe, \ye) node[scale=3, red] {.};
% \draw (\xf, \yf) node[scale=3, red] {.};
% \draw (\xa, \ya) -- (\xp, \yp);
% \draw (\xb, \yb) -- (\xp, \yp);
\draw (\xc, \yc) -- (\xd, \yd);
\draw (\xe, \ye) -- (\xf, \yf);
\end{tikzpicture}
\end{document}