9

I need to draw a curve from a list of points whose coordinates have been calculated beforehand in a foreach loop

I manage to generate my points and to use them independently, I think to manage to create a list, in any case, I manage to display it by cons I can not use this list in the command of drawing

\draw plot coordinates {....};

if I copy the displayed list, and replace in the plot, I get the plot.

\documentclass[11pt]{standalone}
\usepackage{tikz}


\begin{document}

\begin{tikzpicture}[scale=0.2]
\def\RR{5}
%calcul des coordonnées des points
%calculation of the coordinates of the points
\foreach \ii in {0, 1,2,3,...,20}{
    \pgfmathsetmacro{\aa}{360 * \ii/20}
    \pgfmathsetmacro{\rr}{\RR + \ii}
    \coordinate(P-\ii) at (\aa:\rr);
}

%dessin de la spirale/drawing of the spiral
\foreach \ii in {1,2,3,...,20}{
\node[fill=black,inner sep=0,minimum size=0.2cm,circle]at (P-\ii){};
}

%Création de la liste point/ Creating the point list
\def\LL{
\foreach \ii in {1,2,3,...,20}{(P-\ii) }
}
%affichage de la liste / list view
\node{\LL};

% La commande ci dessous ne fonctionne pas, que dois-je modifier?
% The command below does not work, what should I change?

%\draw plot coordinates { \LL};

%par contre, en recopiant les données affichées on peut tracer la spirale
% on the other hand, by copying the displayed data we can draw the spiral
\draw[blue] plot coordinates { (P-1) (P-2) (P-3) (P-4) (P-5) (P-6) (P-7) (P-8) (P-9) (P-10) (P-11) (P-12) (P-13)};

\end{tikzpicture}

\end{document}

enter image description here

Remarqu: my curve is more complex and does not depend on a single foreach but several that I want to be able to concatenate in a list then draw the corresponding curve.

rpapa
  • 12,350

4 Answers4

11

If I understand what is required, then (somewhat surprisingly) \draw plot [...] (P-\x); works, provided you set the domain and samples key correctly. Then options like smooth can be used.

\documentclass[tikz,borde=5]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.2]
\def\R{5}
\foreach \i [evaluate={\a=18*\i; \r=\R+\i;}] in {0, 1,...,20}
  \fill (\a:\r) coordinate (P-\i) circle [radius=0.2]
    node [anchor=\a+180, font=\tiny] {$P_{\i}$};
\draw[blue] plot [domain=0:20, samples=21,smooth] (P-\x);
\end{tikzpicture}
\end{document} 

Alternatively, the coordinates can be stored in a macro (this has the advantage any coordinate format could then be used):

\documentclass[tikz,borde=5]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.2]
\def\R{5}
\foreach \i [evaluate={\a=18*\i; \r=\R+\i;}] in {0, 1,...,20}
  \fill (\a:\r) coordinate (P-\i) circle [radius=0.2]
    node [anchor=\a+180, font=\tiny] {$P_{\i}$}; 
\def\coords{}
\foreach \i in {0,...,20}{\xdef\coords{\coords(P-\i)}}
\draw[blue] plot [smooth] coordinates \coords;
\end{tikzpicture}
\end{document} 

Both methods produce the same result:

enter image description here

Mark Wibrow
  • 70,437
7

You could define your list as expanded macro, for example by using:

\def\LL{}
\foreach \ii in {1,2,3,...,20}{
  \xdef\LL{\LL (P-\ii)}
}

and then it works

\documentclass[tikz,border=7pt]{standalone}

\begin{document}
  \begin{tikzpicture}[scale=0.2]
    \def\RR{5}
    %calcul des coordonnées des points
    %calculation of the coordinates of the points
    \foreach \ii in {0, 1,2,3,...,20}{
        \pgfmathsetmacro{\aa}{360 * \ii/20}
        \pgfmathsetmacro{\rr}{\RR + \ii}
        \coordinate(P-\ii) at (\aa:\rr);
    }
    %dessin des points de la spirale/drawing spiral points
    \foreach \ii in {1,2,3,...,20}{
      \node[fill=black,inner sep=0,minimum size=0.2cm,circle]at (P-\ii){};
    }
    %Création de la liste point/ Creating the point list
    \def\LL{}
    \foreach \ii in {1,2,3,...,20}{
      \xdef\LL{\LL (P-\ii)}
    }
    %affichage de la liste / list view
    \node{\LL};
    % La commande ci-dessous fonctionne.
    % The command below works.
    \draw[blue] plot coordinates { \LL};
  \end{tikzpicture}
\end{document}

enter image description here

Kpym
  • 23,002
  • thank you that it answers this first problem, I will see if it works by concatenating 2 lists, I validate your answer and I will rest another if necessary – rpapa May 17 '18 at 09:37
  • No problem to concatenate two lists : simply for the second one do not use \def\LL{} at the begining of the loop. – Kpym May 17 '18 at 09:39
  • in fact I want to be able to trace a gear wheel (the one of the answerwheelhttps://tex.stackexchange.com/questions/58702/creating-gears-in-tikz/58828#58828 ) in order to fill and colored the – rpapa May 17 '18 at 09:52
  • @rpapa in this case probably you could do better than to name all coordinates. You can use pic, you can save the path and then reuse it, ... probably you should ask another question with what you precisely want to do. – Kpym May 17 '18 at 11:41
  • merci, I do not know well pic, I'll see what I can do – rpapa May 17 '18 at 11:46
4

You can use another \foreach loop to draw the curve.

\documentclass[11pt]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=0.2]
    \def\RR{5}
    %calcul des coordonnées des points
    %calculation of the coordinates of the points
    \foreach \ii in {0, 1,2,3,...,20}{
        \pgfmathsetmacro{\aa}{360 * \ii/20}
        \pgfmathsetmacro{\rr}{\RR + \ii}
        \coordinate(P-\ii) at (\aa:\rr);
    }

    %dessin de la spirale/drawing of the spiral
    \foreach \ii in {1,2,3,...,20}{
    \node[fill=black,inner sep=0,minimum size=0.2cm,circle]at (P-\ii){};
    }

    %par contre, en recopiant les données affichées on peut tracer la spirale
    % on the other hand, by copying the displayed data we can draw the spiral
    \foreach \ii in {1,2,...,19}{
        \draw[blue] (P-\ii) -- (P-\the\numexpr\ii+1\relax);
    }
\end{tikzpicture}

\end{document}

Result:

enter image description here

dexteritas
  • 9,161
  • my curve is more complex and does not depend on a single foreach but several that I want to be able to concatenate in a list then draw the corresponding curve. – rpapa May 17 '18 at 09:14
3

With PSTricks, only for fun.

Method A

\documentclass[pstricks,border=1cm]{standalone}
\usepackage{multido}
\degrees[20]
\begin{document}
\begin{pspicture}[showgrid](-5,-5)(5,5)
\pscustom{\moveto(1,0)\multido{\i=1+1}{20}{\lineto(!\i\space dup 5 add 5 div exch PtoCrel)}}
\end{pspicture}
\end{document}

Method B

\documentclass[pstricks,border=1cm]{standalone}
\usepackage{multido}
\degrees[20]
\begin{document}
\begin{pspicture}[showgrid](-5,-5)(5,5)
\def\pts{(1,0)}%
\multido{\i=1+1}{20}{\xdef\pts{\pts(!\i\space dup 5 add 5 div exch PtoCrel)}}
\expandafter\psline\pts
\end{pspicture}
\end{document}

Method C

\documentclass[pstricks,border=1cm]{standalone}
\usepackage{pst-plot}
\degrees[20]
\begin{document}
\begin{pspicture}[showgrid](-5,-5)(5,5)
\curvepnodes[plotpoints=21]{0}{20}{t dup 5 add 5 div exch PtoCrel}{P}
\psnline(0,\Pnodecount){P} 
\end{pspicture}
\end{document}

Method D

\documentclass[pstricks,border=1cm]{standalone}
\usepackage{pst-plot}
\degrees[20]
\begin{document}
\begin{pspicture}[showgrid](-5,-5)(5,5)
\psparametricplot[plotpoints=21]{0}{20}{t dup 5 add 5 div exch PtoCrel}
\end{pspicture}
\end{document}

Output

enter image description here

Display Name
  • 46,933