TikZ graph edges not drawn nicely
That's because the calculation of \y doesn't give an integer. There are two possibilities:
- the first is to use the macro
\pgfmathtruncatemacro
instead of\pgfmathsetmacro
- the second is to evaluate
\y
within the foreach loop itself
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[auto, scale=0.9]
\tikzstyle{vertex}=[draw, circle, inner sep=0.55mm]
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at (.5,-1) [vertex, fill=blue] {};
\foreach \x[evaluate=\x as \y using int(\x-1)] in {2, 3, 4, 5, 6, 7}{
%\pgfmathtruncatemacro\y{\x - 1}
\draw (v\y) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
\end{document}
\tikzstyle
is deprecated and the issue is that \pgfmathsetmacro
does not yield integers, but something like 2.0
, where .0
gets interpreted as node anchor.
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[auto, scale=0.9]
\tikzset{vertex/.style={draw, circle, inner sep=0.55mm}}
\node (v1) at (0,0) [vertex] {};
\node (v2) at (1,0) [vertex] {};
\node (v3) at (1.5,-1) [vertex] {};
\node (v4) at (1,-2) [vertex] {};
\node (v5) at (0,-2) [vertex] {};
\node (v6) at (-.5,-1) [vertex] {};
\node (v7) at (.5,-1) [vertex, fill=blue] {};
\foreach \x [remember =\x as \lastx (initially 1)] in {2, 3, 4, 5, 6, 7}{
\draw (v\lastx) to (v\x);
}
\draw (v6) to (v1);
\draw (v5) to (v7);
\draw (v4) to (v7);
\draw (v3) to (v7);
\end{tikzpicture}
\end{document}
To get it more wheel like you can use polar coordinates, like (45:1)
to draw a node at distance 1
and 45 degrees from origin. Here is an alternative version with a variable number of nodes. Change the number in \numNodes{6}
to change the number of nodes n the circle.
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
auto,
scale=0.9,
vert/.style={draw, circle, inner sep=0.55mm,fill=white}
]
\newcommand\numNodes{6}
\node[vert,fill=blue] (vC) at (0,0){};
\draw (0:1) node[vert](v0) {}
\foreach \n [evaluate = \n as \deg using {\n*360/\numNodes}] in {1,2,...,\numNodes}{
-- (\deg:1) node[vert](v\n) {}
};
\foreach \n in {0,3,4,5}{
\draw (vC) -- (v\n);
}
\end{tikzpicture}
\end {document}