动画图形着色

动画图形着色

我正在尝试通过使用动画来展示我的论文结果。我为要做的事情构建了一个 MWE。本质上,我有一堆顶点,它们的填充颜色由我在 CSV 表中记录的每个时间点的采样 RGB 值给出。我想通过动画展示图形顶点随时间的演变。但是我不太确定最好的方法。这就是为什么我想听听你对这个话题的意见/建议。以下代码生成一个有三个顶点的图形。

\documentclass{standalone}
\usepackage{tikz,calc}

\begin{document}
   \begin{tikzpicture}[align=center,node distance=5cm]
     \tikzset{
     vertex/.style={draw,circle, line width=3pt,minimum size=1cm,text width=1cm, align=center, font=\Large},
     arc/.style={draw, line width = 3pt,-}}
     % outer shell
     \node[vertex] (V1) at (0,0) {1};
     \node[vertex] (V2) [below right of = V1] {2};
     \node[vertex] (V3) [above of = V2] {3};

     % adjacency
     \draw[arc] (V1) -- (V2);
     \draw[arc] (V3) -- (V2);
     \draw[arc] (V3) -- (V1);
   \end{tikzpicture}
\end{document}

此外,我有一个包含以下条目的 CSV 表:

r1,g1,b1,r2,g2,b2,r3,g3,b3
0.8147,0.1576,0.6557,0.7060,0.4387,0.2760,0.7513,0.8407,0.3517
0.9058,0.9706,0.0357,0.0318,0.3816,0.6797,0.2551,0.2543,0.8308
0.1270,0.9572,0.8491,0.2769,0.7655,0.6551,0.5060,0.8143,0.5853
0.9134,0.4854,0.9340,0.0462,0.7952,0.1626,0.6991,0.2435,0.5497
0.6324,0.8003,0.6787,0.0971,0.1869,0.1190,0.8909,0.9293,0.9172
0.0975,0.1419,0.7577,0.8235,0.4898,0.4984,0.9593,0.3500,0.2858
0.2785,0.4218,0.7431,0.6948,0.4456,0.9597,0.5472,0.1966,0.7572
0.5469,0.9157,0.3922,0.3171,0.6463,0.3404,0.1386,0.2511,0.7537
0.9575,0.7922,0.6555,0.9502,0.7094,0.5853,0.1493,0.6160,0.3804
0.9649,0.9595,0.1712,0.0344,0.7547,0.2238,0.2575,0.4733,0.5678

r1,g1,b1 是节点 1 的 rgb 值,依此类推。

感谢您的所有帮助!

编辑:如果可能的话,我想使用 RGB 填充选项:

\node[...,fill={rgb:red,4;green,2;blue,1}]

答案1

此解决方案通过 LaTeX3 函数从文件中读取带颜色的行。此外,它还利用了新命令\multiframebreak来提前跳出循环\multiframe。( animate[ 2020/08/29])

\documentclass{standalone}

\usepackage[T1]{fontenc}
\usepackage{tikz,calc,animate}

\begin{filecontents}[overwrite,noheader]{vertexcolours.dat}
r1,g1,b1,r2,g2,b2,r3,g3,b3
0.8147,0.1576,0.6557,0.7060,0.4387,0.2760,0.7513,0.8407,0.3517
0.9058,0.9706,0.0357,0.0318,0.3816,0.6797,0.2551,0.2543,0.8308
0.1270,0.9572,0.8491,0.2769,0.7655,0.6551,0.5060,0.8143,0.5853
0.9134,0.4854,0.9340,0.0462,0.7952,0.1626,0.6991,0.2435,0.5497
0.6324,0.8003,0.6787,0.0971,0.1869,0.1190,0.8909,0.9293,0.9172
0.0975,0.1419,0.7577,0.8235,0.4898,0.4984,0.9593,0.3500,0.2858
0.2785,0.4218,0.7431,0.6948,0.4456,0.9597,0.5472,0.1966,0.7572
0.5469,0.9157,0.3922,0.3171,0.6463,0.3404,0.1386,0.2511,0.7537
0.9575,0.7922,0.6555,0.9502,0.7094,0.5853,0.1493,0.6160,0.3804
0.9649,0.9595,0.1712,0.0344,0.7547,0.2238,0.2575,0.4733,0.5678
\end{filecontents}

\usepackage{expl3}
\ExplSyntaxOn

\ior_new:N \l_colours_stream % file descriptor
\seq_new:N \g_colours_seq    % sequence var taking colours at a given time

\ior_open:Nn \l_colours_stream {vertexcolours.dat} % open file for reading
\ior_str_get:NN \l_colours_stream \l_tempa_tl % read table header and throw away

\cs_new:Npn\readColours{
  \seq_gclear:N \g_colours_seq
  \ior_str_get:NNT \l_colours_stream \l_tempa_tl{
    \seq_gset_split:NnV \g_colours_seq {,} \l_tempa_tl
  }
}

\cs_new:Npn\getColourByIndex#1{
  \seq_item:Nn \g_colours_seq {#1}
}

\ExplSyntaxOff

%\makeatletter
%\def\multiframebreak{\global\@anim@mulframecnt=1000}
%\makeatother

\begin{document}

\begin{animateinline}[controls,autoplay,loop]{1}
  \readColours%
  \multiframe{100}{}{
    \begin{tikzpicture}[align=center,node distance=5cm]
      \tikzset{
      vertex/.style={draw,fill=temp,circle, line width=3pt,minimum size=1cm,text width=1cm, align=center, font=\Large},
      arc/.style={draw, line width = 3pt,-}}
      % outer shell
      \definecolor{temp}{rgb}{\getColourByIndex{1},\getColourByIndex{2},\getColourByIndex{3}}
      \node[vertex] (V1) at (0,0) {1};
      \definecolor{temp}{rgb}{\getColourByIndex{4},\getColourByIndex{5},\getColourByIndex{6}}
      \node[vertex] (V2) [below right of = V1] {2};
      \definecolor{temp}{rgb}{\getColourByIndex{7},\getColourByIndex{8},\getColourByIndex{9}}
      \node[vertex] (V3) [above of = V2] {3};

      % adjacency
      \draw[arc] (V1) -- (V2);
      \draw[arc] (V3) -- (V2);
      \draw[arc] (V3) -- (V1);
    \end{tikzpicture}%
    \readColours%
    \ifthenelse{\equal{\getColourByIndex{1}}{}}{%
      \multiframebreak%    % no more colours -> break
    }{}%
  }  
\end{animateinline}
\end{document}

相关内容