我对使用 Tikz 还不熟悉... 我正在尝试使用 TikZ 绘制一个轮状图形。我已经完成了基本图形,但结果并不像我预期的那样。我的图形中的边缘没有很好地居中/对齐。
边缘似乎遵循坐标,而不是在节点之间绘制一条最短路径直线,并停在其圆圈表示处。有些边缘很好(例如 v1 到 v2、v1 到 v6,...),但有些则不好(例如 v2 到 v3)。
这是我的 TikZ 图片代码,以及一张显示我得到的内容的图像。
\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 in {2, 3, 4, 5, 6, 7}{
\pgfmathsetmacro\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}
答案1
这是因为 \y 的计算结果不是整数。有两种可能性:
- 第一种是使用宏
\pgfmathtruncatemacro
代替\pgfmathsetmacro
- 第二种是
\y
在 foreach 循环本身内进行评估
\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}
答案2
\tikzstyle
已被弃用,问题是\pgfmathsetmacro
不会产生整数,但类似的东西2.0
会.0
被解释为节点锚点。
\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}
答案3
也许你会喜欢:
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
vertex/.style = {circle, draw, fill=#1, inner sep=0.5mm}
]
%
\node (s) [regular polygon,regular polygon sides=6,
draw, minimum size=20mm, above] at (0.5,-2) {};
\draw (s.corner 3) -- (s.corner 6);
\node (c) [vertex=blue] at (s.center) {};
%
\foreach \i in {1,...,6}{\node (s\i) [vertex=white] at (s.corner \i) {}; }
\draw (c) -- (s4)
(c) -- (s5);
\end{tikzpicture}
\end{document}
答案4
为了使它更像轮子,您可以使用极坐标,例如在距离原点 45 度处(45:1)
绘制一个节点。这是具有可变节点数量的替代版本。更改中的数字可更改圆上的节点数。1
\numNodes{6}
\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}