![八线图绘制](https://linux22.com/image/343705/%E5%85%AB%E7%BA%BF%E5%9B%BE%E7%BB%98%E5%88%B6.png)
我正在尝试绘制一个只能使用垂直、水平或 45 度对角线的图形。我正在尝试制作一个具有 6 个顶点和 8 条边的图形。到目前为止,我有以下内容:
\begin{tikzpicture}[->,>=stealth',auto,node distance=3cm,
thick,main node/.style={circle,fill=black!30,draw,minimum size=1cm,inner sep=0pt]}]
\node[main node] (v) []{$v$};
\node[main node] (w) [below left of =v] {$w$};
\node[main node] (1) [left of =v] {$1$};
\node[main node] (2) [above of =w] {$2$};
\node[main node] (3) [below of =v ] {$3$};
\node[main node] (4) [right of = w] {$4$};
\path[-]
(v)
edge node {} (1)
edge node {} (2)
edge node {} (3)
edge node {} (4)
(w)
edge node {} (1)
edge node {} (2)
edge node {} (3)
edge node {} (4);
\end{tikzpicture}
但我希望它看起来像附图一样,其中粉色和蓝色线条是八线的。
有小费吗?
答案1
我会用明确的坐标放置节点,使用变量来表达对称性。请注意,您可以缩放图片而无需重新计算坐标(请参阅环境选项scale=2
)tikzpicture
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
[->, >=stealth', auto, thick,
main node/.style=
{circle, fill=black!30, draw, minimum size=1cm, inner sep=0pt]},
scale=2
]
\newcommand\vx{0.5}
\newcommand\vy{1}
\newcommand\fx{2} % 4.x
\node[main node] (2) at (-\vx, 0 ) {$2$};
\node[main node] (3) at ( \vx, 0 ) {$3$};
\node[main node] (v) at ( \vx, \vy) {$v$};
\node[main node] (w) at (-\vx,-\vy) {$w$};
\node[main node] (1) at (-\fx, \vy) {$1$};
\node[main node] (4) at ( \fx,-\vy) {$4$};
\path[-]
(v) edge node {} (1)
edge node {} (2)
edge node {} (3)
edge node {} (4)
(w) edge node {} (1)
edge node {} (2)
edge node {} (3)
edge node {} (4);
\end{tikzpicture}
\end{document}
答案2
那么你可以尝试重新组织你相对于哪个节点定位的节点
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',auto,node distance=3cm,
thick,main node/.style={circle,fill=black!30,draw,minimum size=1cm,inner sep=0pt]}]
\node[main node] (v) []{$v$};
\node[main node] (2) [below left of =v] {$2$};
\node[main node] (w) [below of =2] {$w$};
\node[main node] (1) [above left of =2] {$1$};
\node[main node] (3) [below of =v] {$3$};
\node[main node] (4) [below right of = 3] {$4$};
\path[-]
(v)
edge node {} (1)
edge node {} (2)
edge node {} (3)
edge node {} (4)
(w)
edge node {} (1)
edge node {} (2)
edge node {} (3)
edge node {} (4);
\end{tikzpicture}
\end{document}
或者,虽然 TikZ 的相对定位非常有用,但如果您心中有一个特定的位置,您可能希望明确定位节点,或者使用矩阵将它们更自然地放置在网格上,如下例所示。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[->,>=stealth',auto,node distance=3cm,
thick,main node/.style={circle,fill=black!30,draw,minimum size=1cm,inner sep=0pt]}]
\matrix (octilinear) [matrix of nodes,nodes={main node},column sep = 2cm,row sep = 2cm]
{
1 & & & V & & \\
& & 2 & 3 & & \\
& & W & & & 4 \\
};
\draw[-] (octilinear-1-1) -- (octilinear-1-4);
\draw[-] (octilinear-1-4) -- (octilinear-2-4);
\draw[-] (octilinear-2-3) -- (octilinear-1-4);
\draw[-] (octilinear-2-3) -- (octilinear-3-3);
\draw[-] (octilinear-3-3) -- (octilinear-3-6);
\draw[-] (octilinear-2-4) -- (octilinear-3-3);
\draw[-] (octilinear-1-1) -- (octilinear-3-3);
\draw[-] (octilinear-1-4) -- (octilinear-3-6);
\end{tikzpicture}
\end{document}