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?

  • Do you want to draw a part of an cycle? Maybe helpful: Draw arc in tikz when center of circle is specified – Bobyandbob Nov 4 '17 at 15:26
  • No! I really need a cycle because I would like to compute additional variables in its scope. – Viesturs Nov 4 '17 at 15:29
  • 1
    \pgfusepath can be executed once at the end and path can be added with linetos. – percusse Nov 4 '17 at 15:34
  • @percusse Your comment is very valuable. Could you please provide a complete answer. I think one needs to use \pgfpathmoveto in the beginning. – Viesturs Nov 4 '17 at 16:11
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}
  • This is the approach I needed to circumvent. I am interested to write more complicated commands inside the scope of the cycle. – Viesturs Nov 5 '17 at 1:27
  • @Viesturs So you want to use the basic layer? It's a bit unclear to me. See updated answer. – Henri Menke Nov 5 '17 at 2: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 5 '17 at 9:58

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.