2

I've managed to find the graph below but I would like for the edges to only be between consecutive outside points (ie (1,2), (2,3), ..., (5,1))

\documentclass[border=3mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
 every node/.style={draw,shape=circle,fill=blue,text=white}]
 %%%% variable data data
\def\numpoly{8}%number of nodes
\def\startangle{30}%direction of the first node
\def\pradious{33mm}
%------- calculations of the positions angles
\pgfmathparse{int(\startangle+360/\numpoly)}%
    \let\nextangle=\pgfmathresult
\pgfmathparse{int(\startangle-360/\numpoly+360)}%
    \let\endtangle=\pgfmathresult
%--- nodes
    \foreach \i [count=\ii from 1] in {\startangle,\nextangle,...,\endtangle}
\path (\i:\pradious) node (p\ii) {\ii};
%--- interconnections
    \foreach \x in {1,...,\numpoly}
        \foreach \y in {\x,...,\numpoly}
\draw (p\y) -- (p\x);
    \end{tikzpicture}
\end{document}

This is my first time using latex so sorry if this is super easy and I've just missed it!

Eloise
  • 23

1 Answers1

5

TikZ has these shapes predefined.

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
 \node[regular polygon,regular polygon sides=8,draw,minimum width=6cm]
 (octagon){};
 \foreach \X [count=\Y] in {8,...,1}
 {\node[draw,shape=circle,fill=blue,text=white] at (octagon.corner \X){\Y}; }
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: Just for fun, if you want to relabel the corners arbitrarily, this trick may help.

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
 \node[regular polygon,regular polygon sides=8,draw,minimum width=6cm]
 (octagon){};
 \foreach \X [evaluate=\X as \Y using {ifthenelse(int(mod(\X+1,8))==0,8,
 int(mod(\X+1,8)))}] in {1,...,8}
 {\node[draw,shape=circle,fill=blue,text=white] at (octagon.corner \X){\Y}; }
\end{tikzpicture}
\end{document}

enter image description here

Instead of \X+1 you can put whatever, e.g. 5-\X.

or you could just use rotate to get the same layout as in your picture:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
 \node[rotate=-30,regular polygon,regular polygon sides=8,draw,minimum width=6cm]
 (octagon){};
 \foreach \X in {1,...,8}
 {\node[draw,shape=circle,fill=blue,text=white] at (octagon.corner \X){\X}; }
\end{tikzpicture}
\end{document}

enter image description here