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

  • 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 Nov 29 '16 at 15:21
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

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

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.