tikz 中具有不透明度渐变的路径

tikz 中具有不透明度渐变的路径

灵感来自这个问题,我想画一个无穷符号,其不透明度沿着路径发生变化,到目前为止我能够实现的是:

结果

\documentclass[tikz]{standalone}

\usetikzlibrary{decorations.markings}

\pagecolor{yellow}

\definecolor{col1}{RGB}{127,127,127}
\definecolor{col2}{RGB}{240,240,240}

\begin{document}

\begin{tikzpicture}
    \path[postaction={
        decorate,
        decoration={
            markings,
            mark=between positions 0 and \pgfdecoratedpathlength step 0.5pt with {
                \pgfmathsetmacro\myval{multiply(divide(
                    \pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, \pgfdecoratedpathlength),100)};
                \pgfsetfillcolor{col1!\myval!col2};
                \pgfpathcircle{\pgfpointorigin}{0.1cm};
                \pgfusepath{fill};}
}}] plot[smooth cycle] coordinates{(0,0.7)(-0.2,0.35)(0,0)(1,0.7)(1.2,0.35)(1,0)};
\end{tikzpicture}

\end{document}

但它存在两个问题:

(1)这被淹没了颜色渐变,不是不透明度渐变,因此例如如果我在黄色背景的纸张上使用这张图片,它仍然是灰色和白色,而不是灰色和黄色。

(2)如您所见,路径的边缘并不平滑,边缘看起来就像某种波浪。(我认为这是因为颜色渐变是通过绘制许多圆圈来实现的,因此线条看起来不太直。但是除了减少之外,我不知道如何解决这个问题step 0.5pt,但如果我这样做,绘制过程会非常慢。)

边缘

有没有更简单的方法可以正确绘制这幅图?

答案1

我绘制了两种渐变的符号,并尝试将它们排列在右侧。

这个解决方案显然不是最佳的。褪色不是沿着路径,而是从西到东。线条在右侧相交的地方存在颜色差异和渲染伪影。线条相交的地方存在问题 - 如果只是透明度,可以用 来解决transparency group

\documentclass[tikz]{standalone}
\usetikzlibrary {fadings, patterns}
\begin{document}
\begin{tikzpicture}
\pattern[pattern=checkerboard,pattern color=yellow](-1,-0.5) rectangle (1,0.5);
\begin{scope}
  \clip(-1,-0.5)--(0,-0.5)--(0.5,0)--(1,0)--(1,0.5)--(0,0.5)--(-0.5,0)--(-1,0)--cycle;
  \draw[darkgray, line width=5.5, path fading=west, fading transform={xscale=2, xshift=10},  line cap=round] plot[smooth] coordinates{(-0.7,0.02)(-0.5,-0.35)(0.5,0.35)(0.7,-0.02)};
\end{scope}
\begin{scope}
  \clip (-1,-0.5)--(1,-0.5)--(1,0)--(0.5,0)--(0,0.5)--(-1,0.5)--cycle;
  \draw[darkgray, line width=5.5, path fading=east, fading transform={xscale=2, xshift=10},  line cap=round] plot[smooth] coordinates{(-0.7,0)(-0.5,0.35)(0.5,-0.35)(0.7,0.02)}; 
\end{scope}
\end{tikzpicture}
\end{document}

褪色的无穷大符号


编辑:

这只是为了证明有可能将一条路径完全从另一条路径中切断

\documentclass[tikz]{standalone}
\usetikzlibrary{fadings, patterns}
\begin{tikzfadingfrompicture}[name=myfading]
\clip(-1,-0.5)--(0,-0.5)--(0.5,0)--(1,0)--(1,0.5)--(0,0.5)--(-0.5,0)--(-1,0)--cycle;
\draw[white, line width=5.5, path fading=west, fading transform={xscale=2, xshift=10},  line cap=round] plot[smooth] coordinates{(-0.7,0.02)(-0.5,-0.35)(0.5,0.35)(0.7,-0.02)};
\draw[line width=5.5, line cap=round] plot[smooth] coordinates{(-0.7,0)(-0.5,0.35)(0.5,-0.35)(0.7,0.02)}; 
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\pattern[pattern=checkerboard,pattern color=yellow](-1,-0.5) rectangle (1,0.6);
\fill[path fading=myfading, left color = white, right color = gray] (-1,-1) rectangle (1,1);
\end{tikzpicture}
\end{document}

中间交叉点被切掉的半个无穷大符号

这可以解决重叠问题,但每当我尝试使用时tikzfadingfrompicture,由于我缺乏理解,我就会遇到奇怪的缩放问题。-参见https://tex.stackexchange.com/a/551723/8650

相关内容