箭头条件颜色

箭头条件颜色

我想创建一个动画,当节点\y坐标从正值变为负值时,箭头的颜色会改变(从蓝色变为红色)。

我认为当我使用时事情变得复杂了below= 3 pt of

在此处输入图片描述

\documentclass[tikz]{standalone}

\usetikzlibrary{arrows.meta}

\tikzset{
fleche/.style={-latex},
dot/.style = {circle, fill, minimum size=#1,
              inner sep=0pt, outer sep=0pt,fill=red},
dot/.default = 6pt  
  }       


\begin{document}

\foreach \y in {1}%,...,-1.5} 
{
            
    \begin{tikzpicture}[font=\sf]
        %\draw[help lines,blue!20] (0.5, -4) grid (4.5, 4);
        \useasboundingbox (-0.5,-4) rectangle (4.5,4);
                          
        \node[dot]   (Spot)  at (0,0) {} ;
        
        \node[]   (s1)  at (4,\y)    {$S_{1}$} ;
        \node[below= 3 pt of s1] (s2) {$S_{2}$} ; 
        \node[below= 3 pt of s2] (s3) {$S_{3}$} ;
        \node[below= 3 pt of s3] (s4) {$S_{4}$} ;
        
        \draw[fleche] (Spot) -- (s1) ;
        \draw[fleche] (Spot) -- (s2) ;
        \draw[fleche] (Spot) -- (s3) ;
        \draw[fleche] (Spot) -- (s4) ;
        
    \end{tikzpicture}
            
}


\end{document}

答案1

通过简单的循环和测试,判断端点的 y 坐标是否为正:

\documentclass[border=3,141592mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\tikzset{
arr/.style = {draw=#1, -Latex},
dot/.style = {circle, fill, minimum size=#1,
              inner sep=0pt, outer sep=0pt,
              fill=red, node contents={}},
dot/.default = 6pt
        }


\begin{document}
    \begin{tikzpicture}
\node (n0) [dot];
\foreach \y [count=\i] in {9,3,...,-9}
{
\ifnum\y>0
    \draw[arr=blue] (n0) -- (3,\y/10) node[right] {$\mathsf{S}_{\i}$};
\else
    \draw[arr=red]  (n0) -- (3,\y/10) node[right] {$\mathsf{S}_{\i}$};
\fi
}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容