TransWikia.com

Arrow conditional color

TeX - LaTeX Asked by JeT on March 5, 2021

I’d like to create an animation where arrows change of color (from blue to red) if the node y coordinate switches from positive to negative value.

I think I complicated things when I used below= 3 pt of.

enter image description here

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}

One Answer

With simple loop and testing, if y-coordinate of end point is positive or not:

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}
{
ifnumy>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}

enter image description here

Correct answer by Zarko on March 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP