如何将图像实现为 TikZ 图片?

如何将图像实现为 TikZ 图片?

为了进一步了解 Tikz,我不断努力,尝试将 RAID 系统数据流可视化。;-)

我尝试将至少圆柱体“转换”为pic,但完全失败了。现在有一个简单的“LaTeX”解决方案:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\newcommand*\stapel[4]%
{%
    \draw (0+#1,0) ellipse (1.25 and 0.5);%
    \draw (-1.25+#1,0) -- (-1.25+#1,-3);%
    \draw (-1.25+#1,-3) arc (180:360:1.25 and 0.5);%
    \draw (-1.25+#1,-2) arc (180:360:1.25 and 0.5);%
    \draw (-1.25+#1,-1) arc (180:360:1.25 and 0.5);%
    \draw (1.25+#1,-3) -- (1.25+#1,0);%
    \node at (0+#1,-1) {#2};%
    \node at (0+#1,-2) {#3};%
    \node at (0+#1,-3) {$\vdots$};%
    \coordinate (#4) at (0+#1,0);%
}%
\newcommand*\RLLink[3]%
{%
  \foreach \x[count=\xi] in {#1}%
  {%
    \ifnum\xi=1%
      \xdef\firstnode{\x}%
    \else%
      \xdef\lastnode{\x}%
    \fi%
  }%
  \coordinate (fnl) at ([yshift=1cm]\firstnode);
  \coordinate (lnl) at ([yshift=1cm]\lastnode);
  \draw (fnl) -- (lnl);
  \foreach \x in {#1}%
  {%
    \draw (\x) -- ++(0,1);
  }%
  \coordinate (anchorb) at ($(fnl)!0.5!(lnl)$);
  \coordinate (anchort) at ([yshift=1cm]anchorb);
  \draw (anchorb) -- (anchort);
  \node[left] at (anchort) {#2};
  \node[above right] at (anchorb) {#3};
}%
\makeatother
\begin{document}
\begin{tikzpicture}
\stapel{0}{$D_{11}$}{$D_{21}$}{a};
\stapel{4}{$D_{12}$}{$D_{22}$}{b};
\RLLink{a,b}{$D_1$}{RL0}
\end{tikzpicture}

\begin{tikzpicture}
\stapel{0}{$D_{1}$}{$D_{2}$}{a};
\stapel{4}{$D_{1}$}{$D_{2}$}{b};
\RLLink{a,b}{$D_1$}{RL1}
\end{tikzpicture}

\begin{tikzpicture}
\stapel{0}{$D_{11}$}{$P_{O2}$}{a};
\stapel{3}{$D_{12}$}{$D_{21}$}{b};
\stapel{6}{$P_{1}$}{$D_{22}$}{c};
\stapel{9}{$P_{O1}$}{$P_{2}$}{d};
\RLLink{a,b,c,d}{$D_1$}{RL6}
\end{tikzpicture}
\end{document}

RAID 系统

是否至少可以部分地使用pics 或任何其他 Tikzy 技术来实现这一点?

答案1

我重点关注了“转换\stapel为图片”部分。您可以在图片中定义坐标,然后可以通过 访问它们<pic name><coordinate name>,这就是它的s1-top作用所在。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{pics/.cd,
  stapel/.style n args={2}{
    code={\draw (0,0) ellipse (1.25 and 0.5);%
    \draw (-1.25,0) -- (-1.25,-3);%
    \draw (-1.25,-3) arc (180:360:1.25 and 0.5);%
    \draw (-1.25,-2) arc (180:360:1.25 and 0.5);%
    \draw (-1.25,-1) arc (180:360:1.25 and 0.5);%
    \draw (1.25,-3) -- (1.25,0);%
    \node at (0,-1) {#1};%
    \node at (0,-2) {#2};%
    \node at (0,-3) {$\vdots$};%
    \coordinate (-top) at (0,0);%
    }}}

\newcommand*\RLLink[3]%
{%
  \foreach \x[count=\xi] in {#1}%
  {%
    \ifnum\xi=1%
      \xdef\firstnode{\x}%
    \else%
      \xdef\lastnode{\x}%
    \fi%
  }%
  \coordinate (fnl) at ([yshift=1cm]\firstnode);
  \coordinate (lnl) at ([yshift=1cm]\lastnode);
  \draw (fnl) -- (lnl);
  \foreach \x in {#1}%
  {%
    \draw (\x) -- ++(0,1);
  }%
  \coordinate (anchorb) at ($(fnl)!0.5!(lnl)$);
  \coordinate (anchort) at ([yshift=1cm]anchorb);
  \draw (anchorb) -- (anchort);
  \node[left] at (anchort) {#2};
  \node[above right] at (anchorb) {#3};
}%
\begin{document}
\begin{tikzpicture}
\pic  at (0,0) (s1) {stapel={$D_{11}$}{$D_{21}$}};
\pic  at (4,0) (s2) {stapel={$D_{12}$}{$D_{22}$}};
\RLLink{s1-top,s2-top}{$D_1$}{RL0}
%
\pic  at (0,-6) (s3) {stapel={$D_{1}$}{$D_{2}$}};
\pic  at (4,-6) (s4) {stapel={$D_{1}$}{$D_{2}$}};
\RLLink{s3-top,s4-top}{$D_1$}{RL1}
%
\pic  at (0,-12) (s5) {stapel={$D_{11}$}{$P_{O2}$}};
\pic  at (3,-12) (s6) {stapel={$D_{12}$}{$D_{21}$}};
\pic  at (6,-12) (s7) {stapel={$P_{1}$}{$D_{22}$}};
\pic  at (9,-12) (s8) {stapel={$P_{O1}$}{$P_{2}$}};
\RLLink{s5-top,s6-top,s7-top,s8-top}{$D_1$}{RL6}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容