使用 TikZ 着色区域

使用 TikZ 着色区域

考虑以下 MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
    \foreach \x in {1,1.2,...,3}
    {
        \pgfmathsetmacro{\a}{10*\x}
        \fill[blue!\a,shift={(-1,-2)}] (\x,-.3) rectangle (\x+1,2.3);
    }
        \fill[white] (0,0) .. controls (1,1) and (1.5,-1) .. (3,0) -- (3,.4) -- (0,.4) -- cycle;
        \fill[white] (0,-2) .. controls (1,-1) and (1.5,-3) .. (3,-2) -- (3,-2.3) -- (0,-2.3) -- cycle;
            \draw (0,0) .. controls (1,1) and (1.5,-1) .. (3,0);
            \draw (0,-2) .. controls (1,-1) and (1.5,-3) .. (3,-2);
        \begin{scope}[yshift=4cm]
            \foreach \x in {0,.1,...,.8}
            {
                \pgfmathsetmacro{\a}{\x*40}
                \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- ([yshift=-1cm]$(0,0)!\x!(2,.5)$) -- ([yshift=-1cm,xshift=.5cm]$(0,0)!\x!(2,.5)$) -- ([yshift=.11cm,xshift=.5cm]$(0,0)!\x!(2,.5)$);
            }
            \draw[shorten >=.1cm,thick] (0,0) -- (2,.5);
            \begin{scope}[shift={(0,-2)}]
            \foreach \x in {0,.1,...,.9}
            {
                \pgfmathsetmacro{\a}{\x*40}
                \pgfmathsetmacro{\b}{\x+.1}
                \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- ([yshift=-1cm]$(0,0)!\x!(2,.5)$) -- ($(0,-1)!\x!(2,-.5)$) -- ($(0,-1)!\b!(2,-.5)$) -- ($(0,0)!\b!(2,.5)$) -- cycle;
            }
            \draw[shorten >=.2cm,thick] (0,0) -- (2,.5);
            \end{scope}
        \end{scope}
    \end{tikzpicture}
\end{document}

正如您在上一部分中看到的(参见第一张图片),我可以\usetikzlibrary{calc}实现对区域的方便着色。但在上一部分中(参见第二张图片),我必须手动覆盖可能无法“绘制”的区域。我calc也在这里尝试了该库,但它计算点时就像在一条直线上一样,而不是像具有某些带角的曲线一样。我的问题是:如何使用该calc库为第二部分编写更优雅的代码?

截屏


截屏

答案1

这里还有另一种选择,使用路径中的节点,定义路径中的某些点并从这些点绘制一条改变颜色的线。

结果:

在此处输入图片描述

梅威瑟:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        %Iterative tricks
        \foreach \x in {1,2,...,30}{
            \path
            (1,0) 
                .. controls +(1,1) and +(-1,-1) .. ++(3,0) node[pos=\x/30](a\x){};
            \draw[line width = 4pt,blue!\x] (a\x.center) -- ++ (0,-2);
        }       
        \draw[line width = 1pt]
        (1,0) 
            .. controls +(1,1) and +(-1,-1) .. ++(3,0)
        (1,-2) 
            .. controls +(1,1) and +(-1,-1) .. ++(3,0);
        %Another path
        \foreach \x in {1,2,...,50}{
            \path
            (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0) node[pos=\x/50](a\x){};
            \draw[line width = 3pt,blue!\x] (a\x.center) -- ++ (0,-2);
        }       
        \draw[line width = 1pt]
        (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0)
        (1,-5.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);

    \end{tikzpicture}
\end{document}

更新:

在预动作修改器中添加了另一条移位的线,并缩短了线条终止以隐藏矩形;但如果您使用某些背景,这不是一个好的解决方案;最好的选择是使用剪辑、阴影甚至矩形。

结果: 在此处输入图片描述

梅威瑟:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc,patterns}
\begin{document}
    \begin{tikzpicture}[
        ShiftLine/.style={
            preaction={
                transform canvas={
                    shift={(#1)},
                },
                draw=white,
                line width=3pt,
            }
        }
    ]
    \fill [blue!30](0.5,-8) rectangle (12.5,0.5);
    \pattern [pattern=checkerboard,pattern color=blue!10](0.5,-8) rectangle (12.5,0.5);
        %Avoid use clip
        \foreach \x in {1,2,...,49}{
            \path
            (1,0) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0) node[pos=\x/50](a\x){};
            \draw[line width = 3pt,blue!\x,shorten <=-2pt, shorten >=-2pt] (a\x.center) -- ++ (0,-2);
        }       
        \draw[line width = 1pt,ShiftLine={0,2pt},shorten <=2pt, shorten >=2pt]
        (1,0) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \draw[line width = 1pt,ShiftLine={0,-2pt},shorten <=2pt, shorten >=2pt]
        (1,-2) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);

        %using Clip and shades
        \begin{scope}
        \clip
        (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0)
                -- ++ (0,-2)
                .. controls +(-1.5,-3) and +(1,0.5) .. ++(-4.5,0)
                -- cycle;
        \shade[right color=blue!50, left color=white]
        (1,-3) rectangle ++ (4.5,-4);
                \draw[line width = 2pt]
        (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \draw[line width = 2pt]
        (1,-5.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \end{scope}

        %Using Rectangles in a foreach shifting.
        \begin{scope}[shift={(4,2)}]
        \clip
        (3,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0)
                -- ++(0,-2)
                .. controls +(-1.5,-3) and +(1,0.5) .. ++(-4.5,0)
                -- cycle;
        \foreach \k in {0,1,...,50}{
            \fill[blue!\k]
            (3+\k*0.1,-3) rectangle ++(0.5,-4);
        }
        \draw[line width=2pt]
        (3,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \draw[line width=2pt]
        (3,-5.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \end{scope}
        %Some labels
        \draw[font=\tiny,fill opacity=0.2,text opacity=1,align=center]
        (3.5,-1.8) node[anchor=center,fill=yellow]{\verb+Shift preaction white line  +\\ \verb+foreach lines from nodes in a path+}
        (3.5,-5.3) node[anchor=center,fill=yellow]{\verb+clip shape  +\\ \verb+and shades+}
        (9.5,-3.3) node[anchor=center,fill=yellow]{\verb+clip shape +\\ \verb+rectangles in \foreach shifting+};
    \end{tikzpicture}
\end{document}

答案2

我不知道你问的是不是这个,但通过使用相对坐标,我们可以大大简化阴影轮廓。这样就不需要手动移动了。

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
            \begin{scope}[shift={(0,-2)}]
            \foreach \x in {0,.1,...,.9}
            {
               \pgfmathsetmacro{\a}{\x*40}        
                \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- 
                ++ (0,-1) -- ++ ($(0,0)!0.1!(2,.5)$) -- ++(0,1) -- cycle;
            }
            \draw[shorten >=.2cm,thick] (0,0) -- (2,.5);
            \end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容