8

I am drawing a graph as below:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.shapes}
\begin{document}
\tiny
\begin{tikzpicture}
\tikzset{dot/.style 2 args=
{decorate,decoration={shape backgrounds,shape=circle,shape size=#1,shape sep=#2}}}
    \coordinate (O) at (0,0);
       \draw (O)
        \foreach \n in {1,...,4} {
         -- ++(-120:{\n*3-2}) --  ++(0:{\n*3-1}) -- ++(120:{\n*3})
       };
       \draw[dot={2pt}{1cm},fill] (O)
        \foreach \n in {1,...,4} {
         -- ++(-120:{\n*3-2}) --  ++(0:{\n*3-1}) -- ++(120:{\n*3})
       };
\end{tikzpicture}
\end{document}

But I have 2 questions:

  1. how to annotate the points in the foreach loop? I wish I can get "1,2,3,..." besides each point.
  2. Can we combine two \draw command into one command?

enter image description here

1
  • OP accepted beetlej's edit on changing title (add annotation on <s>loop</s>decoration points. However, I think that one of the distinctive feature of this question is the fact that points are generated in a loop.
    – ebosi
    Commented Nov 29, 2016 at 15:21

2 Answers 2

10
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[dot/.style={
  postaction=decorate,
  decoration={markings,mark= between positions 0 and 1 step #1mm with{%
    \node[circle,inner sep=0.5pt,fill, label=\pgfdecoratedangle-90:{%
      \number\numexpr\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}-1}]{};%
    }
  }
}
]

\coordinate (O) at (0,0);
   \draw[dot=10] (O)
      \foreach \n in {1,...,4} {
         -- ++(-120:{\n*3-2}) --  ++(0:{\n*3-1}) -- ++(120:{\n*3})
      };
\end{tikzpicture}
\end{document}

enter image description here

4
  • Amazing answer! Thanks, can we adjust the start index from 1 to 0?
    – lucky1928
    Commented Nov 29, 2016 at 13:07
  • @lucky1928 See the edit. For more complicated computations you need to switch to \pgfmathparse{...}\pgfmathresult
    – percusse
    Commented Nov 29, 2016 at 13:16
  • Sounds like the last point didn't been decorated. (point 78 in this case).
    – beetlej
    Commented Nov 29, 2016 at 14:27
  • @beetlej It's the precision issue add }--++(120:1bp); at the end of the path
    – percusse
    Commented Nov 29, 2016 at 14:34
3

One modified version to decorate with perpendicular lines.

\documentclass[tikz,border=10pt,convert={outfile=\jobname.jpg}]{standalone}
\usetikzlibrary{decorations.markings,calc}
\begin{document}
\tiny\begin{tikzpicture}
\tikzset{dot/.style={
  postaction=decorate,
   decoration={markings,mark=between positions 0 and 1 step #1mm with{%
    \pgfmathsetmacro{\desc}{int(\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}-1)}
    \draw (0,-3pt) -- node [label={\pgfdecoratedangle-90:\desc}] {} ++(0,6pt);
    } 
    }
  }
}
\def\m{5}
\coordinate (O) at (0,0);
   \draw[dot=3] (O)
      \foreach \n in {1,...,22} {
         -- ++({360/\m*Mod(\n,\m)}:{\n*0.3})
      };
\end{tikzpicture}
\end{document}

Output: enter image description here

You must log in to answer this question.

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