程式碼

程式碼

我正在嘗試重新創建(不太漂亮的)下圖

在此輸入影像描述

使用帖子底部的程式碼我能夠想出以下解決方案

在此輸入影像描述

我對這個解決方案有一些抱怨,我希望能夠解決

  • 三個對角線箭頭(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);
}

因為在你的情況下多邊形是六邊形,所以代碼並沒有像以前一樣短:-)。

相關內容