5

I wish to draw regular polygons with n sides. Each side has an equal number k of segments. Below is the simplest case I can draw. Can you help to draw other cases?

square

\documentclass[border=.2cm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=.5]
\draw [line width=1.8pt, color=gray] (3.,9.)-- (3.,6.);
\draw [line width=1.8pt, color=gray] (3.,5.)-- (3.,2.);
\draw [line width=1.8pt, color=gray] (3.,10.)-- (6.,10.);
\draw [line width=1.8pt, color=gray] (7.,10.)-- (10.,10.);
\draw [line width=1.8pt, color=gray] (11.,10.)-- (14.,10.);
\draw [line width=1.8pt, color=gray] (15.,10.)-- (15.,7.);
\draw [line width=1.8pt, color=gray] (15.,6.)-- (15.,3.);
\draw [line width=1.8pt, color=gray] (15.,2.)-- (15.,-1.);
\draw [line width=1.8pt, color=gray] (15.,-2.)-- (12.,-2.);
\draw [line width=1.8pt, color=gray] (11.,-2.)-- (8.,-2.);
\draw [line width=1.8pt, color=gray] (7.,-2.)-- (4.,-2.);
\draw [line width=1.8pt, color=gray] (3.,-2.)-- (3.,1.);



\draw [fill=blue!66] (3.,9.) circle (4.55pt);
\draw [fill=blue!66] (3.,5.) circle (4.55pt);
\draw [fill=blue!66] (6.,10.) circle (4.55pt);
\draw [fill=blue!66] (10.,10.) circle (4.55pt);
\draw [fill=blue!66] (14.,10.) circle (4.55pt);
\draw [fill=blue!66] (15.,7.) circle (4.55pt);
\draw [fill=blue!66] (15.,3.) circle (4.55pt);
\draw [fill=blue!66] (15.,-1.) circle (4.55pt);
\draw [fill=blue!66] (12.,-2.) circle (4.55pt);
\draw [fill=blue!66] (8.,-2.) circle (4.55pt);
\draw [fill=blue!66] (4.,-2.) circle (4.55pt);
\draw [fill=blue!66] (3.,1.) circle (4.55pt);
\end{tikzpicture}

\end{document}
5

Something like this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
  Bullet/.style     = {fill=blue!66,draw,color=blue!66,circle,minimum size=4.55pt,scale=0.4},
}
\newcommand{\RegularPolygon}[3][3]{
\pgfmathtruncatemacro{\N}{#2}
\pgfmathtruncatemacro{\Rad}{#1}
\pgfmathsetmacro{\Step}{1/#3}
\pgfmathsetmacro{\Max}{1-1/#3}
\pgfmathsetmacro{\Len}{1.6*\Rad*sin(180/\N)/#3}
\foreach \X in {1,...,\N}
{
\coordinate (corner\X) at ({\X*360/\N+180/\N}:\Rad);
\typeout{\X\space\N}
}
\foreach \X in {1,...,\N}
{\pgfmathtruncatemacro{\Y}{mod(\X-2+\N,\N)+1}
\path[postaction={decorate,decoration={markings,
mark=between positions 0 and \Max step \Step with {
 \draw[-,line width=1.8pt,color=gray] (0,0) -- (\Len,0) node[Bullet] {};}}
        }] (corner\X)--(corner\Y);
}
}
\begin{document}
\begin{tikzpicture}
 \RegularPolygon{4}{2}
\end{tikzpicture}

\begin{tikzpicture}
 \RegularPolygon{6}{3}
\end{tikzpicture}


\begin{tikzpicture}
 \RegularPolygon{5}{5}
\end{tikzpicture}

\end{document}

enter image description here

Note that it was not clear to me whether the empty segments after a circle are a certain percentage of the segment or some fixed amount. Once this is clear, I'll be happy to adjust the code if necessary.

EDIT: Here is a somewhat cleaner version. In the above, I truncated the radius to an integer, and had an unnecessary \typeout in. Here I also make the lowest boundary horizontal. (This can be adjusted by changing the -90 in \coordinate (corner\X) at ({\X*360/\N+180/\N-90}:\Rad);.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
  Bullet/.style     = {fill=blue!66,draw,color=blue!66,circle,minimum size=4.55pt,scale=0.4},
}
\newcommand{\RegularPolygon}[3][3]{
\pgfmathtruncatemacro{\N}{#2}
\pgfmathsetmacro{\Rad}{#1}
\pgfmathsetmacro{\Step}{1/#3}
\pgfmathsetmacro{\Max}{1-1/#3}
\pgfmathsetmacro{\Len}{1.6*\Rad*sin(180/\N)/#3}
\foreach \X in {1,...,\N}
{
\coordinate (corner\X) at ({\X*360/\N+180/\N-90}:\Rad);
}
\foreach \X in {1,...,\N}
{\pgfmathtruncatemacro{\Y}{mod(\X-2+\N,\N)+1}
\path[postaction={decorate,decoration={markings,
mark=between positions 0 and \Max step \Step with {
 \draw[-,line width=1.8pt,color=gray] (0,0) -- (\Len,0) node[Bullet] {};}}
        }] (corner\X)--(corner\Y);
}
}
\begin{document}
\begin{tikzpicture}
\begin{scope}
 \RegularPolygon{4}{2}
\end{scope}
\begin{scope}[xshift=6cm]
 \RegularPolygon{6}{3}
\end{scope}
\begin{scope}[yshift=-5.5cm]
 \RegularPolygon{5}{5}
\end{scope} 
\end{tikzpicture}
3

with use of regular polygons and for start without loops (which can make code shorter):

enter image description here

\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shapes.geometric}

\tikzset{
every edge/.style = {-{Circle[length=5pt, color=blue!66!black, line width=0.5pt, fill=blue!66]},
                        draw=gray, line width=1.8pt, shorten >=1mm},
        RP/.style = {regular polygon,regular polygon sides=#1, minimum size=44mm}
        }    
\begin{document}
\begin{figure}
\centering
    \begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=3]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 1);
\end{tikzpicture}\hfil
\begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=4]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 4)
      (s.corner 4) edge (s.side 4) (s.side 4) edge (s.corner 1);
\end{tikzpicture}

\bigskip
\begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=5]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 4)
      (s.corner 4) edge (s.side 4) (s.side 4) edge (s.corner 5)
      (s.corner 5) edge (s.side 5) (s.side 5) edge (s.corner 1);
\end{tikzpicture}\hfil
\begin{tikzpicture}[scale=.5,baseline=(current bounding box.center)]
\node (s) [RP=6]  {};
\path (s.corner 1) edge (s.side 1) (s.side 1) edge (s.corner 2)
      (s.corner 2) edge (s.side 2) (s.side 2) edge (s.corner 3)
      (s.corner 3) edge (s.side 3) (s.side 3) edge (s.corner 4)
      (s.corner 4) edge (s.side 4) (s.side 4) edge (s.corner 5)
      (s.corner 5) edge (s.side 5) (s.side 5) edge (s.corner 6)
      (s.corner 6) edge (s.side 6) (s.side 6) edge (s.corner 1);
\end{tikzpicture}
\end{figure}
\end{document}
3

A decoration might be handy:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,shapes.geometric}
\def\drawmarker#1#2{%
  \draw [line/.try] let \p1=(\tikzinputsegmentfirst),\p2=(\tikzinputsegmentlast), 
    \n1={veclen(\x2-\x1, \y2-\y1)} in
  \foreach \i [evaluate={\j=\n1*(\i-1)/(#1)+#2;\k=\n1*\i/(#1)-#2;}] in {1,...,#1}{
    ($(\p1)!\j pt!(\p2)$)--($(\p1)!\k pt!(\p2)$) 
     node [shape=circle, draw=none, minimum size=0, inner sep=0, dot/.try] {}
  };
}
\tikzset{lines/.style args={#1with sep #2}{
  decoration={
    reverse path, show path construction,
    lineto code=\drawmarker{#1}{#2},
    closepath code=\drawmarker{#1}{#2} 
  }, decorate},
  lines/.default=2,
  line/.style={thick, draw=gray},
  dot/.style={fill=blue!66, minimum size=3pt}
}
\begin{document}
\tikz\foreach \s [count=\t from 1] in {3,...,6}
  \node [lines=\t with sep 3pt, regular polygon, regular polygon sides=\s,
    minimum size=3cm] at (0,-\s*3) {};
\end{document}

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.