TikZ grid inside a circle

Use the TikZ macro \clip to 'cut' out everything outside of a given path, which is the circle here, later on, draw the grid as 'usual' with draw ... grid ...

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\clip[draw] circle (5cm);
\draw (-5,-5) grid (5,5);
\end{tikzpicture}

\end{document}

enter image description here


How to do this with MetaPost, for whom it may interest. The clip <picture> to <path> primitive command is the key here.

\documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
    path circle; circle = fullcircle scaled 6cm; u = cm; N := 4;
    picture grid;
    beginfig(1);
        grid = image(%
            for i = -N upto N:
                draw ((i, -N) -- (i, N)) scaled u;
            endfor;
            for j = -N upto N:
                draw ((-N, j) -- (N, j)) scaled u;
            endfor;);
        clip grid to circle; draw grid; draw circle;
    endfig;
\end{mplibcode}
\end{document}

enter image description here

Tags:

Tikz Pgf

Grids