4

How to use the tikz package to achieve the following. I looked at but still couldn't get it done. Any help is much appreciated.

enter image description here

1
  • +1: Nice question and figure, though it's not really a polygon, I think
    – user156344
    Nov 16, 2018 at 9:05

1 Answer 1

3

There are many possible ways to draw this, here is one of them.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,backgrounds}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes={circle,draw,fill=white},column sep=3mm,row sep=3mm] (mat) { 2 & 7 & 6 \\
9 & 5 & 1\\
4 & 3 & 8\\
};
\foreach \X in {1,2,3}
{\foreach \Y [evaluate=\Y as \Z using {int(10*\X+\Y)}]in {1,2,3}
{\ifnum\Z=22
\else
\draw (mat-\X-\Y) -- (mat-2-2);
\fi}}
\begin{scope}[on background layer]
 \draw (mat-3-1) rectangle (mat-1-3);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Alternatively, if you do not want to fill the nodes, you could use

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes={circle,draw},column sep=3mm,row sep=3mm] (mat) { 2 & 7 & 6 \\
9 & 5 & 1\\
4 & 3 & 8\\
};
\foreach \X [count=\XX starting from 0] in {1,2,3}
{\foreach \Y [evaluate=\Y as \Z using {int(10*\X+\Y)},count=\YY starting from 0]in {1,2,3}
{\ifnum\Z=22
\else
\draw (mat-\X-\Y) -- (mat-2-2);
\fi
\ifnum\X=1
\else
\draw (mat-\XX-\Y) -- (mat-\X-\Y);
\fi
\ifnum\Y=1
\else
\draw (mat-\X-\YY) -- (mat-\X-\Y);
\fi
}}
\end{tikzpicture}
\end{document}
0

You must log in to answer this question.

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