如何使填充图案适合 tikz 中的 z 轴?

如何使填充图案适合 tikz 中的 z 轴?

您好,如下图所示,右侧是输出的图像,左侧是我想要的手绘图像。如何使 yz 平面(水平)上的正弦/余弦曲线的填充图案在 yz 平面上垂直,而不是在 xy 平面上垂直

右侧为输出图像,左侧为 yz 平面中带有线条的所需图像

以下是代码,您可以查看

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}[xscale=1]
\draw[->] (0,0,0) -- (10.5,0,0);
\draw[->] (0,0,0) -- (0,1.5,0);
\draw[->] (0,0,0) -- (0,0,1.5);
\draw[pattern=vertical lines] (0,0,0) -- (0,1,0) cos (2,0,0) sin (3,-1,0) cos (4,0,0) sin (5,1,0) cos (6,0,0) sin (7,-1,0) cos (8,0,0) sin (9,1,0) cos (10,0,0);
\draw[pattern=vertical lines] (0,0,0) -- (0,0,1) cos (2,0,0) sin (3,0,-1) cos (4,0,0) sin (5,0,1) cos (6,0,0) sin (7,0,-1) cos (8,0,0) sin (9,0,1) cos (10,0,0);
\end{tikzpicture}
\end{document}

编辑:除了使用东北线之外的其他方法,这样如果我决定旋转视图,它仍然会正确显示

答案1

这是 Zarko 的一个很好的答案的替代方案,它具有更短的代码和更像波浪的波,即第一个最大值与其他最大值一样宽。

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\draw[->] (0,0,0) -- (10.5,0,0);
\draw[->] (0,0,0) -- (0,1.5,0);
\draw[->] (0,0,0) -- (0,0,1.5);
\draw[pattern=vertical lines] (0,0,0) -- plot[variable=\x,domain=0:10,samples=72] (\x,{cos(72*\x)},0)
-- (10,0,0) -- cycle;
\draw[pattern=north east lines] (0,0,0) -- plot[variable=\x,domain=0:10,samples=72] (\x,0,{cos(72*\x)}) -- (10,0,0) -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:刚刚在 Zarko 的精彩回答下看到了您的评论。Zarko 可能指的是软件包tikz-3dplot,链接是正确的,这里有一个动画来说明他的观点。

\documentclass[tikz,margin=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{patterns}

\begin{document}
\foreach \X in {0,5,...,355}{
\tdplotsetmaincoords{70}{\X}
\begin{tikzpicture}
\path[use as bounding box] (-5.5,-3) rectangle (5.5,3);
\begin{scope}[tdplot_main_coords,scale=0.5]
\draw[->] (0,0,0) -- (10.5,0,0);
\draw[->] (0,0,0) -- (0,1.5,0);
\draw[->] (0,0,0) -- (0,0,1.5);
\draw[pattern=vertical lines] (0,0,0) -- plot[variable=\x,domain=0:10,samples=72] (\x,{cos(72*\x)},0)
-- (10,0,0) -- cycle;
\draw[pattern=north east lines] (0,0,0) -- plot[variable=\x,domain=0:10,samples=72] (\x,0,{cos(72*\x)}) -- (10,0,0) -- cycle;
\end{scope}
\end{tikzpicture}%
}
\end{document}

在此处输入图片描述

为了有更灵活的轮换,你可能需要同时查看这个问题

第二次编辑:如果没有图案,它甚至可能看起来更好一些。

\documentclass[tikz,margin=3.14mm]{standalone}
\usepackage{tikz-3dplot}

\begin{document}
\foreach \X in {0,5,...,355}{
\tdplotsetmaincoords{70}{\X}
\begin{tikzpicture}
\path[use as bounding box] (-5.5,-3) rectangle (5.5,3);
\begin{scope}[tdplot_main_coords,scale=0.5]
\draw[->] (0,0,0) -- (10.5,0,0);
\draw[->] (0,0,0) -- (0,1.5,0);
\draw[->] (0,0,0) -- (0,0,1.5);
\draw (0,0,0) -- plot[variable=\x,domain=0:10,samples=72,smooth] (\x,{cos(72*\x)},0)
-- (10,0,0) -- cycle;
\draw (0,0,0) -- plot[variable=\x,domain=0:10,samples=72,smooth] (\x,0,{cos(72*\x)}) -- (10,0,0) -- cycle;
\foreach \Y in {0,0.2,...,10}
{\draw[thin] (\Y,0,0) -- (\Y,{cos(72*\Y)},0);
\draw[thin] (\Y,0,0) -- (\Y,0,{cos(72*\Y)});}
\end{scope}
\end{tikzpicture}%
}
\end{document}

在此处输入图片描述

答案2

够接近了吗?

在此处输入图片描述

有关可用的模式,请参见“TikZ 和 PGF 手册,3.0.1a”,第 666 页:还列出了 < north east lines,它与您要查找的内容关闭:

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\draw[->] (0,0,0) -- (10.5,0,0);
\draw[->] (0,0,0) -- (0,1.5,0);
\draw[->] (0,0,0) -- (0,0,1.5);
\draw[pattern=vertical lines] (0,0,0) -- (0,1,0) cos (2,0,0) sin (3,-1,0) cos (4,0,0) sin (5,1,0) cos (6,0,0) sin (7,-1,0) cos (8,0,0) sin (9,1,0) cos (10,0,0);
\draw[pattern=north east lines] (0,0,0) -- (0,0,1) cos (2,0,0) sin (3,0,-1) cos (4,0,0) sin (5,0,1) cos (6,0,0) sin (7,0,-1) cos (8,0,0) sin (9,0,1) cos (10,0,0);
\end{tikzpicture}
\end{document}

答案3

我定义了三种新的模式,、、x linesy linesz lines它们可以与该tikz-3dplot包很好地配合使用(实际上,这些模式要正常运行tikz-3dplot至少需要命令)。\tdplotsetmaincoords

您可以检查代码以了解其工作原理,但不要犹豫,问我:)

PS:我还使用了@marmot 绘制正弦波的方法,以便它们在 3D 中旋转时仍然保持美观。

\documentclass[tikz,margin=2mm]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{patterns}

\makeatletter
\newlength\zlines@x
\newlength\zlines@y
\newlength\ylines@x
\newlength\ylines@y
\newlength\xlines@x
\newlength\xlines@y

\pgfmathsetmacro\linesep{5}

\tikzset{
    x lines vector/.code={
        \pgfextractx\xlines@x{#1}
        \pgfextracty\xlines@y{#1}
        \pgfmathparse{ifthenelse(\xlines@x == 0 || abs(\xlines@x) < abs(\xlines@y) || \xlines@y == 0,"\linesep pt","\linesep*\xlines@x/\xlines@y")}
        \pgfmathsetlengthmacro\xlines@width{\pgfmathresult}
        \pgfmathparse{ifthenelse(\xlines@x == 0 || abs(\xlines@x) > abs(\xlines@y) || \xlines@y == 0,"\linesep pt","\linesep*\xlines@y/\xlines@x")}
        \pgfmathsetlengthmacro\xlines@height{\pgfmathresult}
        \pgfmathsetlengthmacro\xlines@vector@x{10*\xlines@x}
        \pgfmathsetlengthmacro\xlines@vector@y{10*\xlines@y}
    },
    y lines vector/.code={
        \pgfextractx\ylines@x{#1}
        \pgfextracty\ylines@y{#1}
        \pgfmathparse{ifthenelse(\ylines@x == 0 || abs(\ylines@x) < abs(\ylines@y) || \ylines@y == 0,"\linesep pt","\linesep*\ylines@x/\ylines@y")}
        \pgfmathsetlengthmacro\ylines@width{\pgfmathresult}
        \pgfmathparse{ifthenelse(\ylines@x == 0 || abs(\ylines@x) > abs(\ylines@y) || \ylines@y == 0,"\linesep pt","\linesep*\ylines@y/\ylines@x")}
        \pgfmathsetlengthmacro\ylines@height{\pgfmathresult}
        \pgfmathsetlengthmacro\ylines@vector@x{10*\ylines@x}
        \pgfmathsetlengthmacro\ylines@vector@y{10*\ylines@y}
    },
    z lines vector/.code={
        \pgfextractx\zlines@x{#1}
        \pgfextracty\zlines@y{#1}
        \pgfmathparse{ifthenelse(\zlines@x == 0 || abs(\zlines@x) < abs(\zlines@y) || \zlines@y == 0,"\linesep pt","\linesep*\zlines@x/\zlines@y")}
        \pgfmathsetlengthmacro\zlines@width{\pgfmathresult}
        \pgfmathparse{ifthenelse(\zlines@x == 0 || abs(\zlines@x) > abs(\zlines@y) || \zlines@y == 0,"\linesep pt","\linesep*\zlines@y/\zlines@x")}
        \pgfmathsetlengthmacro\zlines@height{\pgfmathresult}
        \pgfmathsetlengthmacro\zlines@vector@x{10*\zlines@x}
        \pgfmathsetlengthmacro\zlines@vector@y{10*\zlines@y}
    },
    x/.forward to=/tikz/x lines vector,
    y/.forward to=/tikz/y lines vector,
    z/.forward to=/tikz/z lines vector,
}

\pgfdeclarepatternformonly[\xlines@vector@x,\xlines@vector@y,\xlines@width,\xlines@height]{x lines}
{\pgfpoint{-0.6*\xlines@width}{-0.6*\xlines@height}}
{\pgfpoint{0.6*\xlines@width}{0.6*\xlines@height}}
{\pgfpoint{\xlines@width}{\xlines@height}}
{
    \pgfpathmoveto{\pgfpoint{-\xlines@vector@x}{-\xlines@vector@y}}
    \pgfpathlineto{\pgfpoint{\xlines@vector@x}{\xlines@vector@y}}
    \pgfsetlinewidth{0.3pt}
    \pgfusepath{stroke}
}
\pgfdeclarepatternformonly[\ylines@vector@x,\ylines@vector@y,\ylines@width,\ylines@height]{y lines}
{\pgfpoint{-0.6*\ylines@width}{-0.6*\ylines@height}}
{\pgfpoint{0.6*\ylines@width}{0.6*\ylines@height}}
{\pgfpoint{\ylines@width}{\ylines@height}}
{
    \pgfpathmoveto{\pgfpoint{-\ylines@vector@x}{-\ylines@vector@y}}
    \pgfpathlineto{\pgfpoint{\ylines@vector@x}{\ylines@vector@y}}
    \pgfsetlinewidth{0.3pt}
    \pgfusepath{stroke}
}
\pgfdeclarepatternformonly[\zlines@vector@x,\zlines@vector@y,\zlines@width,\zlines@height]{z lines}
{\pgfpoint{-0.6*\zlines@width}{-0.6*\zlines@height}}
{\pgfpoint{0.6*\zlines@width}{0.6*\zlines@height}}
{\pgfpoint{\zlines@width}{\zlines@height}}
{
    \pgfpathmoveto{\pgfpoint{-\zlines@vector@x}{-\zlines@vector@y}}
    \pgfpathlineto{\pgfpoint{\zlines@vector@x}{\zlines@vector@y}}
    \pgfsetlinewidth{0.3pt}
    \pgfusepath{stroke}
}
\makeatother

\begin{document}
    \foreach \rotation in {30,50,...,360}{
    \tdplotsetmaincoords{70}{\rotation}
    \begin{tikzpicture}
        \clip (-11,-4) rectangle (11,4);
        \tdplotsetrotatedcoords{0}{0}{0}
        \begin{scope}[xscale=1,tdplot_main_coords,tdplot_rotated_coords]
            \draw[->,red]   (0,0,0) -- (10.5,0,0);
            \draw[->,green] (0,0,0) -- (0,1.5,0);
            \draw[->,blue]  (0,0,0) -- (0,0,1.5);

            \draw[pattern=y lines] (0,0,0) -- (0,1,0)  --plot[domain=0:2.25*360,samples=30,smooth] ({10*\x/(2.25*360)},{cos(\x)},{0}) -- cycle;
            \draw[pattern=z lines] (0,0,0) -- (0,0,1)  --plot[domain=0:2.25*360,samples=30,smooth] ({10*\x/(2.25*360)},{0},{cos(\x)}) -- cycle;
        \end{scope}
    \end{tikzpicture}
}
\end{document}

在此处输入图片描述

相关内容