12

any idea how I could draw an even grid inside a circle in TikZ? I could manually add invisible nodes on the circumferences and connect these with lines but there is probably a more sophisticated way to do this?

grid inside a circle

2 Answers 2

17

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

2
  • I managed to draw the four circles as in my example picture by separating each \clip and \draw combo with \begin{scope}.
    – trikki
    Commented Oct 31, 2017 at 13:46
  • @trikki: I didn't know you wanted to use all 4 circles. Of course, \begin{scope}...\end{scope} is a possible way then
    – user31729
    Commented Oct 31, 2017 at 22:45
3

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

2
  • In this case, you should explain the difference between the tikz and metapost clip. They have the same name but do not work at all the same. With tikz clip cut the canvas and the following constructions are therefore located in this cut. With Metapost, clip cuts out the current figure, i. e. constructions that have already been built. The following constructions are not cut out.
    – AndréC
    Commented Oct 31, 2017 at 9:11
  • 1
    @AndréC More precisely, the MetaPost clip command applies to the picture given in first argument. In this case, the grid alone. Only if this argument is currentpicture, it means all the already built constructions. I didn't know precisely how the tikz \clip command works, so your comment explains it much better than I would have done it. Commented Oct 31, 2017 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .