[Tex/LaTex] Pixelized Circle in Tikz

tikz-pgf

I'm trying to generate something like this in TikZ:

Pixelated Minecraft Circle

Which can be generated for an arbitrary radius here, which uses this code.

I know how to do it manually in TikZ but it is a real pain. Since I saw in the manual that there is a way to do a for loop in TikZ, there must be a way to generate the pixelated(/minecraft) circle.

Any pointers on how to do this?

Best Answer

A very quick proposal.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
 \draw[gray](-5,-5) grid[step=0.25] (5,5);
 \foreach \X in {-4.875,-4.625,...,4.875}
 {\foreach \Y in {-4.875,-4.625,...,4.875}
 {\pgfmathtruncatemacro{\itest}{sqrt(abs(\X*\X+\Y*\Y-4*4))}
 \ifnum\itest=0
 \draw[gray,fill=red] (\X-0.125,\Y-0.125) rectangle (\X+0.125,\Y+0.125);
 \fi}}
\end{tikzpicture}
\end{document}

enter image description here

And something in which you can adjust the parameters. The fudge factor determines the "width" of the circle.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={radius=4.8;xmax=5;xstep=0.25;fudge=0.9;}]
 \draw[gray](-xmax,-xmax) grid[step=xstep] (xmax,xmax);
 \pgfmathsetmacro{\Xmax}{xmax-xstep/2}
 \pgfmathsetmacro{\Xnext}{xmax-3*xstep/2}
 \foreach \X in {-\Xmax,-\Xnext,...,\Xmax}
 {\foreach \Y in {-\Xmax,-\Xnext,...,\Xmax}
 {\pgfmathtruncatemacro{\itest}{fudge*sqrt(abs(\X*\X+\Y*\Y-radius*radius))}
 \ifnum\itest=0
 \draw[gray,fill=red] (\X-xstep/2,\Y-xstep/2) rectangle (\X+xstep/2,\Y+xstep/2);
 \fi}}
\end{tikzpicture}
\end{document}

enter image description here

And this is an attempt of the implementation of the prescription from the web site pointed out by tch.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[declare
function={radius=4.8;xmax=5;xstep=0.25;},pixel at/.style n
args={2}{insert path={({#1*xstep},{#2*xstep}) rectangle
++(xstep,xstep)}}]
 \draw[gray](-xmax,-xmax) grid[step=xstep] (xmax,xmax);
 \pgfmathtruncatemacro{\Ymax}{int(cos(45)*radius/xstep)}
 \foreach \Z in {0,90,180,270}
 {\foreach \Y in {-\Ymax,...,\the\numexpr\Ymax+1\relax}
 {\pgfmathtruncatemacro{\myx}{sqrt(radius*radius-\Y*\Y*xstep*xstep)/xstep}
 \draw[rotate around={\Z:(0,0)},gray,fill=red,pixel at={\myx}{\Y}];}}
\end{tikzpicture}
\end{document}

enter image description here