Skip to content

Instantly share code, notes, and snippets.

@kpym
Created February 26, 2015 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpym/3265bc0bce45737149bf to your computer and use it in GitHub Desktop.
Save kpym/3265bc0bce45737149bf to your computer and use it in GitHub Desktop.
\documentclass[tikz]{standalone}
\usetikzlibrary{math,spy}
\tikzmath{
let \startcolor=blue; let \endcolor=red; % --- the start and end colors
function FordCircles(\a,\b,\n){
int \p, \q; % ------------------------------ p and q are integers
for \q in {1,...,\n}{ % -------------------- 0 < q <= n
int \mix; \mix = 100*(\q-1)/(\n-1); % ---- color mix parameter in [0,100]
for \p in {\a*\q,...,\b*\q}{ % ----------- a < p/q < b <=> [aq] < p < [bq]
if gcd(\p,\q) == 1 then { % ------------ if the fraction is irreducible
\f = \p/\q; % ------------------------ evaluate the tuch point f = p/q
\r = 1/(2*\q*\q); % ------------------ evaluate the radius r = 1/2q^2
{
\filldraw[ultra thin,fill=\startcolor!\mix!\endcolor,fill opacity=.77, text opacity=1]
(\f,\r) circle(\r) % --- and draw the Ford circle at (f,r)
node[white,scale=3*\r]{$\frac{\p}{\q}$};
};
};
};
};
};
}
\begin{document}
\begin{tikzpicture}[spy using outlines={rectangle, size=10mm, connect spies}]
\draw[help lines,step=2mm] (-1,-0.1) grid (4,1.1);
% Draw the Ford circles
\tikzmath{FordCircles(-.9,3.9,8);}
% Magnification on some regions
\spy[magnification=3,green]
on (0.41,0.1) in node [left] at (1,-.7);
\spy[magnification=7,green]
on (pi,0) in node [left] at (3,-.7);
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment