14

I have a question, how can have the shortest set of tikz code for this picture? enter image description here

I am tring to optimize this code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
La version de TikZ est : \pgfversion

    \vfill  

\begin{center}
    \begin{tikzpicture}
    %
        \draw (0,1) -- (1,0);
    \draw (1,0) -- (0,-1);
    \draw (0,-1) -- (-1,0);
    \draw (-1,0) -- (0,1);
    \draw (0,0) circle (1);
    \draw (0,1) circle (1);
    \draw (0,-1) circle (1);
    \draw (-1,0) circle (1);
    \draw (1,0) circle (1);
    %
        \draw (0,2) -- (2,0);
        \draw (2,0) -- (0,-2);
        \draw (0,-2) -- (-2,0);
        \draw (-2,0) -- (0,2);
        \draw (0,0) circle (2);
        \draw (0,2) circle (2);
        \draw (0,-2) circle (2);
        \draw (-2,0) circle (2);
        \draw (2,0) circle (2);
    \end{tikzpicture}   
\end{center}
\end{document}
2
  • 3
    Do you mean who has the shortest execution time in processor time? Who requires the least memory?
    – AndréC
    Feb 8, 2019 at 15:42
  • 1
    Some think with a loop will be a great approach, but if we consider CPU and memory we must be more careful.
    – mou-nadal
    Feb 8, 2019 at 16:04

6 Answers 6

11

One path for everything. EDIT: Because AndréC accused me of copying from "his" answer, this the original answer:

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[rotate=45]  foreach \Y in {1,2} 
   {(0,0) circle[radius=\Y]
    ({-\Y/sqrt(2)},{-\Y/sqrt(2)}) rectangle ({\Y/sqrt(2)},{\Y/sqrt(2)})
   foreach \X in {45,135,225,315}
   { (\X:\Y) circle[radius=\Y]}};
\end{tikzpicture}
\end{document}

To put things into perspective:

  1. Paul Gaborit's answer is also one path. I should have mentioned this above.
  2. Paul Gaborit had the first answer here, which I upvoted, and which uses polar coordinates.
  3. As one can see, I used polar coordinates right from the start.
  4. The new features of this answer are two nested foreach loops and drawing the rotated rectangles as rotated rectangles.
  5. On the other hand AndréC's answer copies the two loops from mine. (I became only aware of "his" answer long after the following code was written.)

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw[rotate=45]  foreach \Y in {1,2} 
   {(0,0) circle[radius=\Y]
    (45:\Y) rectangle (225:\Y)
   foreach \X in {45,135,225,315}
   { (\X:\Y) circle[radius=\Y]}};
\end{tikzpicture}
\end{document}

enter image description here

1
  • Non-technical comments removed, as it were too many. Please use the chat for talk, if really needed.
    – Stefan Kottwitz
    Feb 9, 2019 at 11:18
10

Here is a solution (not the shortest):

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw
  circle (1) circle(2)
  (0:1) --(90:1) -- (180:1) -- (270:1) -- cycle
  (0:1) circle(1) (90:1) circle(1) (180:1) circle(1) (270:1) circle(1)
  (0:2) --(90:2) -- (180:2) -- (270:2) -- cycle
  (0:2) circle(2) (90:2) circle(2) (180:2) circle(2) (270:2) circle(2);
\end{tikzpicture}
\end{document}
9

Just for fun:

\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\begin{tikzpicture}

\draw (0,0) circle (1) circle (2);

\foreach \t/\n in {0/1,90/2,180/3,270/4}{
  \draw (\t:1)coordinate(\n) circle (1)  ;}

\foreach \t/\m in {0/5,90/6,180/7,270/8}{
  \draw (\t:2)coordinate(\m) circle (2) ;}

\draw (1)--(2)--(3)--(4)--cycle;
\draw (5)--(6)--(7)--(8)--cycle;  


\end{tikzpicture}
\end{document}
9

Borrowing a bit from everyone:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\tikz\draw\foreach~in{1,2}{[rotate=45,scale=~/sqrt 2](-1,-1)rectangle(1,1)
  \foreach~in{0,...,4}{(~*90-45:{(~>0)*sqrt 2})circle(sqrt 2)}};
\end{document}

enter image description here

4
  • 1
    -3 characters : \tikz\draw\foreach~in{1,2}{[rotate=45,scale=~/sqrt 2](-1,-1)rectangle(1,1)\foreach~in{0,...,4}{[scale=sqrt 2](~*90-45:~>0)circle()}}; ;)
    – Kpym
    Mar 27, 2019 at 18:01
  • 1
    @Kpym You can save another two characters by replacing \end{document} by \enddocument.
    – user121799
    Mar 27, 2019 at 18:30
  • 1
    -41 characters \tikz\draw\foreach~in{1,2}{[scale=~](0,0)circle()(0:1)foreach~in{1,...,4}{--(~*90:1)circle()}};.
    – Kpym
    Mar 27, 2019 at 18:31
  • @marmot I'm golfing only on the tikz green ;)
    – Kpym
    Mar 27, 2019 at 18:33
6

Not as much as @marmot's nicely optimised answer but a try (gives more knobs for fun though):

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[mystyle/.style={circle,draw,fill=none,minimum size=20, line width = 0.1pt}]
\foreach \x/\y/\z in {0/0/2,0/1/2,0/-1/2,-1/0/2,1/0/2,0/0/4,0/1/4,0/-1/4,-1/0/4,1/0/4}
\node [mystyle,  minimum size = \z cm, color =black]  (2) at (\x, \y) {};
\foreach \x/\y in {-0.7/0.7, -1.41/1.41}
\draw[rotate=45, line width = 0.1pt] (\x , \x ) rectangle (\y, \y);
\end{tikzpicture}
\end{document}

which gives

enter image description here

4

I am participating in the celebration with two proposals, both of which have been made:

Two loops:

  • the first one to make an enlargement (scale) by two
  • the second to build the 4 circles and the square using polar coordinates

The code is indented so that it can be read:

First proposal (easier to read):

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
La version de TikZ est : \pgfversion

    \vfill  

\begin{center}
\begin{tikzpicture}
\foreach  \s in {1,2}{
        \begin{scope}[scale=\s]
            \draw (0,0) circle (1);
            \foreach \r [remember= \r as \rr (initially 270)] in {0,90,180,270}{
                \draw (\r:1) circle (1);
                \draw (\rr:1)--(\r:1);
                }
        \end{scope}
        }
\end{tikzpicture}   
\end{center}
\end{document}

screenshot

Second proposal (without scope environment):

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
La version de TikZ est : \pgfversion

    \vfill  

\begin{center}
\begin{tikzpicture}
\foreach  \s in {1,2}{
            \draw (0,0) circle (\s);
            \foreach \r [remember= \r as \rr (initially 270)] in {0,90,180,270}{
                \draw (\r:\s) circle (\s);
                \draw (\rr:\s)--(\r:\s);
                }
        }
\end{tikzpicture}   
\end{center}
\end{document}
5
  • Non-technical comments removed, as it were too many. Please use the chat for talk, if really needed.
    – Stefan Kottwitz
    Feb 9, 2019 at 11:19
  • Think about an enlargement (scale) by two minus one circle (incriminating from one of the center of the larger circles)
    – mou-nadal
    Feb 9, 2019 at 20:02
  • @mou-nadal I didn't quite understand what you're trying to tell me. The enlargement (homothety) has by default the origin (0,0) for center.
    – AndréC
    Feb 9, 2019 at 20:30
  • yes, you r right .
    – mou-nadal
    Feb 9, 2019 at 20:46
  • Voting down deserves an explanation. Without explanation this has no meaning other than gratuitous hostility.
    – AndréC
    Jul 31, 2019 at 6:18

You must log in to answer this question.

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