代码

代码

我正在尝试重新创建(不太漂亮的)下图

在此处输入图片描述

使用文章底部的代码,我能够得出以下解决方案

在此处输入图片描述

我对此解决方案有一些不满,希望能够解决

  • 三个对角箭头(A ->- C、B ->- E 和 D ->- F 的路径)可以平行吗?
  • 我使用了以下问题中的代码:TikZ:如何在线中间画箭头?创建中间的箭头。但是,这似乎过于复杂,有没有更简单的解决方案?
  • 类似地,定义每个节点似乎是不必要的,可以使用某种循环来完成吗?
  • 关于如何重建图像的任何其他解决方案都非常受欢迎。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing,
decorations.markings,
positioning}
\tikzset{
  % style to apply some styles to each segment of a path
  on each segment/.style={
    decorate,
    decoration={
      show path construction,
      moveto code={},
      lineto code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
      curveto code={
        \path [#1] (\tikzinputsegmentfirst)
        .. controls
        (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
        ..
        (\tikzinputsegmentlast);
      },
      closepath code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
    },
  },
  % style to add an arrow in the middle of a path
  mid arrow/.style={postaction={decorate,decoration={
        markings,
        mark=at position .5 with {\arrow[#1]{stealth}}
      }}},
}

\usepackage[utf8]{inputenc}

\begin{document}

\begin{tikzpicture}[
    every node/.style={draw,circle}]

\node (A) at (0,0) {$A$};
\node[label=above:{$\$\,1$},above right= of A] (B) {$B$};
\node[label=below:{$\$\,2$},below right= of A] (C) {$C$};
\node[label=above:{$\$\,2$},right= of B] (D) {$D$};
\node[label=below:{$\$\,1$},right= of C] (E) {$E$};
\node[above right= of E] (F) {$F$};

\path [draw,postaction={on each segment={mid arrow}}]
(A) -- (B)%
(A) -- (C)%
(B) -- (D)%
(B) -- (E)%
(C) -- (E)%
(D) -- (F)%
(E) -- (F)%
;

\end{tikzpicture}

\end{document}

答案1

将其画成一个正六边形。

在此处输入图片描述

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings,positioning}
\tikzset{
 mid arrow/.style={draw, postaction={decorate},
 decoration={
    markings, mark=at position 0.5 with {\arrow{stealth}}}},
 every node/.style={draw,circle}}

\begin{document}
\begin{tikzpicture}
\node (F) at (0:2) {$F$};
\node (D) at (60:2) {$D$};
\node (B) at (120:2) {$B$};
\node (A) at (180:2) {$A$};
\node (C) at (240:2) {$C$};
\node (E) at (300:2) {$E$};

\path [mid arrow] (A) -- (B);
\path [mid arrow] (A) -- (C);%
\path [mid arrow] (B) -- (D);%
\path [mid arrow] (B) -- (E);%
\path [mid arrow] (C) -- (E);%
\path [mid arrow] (D) -- (F);%
\path [mid arrow] (E) -- (F);
\end{tikzpicture}
\end{document}

答案2

像这样?

在此处输入图片描述

正多边形(在你的情况下是六边形)具有对边平行:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{decorations.markings,
                positioning,
                shapes.geometric}

\begin{document}

\begin{tikzpicture}[
     node distance = 12mm and 12mm,
     vertex/.style = {circle, draw, fill=white, minimum size=1em, inner sep=1pt}, 
        decoration = {markings, mark=at position .5 with {\arrow{stealth}}},
 every edge/.style = {draw, postaction={decorate}},
every label/.style = {inner sep=1pt, font=\footnotesize}
                    ]
\node (s) [regular polygon, 
           regular polygon sides=6,
           minimum size=44mm] {}; % coordinates for nodes
\foreach \i/\j [count=\k] in {D/\$\,2,B/\$\,1,A/,C/\$\,2,E/\$\,1,F/} % loop for nodes
\node (v\k) [vertex, label={\k*60}:\j] at (s.corner \k) {$\i$};
\draw  (v1) edge (v2);
\draw  (v2) edge (v3);
\draw  (v3) edge (v4);
\draw  (v4) edge (v5);
\draw  (v5) edge (v6);
\draw  (v6) edge (v1);
%
\draw  (v2) edge (v5);
    \end{tikzpicture}
\end{document}

编辑: 您可以使用循环绘制多边形周围的边:

\foreach \i in {0,...,5}
{
\pgfmathsetmacro{\j}{int(Mod(\i,6)+1)}
\pgfmathsetmacro{\k}{int(Mod(\i+1,6)+1)}
\draw  (v\j)  edge (v\k);
}

因为在您的情况下多边形是六边形,所以代码不会像以前一样短很多:-) 。

相关内容