10

I'm trying to draw some lines within a circle of arbitrary size. I want to start with one vertical line through the centre of the circle and then add further lines like so:

enter image description here

I want to be able to control the spacing between the lines, L, and the number of lines plotted out from the centre (in the example there are two on either side of the centre line). The red markings are just for a guide I don't need that in the final version.

What is the best way to code this using TikZ?

MWE

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle (1cm); % The choice of 1cm was arbitrary, what is the best way to scale this diagram?
\draw (0,-1) -- (0,1);
\draw (3,0) circle (1cm);
\draw (3,-1) -- (3,1);
\end{tikzpicture}
\end{document}
1
  • 1
    The best way is to calculate the endpoints using pgfmath: y=sqrt{r*r-x*x). May 14, 2018 at 13:17

3 Answers 3

15

Using clipping? I am using a scope here to restrict the clipping to it.

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \begin{scope} 
        \draw  [clip] (0,0) circle (1cm);
        \draw (0,-1) -- (0,1); % first (center) line 
            \foreach \x in {1,...,4} {% 4 lines per side 
                \draw (\x/10, -1) -- (\x/10,1); % positive x position 0.1, 0.2...
                \draw (-\x/10, -1) -- (-\x/10,1); % and negative ones 
        }
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

(Notice that the seemingly bigger line at the center is an aliasing problem of the PDF viewer).

Using scopes let you manipulate the thing quite easily:

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \begin{scope}
        \draw  [clip] (0,0) circle (1cm);
        \draw (0,-1) -- (0,1); % first diagonal
            \foreach \x in {1,...,4} {% 4 lines 
                \draw (\x/10, -1) -- (\x/10,1);
                \draw (-\x/10, -1) -- (-\x/10,1);
        }
    \end{scope}
    \begin{scope}[xshift=2cm, scale=0.5, rotate=45]
        \draw  [clip] (0,0) circle (1cm);
        \draw (0,-1) -- (0,1); % first diagonal
            \foreach \x in {1,...,4} {% 4 lines 
                \draw (\x/10, -1) -- (\x/10,1);
                \draw (-\x/10, -1) -- (-\x/10,1);
        }
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

14

For fun, here is a style vlines using path picture and grid to draw the lines.

\documentclass[tikz,border=7pt]{standalone}
\tikzset{
  vlines/.style={
    path picture={
      \draw[xstep=#1, ystep=100cm, shift={(path picture bounding box.south west)} ]
      (path picture bounding box.south west) grid (path picture bounding box.north east);
    }
  }
}
\begin{document}
  \begin{tikzpicture}
    \draw[vlines=2mm] (0,0) circle (1);
  \end{tikzpicture}
\end{document}

enter image description here

Note : The ystep=100cm can be set to the height of path picture bounding box but this is another story ;)

1

With the neweast version of TikZ/PGF, the best way should be grid[ystep=0,xstep=\L] after clip inside the unit circle. Note that the grid always go through the center. You have full control via \L for horizontal spacing between lines (hence, number of lines) and [scale=2] for scalethe whole picture.

enter image description here

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=2] 
\def\L{1.5mm}
\draw[clip] (0,0) circle(1);
\draw (-1,-1) grid[ystep=0,xstep=\L] (1,1);
\draw[red] (0,-1)--(0,1); % for checking grid go through the center, should be removed
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.