# How to draw a path to form a regular pentagon?

I already have a code that draws a regular hexagon the following way

\begin{tikzpicture}[every node/.style={draw,shape=circle,fill=blue}]
\path (0,0) node (p0) {}
(-1.5,-1) node (p1) {}
(-1.5,-2.5) node (p2) {}
(1.5,-2.5) node (p3) {}
(1.5,-1) node (p4) {}
(0,-3.5) node (p5) { };
\draw (p0) -- (p1)
(p0) -- (p1)
(p0) -- (p2)
(p0) -- (p3)
(p0) -- (p4)
(p0) -- (p5)
(p1) -- (p2)
(p1) -- (p3)
(p1) -- (p4)
(p1) -- (p5)
(p2) -- (p3)
(p2) -- (p4)
(p2) -- (p5)
(p3) -- (p4)
(p3) -- (p5)
(p4) -- (p5);
\end{tikzpicture}


But Now I need to remove one of the points and to draw a regular pentagon the same way. So I need to remove (P5) and to recalibrate all other points (change their coordinates) But I don't need to change the code much.

Is there a way to do it by just slightly changing my code here. ?

• Define your points using the polar notation for radius+angle of rotation. Then you can simply make the angle 360 / number of sides, and your code is completely general... Commented Dec 1, 2015 at 19:06

You can use the geometric shapes given by the shapes.geometric library

\usetikzlibrary{shapes.geometric}

\begin{tikzpicture}[mystyle/.style={draw,shape=circle,fill=blue}]
\def\ngon{5}
\node[regular polygon,regular polygon sides=\ngon,minimum size=3cm] (p) {};
\foreach\x in {1,...,\ngon}{\node[mystyle] (p\x) at (p.corner \x){};}
\foreach\x in {1,...,\numexpr\ngon-1\relax}{
\foreach\y in {\x,...,\ngon}{
\draw (p\x) -- (p\y);
}
}
\end{tikzpicture}


For ngon being 9

One short tikz code without use of a library.

\documentclass[border=7mm]{standalone}
\usepackage{tikz}
\begin{document}
\foreach \n in {3,...,7}
\tikz\foreach \i in {1,...,\n}
\fill (\i*360/\n:1) coordinate (n\i) circle(2 pt)
\ifnum \i>1 foreach \j in {\i,...,1}{(n\i) edge (n\j)} \fi;
\end{document}


• surely you meant \documentclass[tikz,border=7mm]{standalone}, then your answer could be even shorter :) Commented Dec 3, 2015 at 9:43
• @MarkWibrow yes, but the problem with [tikz] is that in this case standalone produce one page and so one image for every \tikz :(
– Kpym
Commented Dec 3, 2015 at 10:18
• ah yes. rather silly of me not to notice that. Commented Dec 3, 2015 at 14:53

For fun, a short code with pst-poly gets the desired result:

\documentclass[svgnames]{standalone}

\usepackage{pst-poly}
\usepackage{auto-pst-pdf}
\begin{document}

\psset{unit=3.5cm, dimen=middle, linejoin=1, dotsize=12pt}
\begin{pspicture}(-1,-1)(1,1)
\providecommand{\PstPolygonNode}{\psdots[dotsize=12pt, linecolor=SteelBlue](1;\INode)
}
\rput(0,0){\PstPentagon[PolyName=A, linecolor=LightSteelBlue, linewidth=1.2pt] }
\rput(0,0){\PstPentagon[PolyName=A, PolyOffset=2] }
\end{pspicture}

\end{document}


Maybe you like the following (based on (Thruston comment) rude solution (still need some manual calculation):

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

\begin{document}
\begin{tikzpicture}[every node/.style={draw,shape=circle,fill=blue,text=white}]
\foreach \i [count=\ii from 0] in {90,150,...,390}
\path (\i:32mm) node (p\ii) {\ii};
\foreach \x in {0,...,5}
\foreach \y in {\x,...,5}
\draw (p\y) -- (p\x);
\end{tikzpicture}
\end{document}


For change number of nodes you need to determine start angle and manually determine next and final angle of node position.

Number in nodes are only informative, you can erase it.

Upgrade: An improved version, which itself calculate all necessary data from given number of nodes and angle of the first node position:

\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
%------- 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}
%--- interconnections
\foreach \x in {1,...,\numpoly}
\foreach \y in {\x,...,\numpoly}
\draw (p\y) -- (p\x);
\end{tikzpicture}
\end{document}


With selection node numbers = 8 and the first node as in direction 30 degrees, code we obtain:

Here is a version in Metapost for comparison. Something of an exercise in loops.

prologues := 3;
outputtemplate := "%j%c.eps";
vardef polygon(expr n) = for t=0 step 360/n until 359: right rotated t -- endfor cycle enddef;
beginfig(1);
path p;
for n=3 upto 7:
p := polygon(n) scaled 20 shifted (n*48,0);
for i=1 upto length p:
for j=i+1 upto length p:
draw point i of p -- point j of p;
endfor
endfor
for i=1 upto length p:
fill fullcircle scaled 3 shifted point i of p withcolor .67 red;
endfor
endfor
endfig;
end


With the PGF graph drawing stuff (compile with lualatex):

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard}
\tikzgraphsset{declare={polygon_n}{[clique]\foreach\x in\tikzgraphV{\x/}}}
\begin{document}
\foreach \n in {3,...,7}
\tikz\graph [clockwise, nodes={circle, fill=blue, inner sep=1}]
{ polygon_n [n=\n] };
\end{document}