带颜色底纹的波浪箭头

带颜色底纹的波浪箭头

我想创建一个波浪箭头,波浪上有颜色渐变。为此,我尝试结合以下解决方案:https://tex.stackexchange.com/a/197867/96546获得颜色渐变的解决方案https://tex.stackexchange.com/a/193451/96546表示波浪箭头。

以下是 MWE:

    \documentclass[tikz, border=5]{standalone}
    \usetikzlibrary{shapes.arrows}
    \makeatletter
    \def\createshadingfromlist#1#2#3{%
        \pgfutil@tempcnta=0\relax
        \pgfutil@for\pgf@tmp:={#3}\do{\advance\pgfutil@tempcnta by1}%
        \ifnum\pgfutil@tempcnta=1\relax%
        \edef\pgf@spec{color(0)=(#3);color(100)=(#3)}%
        \else%
        \pgfmathparse{50/(\pgfutil@tempcnta-1)}\let\pgf@step=\pgfmathresult%
        %
        \pgfutil@tempcntb=1\relax%
        \pgfutil@for\pgf@tmp:={#3}\do{%
            \ifnum\pgfutil@tempcntb=1\relax%
            \edef\pgf@spec{color(0)=(\pgf@tmp);color(25)=(\pgf@tmp)}%
            \else%
            \ifnum\pgfutil@tempcntb<\pgfutil@tempcnta\relax%
            \pgfmathparse{25+\pgf@step/4+(\pgfutil@tempcntb-1)*\pgf@step}%
            \edef\pgf@spec{\pgf@spec;color(\pgfmathresult)=(\pgf@tmp)}%
            \else%
            \edef\pgf@spec{\pgf@spec;color(75)=(\pgf@tmp);color(100)=(\pgf@tmp)}%
            \fi%
            \fi%
            \advance\pgfutil@tempcntb by1\relax%
        }%
        \fi%
        \csname pgfdeclare#2shading\endcsname{#1}{100}\pgf@spec%
    }
    
    \createshadingfromlist{shading1}{vertical}{red,yellow,green,cyan,blue}
    \createshadingfromlist{shading2}{vertical}{red,yellow}
    \createshadingfromlist{shading3}{vertical}{black,blue,cyan,white}
    
    %%%
    \usetikzlibrary{decorations.shapes}
    \usetikzlibrary{shapes.geometric, arrows}
    \usetikzlibrary{patterns}
    \usepackage[compat=1.0.0]{tikz-feynman}
    \usetikzlibrary{decorations.pathreplacing,angles,quotes,decorations.pathmorphing}
    \usepackage{tkz-euclide}
    %\usetkzobj{all}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.9}
    
    \begin{document}
        \tikzset{decorate sep/.style 2 args=
            {decorate,decoration={shape backgrounds,shape=circle,shape size=#1,shape sep=#2}}}
        \begin{tikzpicture}[colorbar arrow/.style={
                shape=double arrow,
                double arrow head extend=0.125cm, 
                shape border rotate=90, 
                minimum height=5cm,
                shading=#1
            },
            wavy/.style={decorate,decoration={snake,post length=1.2mm}}
            ]
            
            \node [colorbar arrow=shading1,wavy] at (0,0) {};
    %       \node [colorbar arrow=shading2] at (1,0) {};
    %       \node [colorbar arrow=shading3] at (2,0) {};
        \end{tikzpicture}  
    \end{document}

如下图所示: 在此处输入图片描述

所以它几乎可以正常工作,但不知何故,末端不再是真正的箭头,并且波会修改箭头边界的两侧。我想创建的是类似

在此处输入图片描述

带有颜色渐变。这可以在上面的 MWE 中通过将\node行替换为\draw[red,wavy,->,>=stealth'] (5.0,0.2) -- (2.2,0.2) node [above] {};

对于这种组合中出现了什么问题或如何正确进行的任何提示,我们深表感谢!

答案1

在此处输入图片描述

我提出的解决方案是通过循环和 sin 函数同时实现波浪路径和颜色变化;不涉及任何装饰。

箭头是一个pic对象,w arrow取决于六个参数:起点和终点、“波”的振幅、(完整)周期数、起始和结束颜色。

评论。如果您需要两种以上颜色的渐变,则必须以列表形式提供颜色。修改很少。如果您需要,我们可以讨论。

代码

\documentclass[margin=10pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{math, calc}

\tikzset{%
  pics/w arrow/.style args={from=#1 to=#2, amplitude=#3, periods=#4,
    s-color=#5, e-color=#6%
  }{%
    code={%
      \tikzmath{%
        integer \nbSteps, \i, \j;
        real \d, \r, \angle, \nbPeriods, \h, \ecst, \acst;
        coordinate \S, \E;
        \nbPeriods = #4 +.75;
        \nbSteps = 25;
        \S = #1;
        \E = #2;
        \d = veclen(\Ex-\Sx, \Ey-\Sy)*1pt/1cm;
        \acst = .65;  % controls the horizontal segment supporting the arrow
        \r = \d/(\nbPeriods +\acst);
        \ecst = (\nbPeriods +.3)/(\nbPeriods +\acst);
        \angle = atan2(\Ey-\Sy, \Ex-\Sx);
        \h = \nbPeriods/\nbSteps;  % domain length for \x
        \a = 0;
        \b = \a +\h;
        for \i in {1,...,\nbSteps}{%
          \j = 4*\i;
          {%
            \draw[#6!\j!#5, line cap=round,
            domain=\a:\b, samples=15, shift={(\S)}, rotate=\angle]
            plot ({\r*\x}, {-#3*sin(360*\x)});
          };
          \a = \b;
          \b = \a +\h;
        };
        {%
          \path[shift={(\S)}, rotate=\angle]
          ({\r*\nbPeriods}, {-#3*sin(360*\nbPeriods)})
          coordinate (tmpPoint);
          \draw[#6, ->] (tmpPoint)           
          to[out=\angle, in=\angle+180] ($(\S)!\ecst!(\E)$) -- (\E);
        };
      }
    }
  }
}

\begin{document}
\begin{tikzpicture}[]
  \draw[fill=gray!20] (0, 0) circle (1.5pt) node[left] {$S$};
  \draw[fill=gray!20] (3, 1) circle (1.5pt) node[right] {$E$};

  \path pic[very thick]
  {w arrow={from=(0, 0) to=(3, 1), amplitude=.2, periods=3,
      s-color=green, e-color=blue}};
  \path pic[]
  {w arrow={from=(0, -.5) to=(4, -.5), amplitude=.1, periods=5,
      s-color=cyan, e-color=red}};
  \path pic[line width=4pt]
  {w arrow={from=(0, -.5) to=(4, -3), amplitude=.3, periods=2,
      s-color=cyan, e-color=violet}};
\end{tikzpicture}
\end{document}

相关内容