TikZ 矩阵用于通过具有整数坐标的点绘制蛇形曲线(轨道模型)

TikZ 矩阵用于通过具有整数坐标的点绘制蛇形曲线(轨道模型)

我想使用 TikZ 矩阵创建这样的图片:
在此处输入图片描述

添加这些“蛇线”,特别是其末端的“半圆”的最佳方法是什么?

在此处输入图片描述

提示:我添加了一些帮助行和列。

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\pgfmathsetlengthmacro\RowSep{1.5em}
\pgfmathsetlengthmacro\MinSize{1.5em}
\begin{tikzpicture}[]
\matrix (m) [matrix of math nodes, nodes in empty cells,
nodes={inner sep=0pt,  outer sep=0pt, anchor=center, 
circle, draw, minimum size=\MinSize,
}, 
column sep=1.5em, row sep=\RowSep, 
]{
1 & 2  & 3 & 4 & 5 & 6 & 7 & 8  \\ % Help
1 & 1s   &        &                  \\
1 & 2s   &  2p  &                    \\
1 & 3s   &  3p  & 3d                \\
1 & 2  & 3 & 4 & 5 & 6 & 7 & 8  \\ % Help
};

\draw[red] (m-1-3.north east) --  (m-3-1.south west);
\draw[red] (m-1-4.north east) --  (m-4-1.south west);
\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述 我提出了一个基本的构造:关键思想是使用序列枚举具有整数坐标的点(参见第二张图)

1 | 2 | 3 4 | 5 6 | 7 8 9 | 10 11 12 | ...

第二幅图说明了此序列(索引写为相应点上方的节点)。字符串的构造基​​于嵌套循环。

在此处输入图片描述

第一个绘图的代码

\documentclass[11pt, border=.8cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math}

\begin{document}

\tikzmath{
  real \a;
  \a = sqrt(2)/4;
  integer \N, \M;
  \N = 5;
  \M = 2*\N +1;
}
\begin{tikzpicture}[scale=1.4, y={(0, -1)},
  every node/.style={draw, red, circle, thin, font=\bfseries,
    text width=4ex, align=center, scale=.8, inner sep=0pt
  }]
  % horizontal elements
  \path (0, \N +1.1) node[black, font=\mdseries] {0}
  \foreach \i in {1, ..., \N} {-- ++(1, 0) node[black, font=\mdseries] {\i}};
  % vertical elements
  \path (-.75, 0) node[black, font=\mdseries] {0}
  \foreach \i in {1, ..., \N} {-- ++(0, 1) node[black, font=\mdseries] {\i}};
  
  % the string
  \begin{scope} 
    \clip (-1, -.5) rectangle (\N +.9, \N +.75);
    \draw[gray!60, very thin] (0, 0) grid ++(\N+1, \N+2);

    % thicker parts of the string
    \foreach \k in {0, ..., \M}{
      \draw[black, ultra thick]
      ($(0, \k)+(135: \a)$) -- ++(-45: {2*\k*\a +2*\a});
    }

    \draw[red, thick] (-45: \a)
    -- (0, 0)
    -- ++(135: \a) .. controls +(-.2, .2) and +(-.2, .2) .. ++(45: \a)
    -- ++($.5*(1, -1)$)
    .. controls +(.2, -.2) and +(.2, -.2) .. ++(45: \a) -- ++(135: \a)
    -- ++($.5*(-1, 1)$)
    \foreach \k in {1, ..., \N}{
      -- ++(135: \a) .. controls +(-.2, .2) and +(-.2, .2) .. ++(45: \a)
      -- ++($\k*(1, -1)$)
      .. controls +(.2, -.2) and +(.2, -.2) .. ++(45: \a) -- ++(135: \a)
      \foreach \i in {1, ..., \k}{-- ++(-1, 1)}
      -- ++(135: \a) .. controls +(-.2, .2) and +(-.2, .2) .. ++(45: \a)
      -- ++(${(\k +.5)}*(1, -1)$)
      .. controls +(.2, -.2) and +(.2, -.2) .. ++(45: \a) -- ++(135: \a)
      -- ++(-.5, .5)
      \foreach \i in {1, ..., \k}{-- ++(-1, 1)}
    };
  \end{scope}

  % nodes of the string
  \foreach \s [count=\j from 0] in {a, b, c, d, e, f}{
    \foreach \k in {\j, ..., 5}{
      {
        \path (\j, \k)
        node[fill=white] {\s};
      }
    }
  }
\end{tikzpicture}
\end{document}

第二幅图的代码

\documentclass[11pt, border=.8cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math}

\begin{document}

\tikzmath{
  real \a;
  \a = sqrt(2)/4;
  integer \N;
  \N = 5;
}
\begin{tikzpicture}[scale=1.4, y={(0, -1)},
  every node/.style={draw, blue, circle, fill=blue, font=\bfseries,
    text=white, text width=4.5ex, scale=.8, align=center, inner sep=0pt
  }]
  % horizontal elements
  \path (0, \N +1.1) node[fill=white, text=blue] {0}
  \foreach \i in {1, ..., \N} {
    -- ++(1, 0) node[fill=white, text=blue] {\i}
  };
  % vertical elements
  \path (-.75, 0) node[fill=white, text=blue] {0}
  \foreach \i in {1, ..., \N} {
    -- ++(0, 1) node[fill=white, text=blue] {\i}
  };
  % the string
  \begin{scope} 
    \clip (-1, -.5) rectangle (\N +.9, \N +.75);
    \draw[gray!60, very thin] (0, 0) grid ++(\N+1, \N+2);

    \draw[red, thick] (-45: \a)
    -- (0, 0) node {1}
    -- ++(135: \a) .. controls +(-.2, .2) and +(-.2, .2) .. ++(45: \a)
    -- ++($.5*(1, -1)$)
    .. controls +(.2, -.2) and +(.2, -.2) .. ++(45: \a) -- ++(135: \a)
    -- ++($.5*(-1, 1)$) node {2}
    \foreach \k [evaluate=\k as \inj using {int(\k*(\k +1) +1)},
    evaluate=\k as \ink using {int((\k +1)*(\k +1) +1)}]
    in {1, ..., \N}{
      -- ++(135: \a) .. controls +(-.2, .2) and +(-.2, .2) .. ++(45: \a)
      -- ++($\k*(1, -1)$)
      .. controls +(.2, -.2) and +(.2, -.2) .. ++(45: \a) -- ++(135: \a)
      node {\inj}
      \foreach \i [evaluate=\i as \tmp using {int(\i+\inj)}]
      in {1, ..., \k}{-- ++(-1, 1) node {\tmp}}
      -- ++(135: \a) .. controls +(-.2, .2) and +(-.2, .2) .. ++(45: \a)
      -- ++(${(\k +.5)}*(1, -1)$)
      .. controls +(.2, -.2) and +(.2, -.2) .. ++(45: \a) -- ++(135: \a)
      -- ++(-.5, .5) node {\ink}
      \foreach \i [evaluate=\i as \tmp using {int(\i+\ink)}]
      in {1, ..., \k}{-- ++(-1, 1) node {\tmp}}
    };
  \end{scope}
\end{tikzpicture}
\end{document}

我让你为每个节点选择适当的标签。

相关内容