1

In tikz: drawing a parametrised curve I asked about drawing a curve using the \pgfplotfunction command.

I could make my code better if I could use the \foreach cycle:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[x=2cm/2]
  \newcommand\rad{1}
  \pgfplothandlerlineto
  \foreach \phiparam in {0, 1, ..., 360}
           {
             \pgfpointxy{\rad *cos(\phiparam)}{\rad *sin(\phiparam)}
             \pgfusepath{stroke}
           }
\end{tikzpicture}
\end{document}

How to use the \foreach cycle to calculate parameters, plot points and connect them to show a smooth curve?

Viesturs
  • 7,895

1 Answers1

1

foreach can be part of a path. You only have to unroll the 0-th iteration manually.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \pgfpathmoveto{\pgfpointxy{cos(0)}{sin(0)}}
  \foreach \phiparam in {1, ..., 360} {
    \pgfpathlineto{\pgfpointxy{cos(\phiparam)}{sin(\phiparam)}}
  }
  \pgfusepath{stroke}
\end{tikzpicture}

\end{document}

enter image description here

In this case plot might be just easier.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \pgfplothandlerlineto
  \pgfplotfunction{\x}{0,...,360}{\pgfpointxy{cos(\x)}{sin(\x)}}
  \pgfusepath{stroke}
\end{tikzpicture}

\end{document}
Henri Menke
  • 109,596
  • This is the approach I needed to circumvent. I am interested to write more complicated commands inside the scope of the cycle. – Viesturs Nov 05 '17 at 01:27
  • @Viesturs So you want to use the basic layer? It's a bit unclear to me. See updated answer. – Henri Menke Nov 05 '17 at 02:27
  • The first part of your answer is what I was looking for - a bare \foreach cycle where additional commands can be inserted. – Viesturs Nov 05 '17 at 09:58