2

In tkz-euclide there is a shortcut:

\tkzDrawPolygon(A1,A...,A13)

Is there something similar in plain tikz-pgf? I'm looking for a shortcut for the following:

\draw (A1) -- (A2) -- (A3) -- (A4) -- (A5) -- (A5) -- (A6) -- (A7) -- (A8) -- (A9) -- (A10) -- (A11) -- (A12) -- (A13) -- cycle;
  • 1
    Sure, \draw plot[samples at={1,...,13}] (A\x); or \draw plot[samples at={1,...,13}] (A\x) -- cycle;. –  Apr 28 '20 at 17:22

3 Answers3

9

Given a set of coordinates (A1), (A2), ... (A<n>), we can draw the polygon through them via

 \draw plot[samples at={1,...,<n>}] (A\x) -- cycle;

which is only slightly longer than the tkz-euclide syntax.

Since there is no MWE, I had to create some nodes.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
 \path foreach \X in {1,...,13} {(360*\X/14:2+rnd) coordinate (A\X)};
 \draw plot[samples at={1,...,13}] (A\x) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

One can, of course, also change the plot handler, e.g.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
 \path foreach \X in {1,...,13} {(360*\X/14:2+rnd) coordinate (A\X)};
 \draw plot[smooth cycle,samples at={1,...,13}] (A\x);
\end{tikzpicture}
\end{document}

enter image description here

As usual, you can define styles for this. Here is a style polygon through which produces the above polygon with

\draw[polygon through={A1,A...,A13}];

One can then use several of those in a path, e.g.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[polygon through/.style={insert path={
    plot[samples at={#1}] (\x) -- cycle}}]
 \path foreach \X in {1,...,13} {(360*\X/14:3+rnd) coordinate (A\X)
 (360*\X/14:1.5+rnd) coordinate (B\X)};
 \fill[even odd rule,blue,
    polygon through={A1,A...,A13},polygon through={B1,B...,B13}];
\end{tikzpicture}
\end{document}

enter image description here

  • "it is perhaps a bit easier to add the usual keys to \draw such as colors, decorate, save path and so on" ? Yes and No: Yes currently there's a slight problem with "decorate" but no with color, save path. With the next version, for this macro I was able to authorize the use of decorate. I'll try to generalize to the other macros. – Alain Matthes Apr 29 '20 at 05:09
  • @AlainMatthes Thanks for letting me know! Can you also just append stuff to the path in the macro, to get the same effect as e.g. \draw[fill=blue,even odd rule] plot[smooth cycle,samples at={1,...,13}] (A\x) (0,0) circle[radius=1cm];? (Thinking loudly, the answer is probably yes, at least insert path should work.) –  Apr 29 '20 at 05:14
5

Behind tkz-euclide there is TikZ. You can recreate the same macro

\def\tkzDrawPolygon[#1](#2,#3){%
 \begingroup
 \draw[#1] (#2)
     \foreach \pt in {#2,#3}{--(\pt)}--cycle;
 \endgroup
} 

\documentclass{standalone} 
\usepackage{tikz}
\begin{document} 

\begin{tikzpicture}
 \path foreach \X in {1,...,13} {(360*\X/14:2+rnd) coordinate (A\X)};
  \draw[thick,blue] (A1) foreach \pt in {A1,A...,A13} {--(\pt)} --cycle;
\end{tikzpicture}
\end{document}

enter image description here

Alain Matthes
  • 95,075
  • I have a suspicion why decorations may not work: in \def\tkzDrawPolygon[#1](#2,#3){% \begingroup \draw[#1] (#2) \foreach \pt in {#2,#3}{--(\pt)}--cycle; \endgroup } you plot the point #2 twice, so you have a segment of length 0, which decorations often do not like. –  Apr 29 '20 at 17:42
  • Yes you are right ! It’s why I used another définition of the macro ! – Alain Matthes Apr 29 '20 at 17:50
  • ;-) BTW, I found (more or less empirically) that many of the dimension too large errors in decorations come from the built in reciprocal. If you replace that by an fpu variant, say, these errors are gone, see e.g. here for a concrete example. Since you are improving several functions in tkz-euclide, one may think of providing the user with a switch that allows them to get rid of some of these unnecessary errors. –  Apr 29 '20 at 17:56
  • @Schrödinger'scat Very fine answer ! I like the polygon through/.style. Yes I agree with you about the use of flu. I'm already using it. – Alain Matthes Apr 29 '20 at 20:08
  • Yes, an ordinary flu is less dangerous than this virus... –  Apr 29 '20 at 20:12
  • @Schrödinger'scat Coincidence does the trick.:) – Alain Matthes Apr 29 '20 at 20:20
3

Probably less elegant than Schrödinger's cat's technique, here is another one based on expl3's \int_step_function:nnN (it is “restricted expandable,” which is enough here).

\documentclass[tikz, border=2mm]{standalone}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_eq:NN \intstepfunction \int_step_function:nnN
\ExplSyntaxOff

\begin{document}
\begin{tikzpicture}
  % Define some coordinates
  \foreach \i in {1,...,13} {
    \coordinate (A\i) at (360/13*\i:3);
  }

  % Use them in the prescribed way
  \def\tmp#1{-- (A#1)}
  \draw[blue] (A1) \intstepfunction{2}{13}{\tmp} --cycle;
\end{tikzpicture}
\end{document}

enter image description here

Of course, regular polygons can be drawn using the shapes.geometric TikZ library:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
\node[regular polygon, regular polygon sides=13, draw=blue,
      inner sep=3cm] at (0,0) {};
\end{tikzpicture}
\end{document}

enter image description here

frougon
  • 24,283
  • 1
  • 32
  • 55