添加交叉对角线

添加交叉对角线

大家好,我想画一张这样的画: 在此处输入图片描述 到目前为止我得到了一个类似的:

在此处输入图片描述

我想在对角线分隔的不同部分中添加交叉的对角线和数字。这是我的代码:

\tikzstyle{start_state} =[rectangle, draw=orange!60, fill=orange!5, very thick, minimum size=15mm]
\tikzstyle{normal_state} =[rectangle, draw=green!60, fill=green!5, very thick, minimum size=15mm]
\tikzstyle{water_state} =[rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=15mm]
\tikzstyle{terminal_state} =[rectangle, draw=red!60, fill=red!5, very thick, minimum size=15mm]

% \tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{policy_arrow} = [very thick,->,>=stealth, orange]


\begin{tikzpicture}[node distance=3cm]
%Nodes
\node[start_state] (state_1)  {\(S_1\)};
\node[normal_state] (state_2)  [right of = state_1] {\(S_2\)};
\node[normal_state] (state_3)  [right of = state_2] {\(S_3\)};
\node[normal_state] (state_4)  [below of = state_1] {\(S_4\)};
\node[water_state] (state_5)  [right of = state_4] {\(S_5\)};
\node[normal_state] (state_6)  [right of = state_5] {\(S_6\)};
\node[normal_state] (state_7)  [below of = state_4] {\(S_7\)};
\node[water_state] (state_8)  [right of = state_7] {\(S_8\)};
\node[terminal_state] (state_9)  [right of = state_8] {\(S_9\)};
%Lines
% east--west
\draw[arrow] ([yshift=2pt] state_1.east) --node[anchor = south] {-1} ([yshift=2pt] state_2.west);
\draw[arrow] ([yshift=-2pt] state_2.west) --node[anchor = north] {-1} ([yshift=-2pt] state_1.east);
\draw[arrow] ([yshift=2pt] state_2.east) --node[anchor = south] {-1} ([yshift=2pt] state_3.west);
\draw[arrow] ([yshift=-2pt] state_3.west) --node[anchor = north] {-1} ([yshift=-2pt] state_2.east);
\draw[arrow] ([yshift=2pt] state_4.east) --node[anchor = south,blue] {-3} ([yshift=2pt] state_5.west);
\draw[arrow] ([yshift=-2pt] state_5.west) --node[anchor = north] {-1} ([yshift=-2pt] state_4.east);
\draw[arrow] ([yshift=2pt] state_5.east) --node[anchor = south] {-1} ([yshift=2pt] state_6.west);
\draw[arrow] ([yshift=-2pt] state_6.west) --node[anchor = north,blue] {-3} ([yshift=-2pt] state_5.east);
\draw[arrow] ([yshift=2pt] state_7.east) --node[anchor = south,blue] {-3} ([yshift=2pt] state_8.west);
\draw[arrow] ([yshift=-2pt] state_8.west) --node[anchor = north] {-1} ([yshift=-2pt] state_7.east);
\draw[arrow] (state_8)     -- node[anchor = south, red] {+5} (state_9);
% south--north
\draw[arrow] ([xshift=2pt] state_1.south) --node[anchor = west] {-1} ([xshift=2pt] state_4.north);
\draw[arrow] ([xshift=-2pt] state_4.north) --node[anchor = east] {-1} ([xshift=-2pt] state_1.south);
\draw[arrow] ([xshift=2pt] state_2.south) --node[anchor = west,blue] {-3} ([xshift=2pt] state_5.north);
\draw[arrow] ([xshift=-2pt] state_5.north) --node[anchor = east] {-1} ([xshift=-2pt] state_2.south);
\draw[arrow] ([xshift=2pt] state_3.south) --node[anchor = west] {-1} ([xshift=2pt] state_6.north);
\draw[arrow] ([xshift=-2pt] state_6.north) --node[anchor = east] {-1} ([xshift=-2pt] state_3.south);
\draw[arrow] ([xshift=2pt] state_4.south) --node[anchor = west] {-1} ([xshift=2pt] state_7.north);
\draw[arrow] ([xshift=-2pt] state_7.north) --node[anchor = east] {-1} ([xshift=-2pt] state_4.south);
\draw[arrow] ([xshift=2pt] state_5.south) --node[anchor = west,blue] {-3} ([xshift=2pt] state_8.north);
\draw[arrow] ([xshift=-2pt] state_8.north) --node[anchor = east,blue] {-3} ([xshift=-2pt] state_5.south);
\draw[arrow] (state_6)     -- node[anchor = west,red] {+5} (state_9);
\end{tikzpicture}

答案1

作为起点,您可以使用以下内容:

带数字的交叉节点矩阵

\documentclass[tikz,border=3.14mm]{standalone}

\newcommand{\cross}[5]
    {
    \def\shft{1pt}
    \draw   ([xshift=\shft,yshift=-\shft)]#1.north west) -- ([xshift=-\shft,yshift=\shft)]#1.south east)
            ([xshift=-\shft,yshift=-\shft)]#1.north east) -- ([xshift=\shft,yshift=\shft)]#1.south west);
    \def\l{6pt}
    \node [above=\l] at (#1.center) {#2}; 
    \node [right=\l] at (#1.center) {#3}; 
    \node [below=\l] at (#1.center) {#4}; 
    \node [left=\l] at (#1.center) {#5}; 
    }

\begin{document}

\tikzstyle{start_state} =[rectangle, draw=orange!60, fill=orange!5, very thick, minimum size=15mm,,label={[label distance=-2mm]-45:$#1$}]
\tikzstyle{normal_state} =[rectangle, draw=green!60, fill=green!5, very thick, minimum size=15mm,label={[label distance=-2mm]-45:$#1$}]
\tikzstyle{water_state} =[rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=15mm,,label={[label distance=-2mm]-45:$#1$}]
\tikzstyle{terminal_state} =[rectangle, draw=red!60, fill=red!5, very thick, minimum size=15mm,,label={[label distance=-2mm]-45:$#1$}]

% \tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{policy_arrow} = [very thick,->,>=stealth, orange]


\begin{tikzpicture}[node distance=3cm]
%Nodes
\node[start_state=S_1] (state_1)  {};
\node[normal_state=S_2,right of = state_1] (state_2) {};
\node[normal_state=S_3] (state_3)  [right of = state_2] {};
\node[normal_state=S_4] (state_4)  [below of = state_1] {};
\node[water_state=S_5] (state_5)  [right of = state_4] {};
\node[normal_state=S_6] (state_6)  [right of = state_5] {};
\node[normal_state=S_7] (state_7)  [below of = state_4] {};
\node[water_state=S_8] (state_8)  [right of = state_7] {};
\node[terminal_state=S_9] (state_9)  [right of = state_8] {};

% Crosses and figures
\cross{state_1}{6}{0}{4}{1}
\cross{state_2}{5}{}{}{2}
\cross{state_3}{}{}{4}{}
\cross{state_4}{1}{}{3}{9}

%Lines
% east--west
\draw[arrow] ([yshift=2pt] state_1.east) --node[anchor = south] {-1} ([yshift=2pt] state_2.west);
\draw[arrow] ([yshift=-2pt] state_2.west) --node[anchor = north] {-1} ([yshift=-2pt] state_1.east);
\draw[arrow] ([yshift=2pt] state_2.east) --node[anchor = south] {-1} ([yshift=2pt] state_3.west);
\draw[arrow] ([yshift=-2pt] state_3.west) --node[anchor = north] {-1} ([yshift=-2pt] state_2.east);
\draw[arrow] ([yshift=2pt] state_4.east) --node[anchor = south,blue] {-3} ([yshift=2pt] state_5.west);
\draw[arrow] ([yshift=-2pt] state_5.west) --node[anchor = north] {-1} ([yshift=-2pt] state_4.east);
\draw[arrow] ([yshift=2pt] state_5.east) --node[anchor = south] {-1} ([yshift=2pt] state_6.west);
\draw[arrow] ([yshift=-2pt] state_6.west) --node[anchor = north,blue] {-3} ([yshift=-2pt] state_5.east);
\draw[arrow] ([yshift=2pt] state_7.east) --node[anchor = south,blue] {-3} ([yshift=2pt] state_8.west);
\draw[arrow] ([yshift=-2pt] state_8.west) --node[anchor = north] {-1} ([yshift=-2pt] state_7.east);
\draw[arrow] (state_8)     -- node[anchor = south, red] {+5} (state_9);
% south--north
\draw[arrow] ([xshift=2pt] state_1.south) --node[anchor = west] {-1} ([xshift=2pt] state_4.north);
\draw[arrow] ([xshift=-2pt] state_4.north) --node[anchor = east] {-1} ([xshift=-2pt] state_1.south);
\draw[arrow] ([xshift=2pt] state_2.south) --node[anchor = west,blue] {-3} ([xshift=2pt] state_5.north);
\draw[arrow] ([xshift=-2pt] state_5.north) --node[anchor = east] {-1} ([xshift=-2pt] state_2.south);
\draw[arrow] ([xshift=2pt] state_3.south) --node[anchor = west] {-1} ([xshift=2pt] state_6.north);
\draw[arrow] ([xshift=-2pt] state_6.north) --node[anchor = east] {-1} ([xshift=-2pt] state_3.south);
\draw[arrow] ([xshift=2pt] state_4.south) --node[anchor = west] {-1} ([xshift=2pt] state_7.north);
\draw[arrow] ([xshift=-2pt] state_7.north) --node[anchor = east] {-1} ([xshift=-2pt] state_4.south);
\draw[arrow] ([xshift=2pt] state_5.south) --node[anchor = west,blue] {-3} ([xshift=2pt] state_8.north);
\draw[arrow] ([xshift=-2pt] state_8.north) --node[anchor = east,blue] {-3} ([xshift=-2pt] state_5.south);
\draw[arrow] (state_6)     -- node[anchor = west,red] {+5} (state_9);
\end{tikzpicture}
\end{document}

这不是最佳选择,因为一切都可以通过创建新命令来实现自动化,但无论如何。例如,如果您希望线条以不同的颜色绘制,您还可以添加第 6 个参数。

答案2

另一种可能性是使用 ,pic它将节点的名称(准确地说是数字)、正方形内的数字组和带有箭头的数字组(均逆时针方向)作为参数。仅当箭头的数量大于固定下限时才会绘制箭头。

我认为这种方法简单但有局限性。如果我们想给箭头数字添加颜色,则需要很多参数,所以我更喜欢所有数字都是黑色。

\documentclass[tikz,border=2mm]{standalone}

\def\lv{-10} % lower value for the numbers over the arrows
             % this number is not drawn, neither the arrow
             % bigger numbers are drawn
\tikzset
{%
  start/.style   ={draw=orange!60,fill=orange!5,very thick},
  normal/.style  ={draw=green!60, fill=green!5, very thick},
  water/.style   ={draw=blue!60,  fill=blue!5,  very thick},
  terminal/.style={draw=red!60,   fill=red!5,   very thick},
  pics/state/.style n args={3}{% name (number), inner numbers (E,N,W,S); numbers over the arrows starting here (same order)
    code={%
      \foreach\i [count=\xi] in {#3}
      {
        \ifnum\i > \lv
          \begin{scope}[rotate=90*\xi-90]
            \draw[thin,black,-stealth] (0.78,0.1) --++ (1.44,0);
            \node at (1.5,0.4) {$\i$};
        \end{scope}
       \fi
      }
      \filldraw (-0.75,-0.75) rectangle (0.75, 0.75);
      \draw     (-0.75,-0.75) --        (0.75, 0.75);
      \draw     (-0.75, 0.75) --        (0.75,-0.75);
      \node[fill,inner sep=1pt] at (0,0) {$S_{#1}$};
      \foreach\i [count=\xi] in {#2}
        \node at (90*\xi-90:0.5)  {\i};
    }},
}

\begin{document}
\begin{tikzpicture}
\pic[start]    at (0, 0) {state={1}{2,0,,}{-1,\lv,\lv,-1}};
\pic[normal]   at (3, 0) {state={2}{3,,1,1}{-1,\lv,-1,-3}};
\pic[normal]   at (6, 0) {state={3}{,,2,4}{\lv,\lv,-1,-1}};
\pic[normal]   at (0,-3) {state={4}{1,1,,1}{-3,-1,\lv,-1}};
\pic[water]    at (3,-3) {state={5}{4,2,0,2}{-1,-1,-1,-3}};
\pic[water]    at (6,-3) {state={6}{,3,1,5}{\lv,-1,-3,+5}};
\pic[normal]   at (0,-6) {state={7}{2,0,,}{-3,-1,\lv,\lv}};
\pic[water]    at (3,-6) {state={8}{5,1,1}{+5,-3,-1,\lv}};
\pic[terminal] at (6,-6) {state={9}{}{\lv,\lv,\lv,\lv}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

这只是另一种解决方案,使用path picture来绘制内部交叉点,并labels获取节点内的值。一般分布是用 进行的matrix

\documentclass[tikz,border=2mm]{standalone}

\tikzset
{%
  basenode/.style = {minimum size=1.5cm, very thick, draw=#1!60, fill=#1!5},    
  cross/.style 2 args ={basenode=#2, 
    label={[fill=#2!5, circle, inner sep=1pt]center:#1},
    path picture={\draw (path picture bounding box.north west) -- (path picture bounding box.south east);
      \draw (path picture bounding box.north east) -- (path picture bounding box.south west);},
    },
    labels/.style n args={4}{
        label={[anchor=west]west:#1},
        label={[anchor=north]north:#2},
        label={[anchor=east]east:#3},
        label={[anchor=south]south:#4},
    },
    arrow/.style={thick, >=stealth,->}
}

\begin{document}
\begin{tikzpicture}
\matrix(A)[column sep=2cm, row sep=2cm] {
\node[cross={$S_1$}{orange}, labels={}{}{2}{0}] (S1) {}; &
\node[cross={$S_2$}{green}, labels={1}{}{3}{1}] (S2) {}; &
\node[cross={$S_3$}{green}, labels={2}{}{}{4}] (S3) {}; \\
\node[cross={$S_4$}{orange}, labels={}{1}{1}{1}] (S4) {}; &
\node[cross={$S_5$}{green}, labels={0}{7}{4}{2}] (S5) {}; &
\node[cross={$S_6$}{green}, labels={1}{3}{}{5}] (S6) {}; \\
\node[cross={$S_7$}{orange}, labels={}{0}{2}{}] (S7) {}; &
\node[cross={$S_8$}{green}, labels={1}{1}{5}{}] (S8) {}; &
\node[basenode=red] (S9) {0}; \\
};

\foreach \i/\j/\k in {1/2/-1, 2/3/-1, 5/6/-1}
    \draw[arrow] ([yshift=2mm]S\i.east)--node[above]{\k} ([yshift=2mm]S\j.west);
\foreach \i/\j/\k in {4/5/-3, 7/8/-3}
    \draw[arrow] ([yshift=2mm]S\i.east)--node[above,blue]{\k} ([yshift=2mm]S\j.west);
\foreach \i/\j/\k in {2/1/-1, 3/2/-1,5/4/-1,8/7/-1}
    \draw[arrow] ([yshift=-2mm]S\i.west)--node[below]{\k} ([yshift=-2mm]S\j.east);
\draw[arrow] ([yshift=-2mm]S6.west)--node[below,blue]{-3} ([yshift=-2mm]S5.east);
\draw[arrow] (S8)--node[above, red]{+5}(S9);


\foreach \i/\j/\k in {1/4/-1, 3/6/-1, 4/7/-1}
    \draw[arrow] ([xshift=2mm]S\i.south)--node[right]{\k} ([xshift=2mm]S\j.north);
\foreach \i/\j/\k in {2/5/-3, 5/8/-3}
    \draw[arrow] ([xshift=2mm]S\i.south)--node[right,blue]{\k} ([xshift=2mm]S\j.north);
\foreach \i/\j/\k in {4/1/-1,5/2/-1,6/3/-1,7/4/-1}
    \draw[arrow] ([xshift=-2mm]S\i.north)--node[left]{\k} ([xshift=-2mm]S\j.south);
\draw[arrow] ([xshift=-2mm]S8.north)--node[left,blue]{-3} ([xshift=-2mm]S5.south);
\draw[arrow] (S6)--node[above, right]{+5}(S9);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容