![当我想要在 Tikz 中使用 plot 绘制曲线时,出现了一条直线](https://linux22.com/image/408982/%E5%BD%93%E6%88%91%E6%83%B3%E8%A6%81%E5%9C%A8%20Tikz%20%E4%B8%AD%E4%BD%BF%E7%94%A8%20plot%20%E7%BB%98%E5%88%B6%E6%9B%B2%E7%BA%BF%E6%97%B6%EF%BC%8C%E5%87%BA%E7%8E%B0%E4%BA%86%E4%B8%80%E6%9D%A1%E7%9B%B4%E7%BA%BF.png)
我想要绘制的是如下图两个圆柱的相交曲线,但是画出来的却是一条直线,为了简单起见,只贴出绘制曲线的基本代码。
\documentclass{border=10pt}[standalone]
\usepackage{tikz}
\begin{document}
\tikzset{%
math3d/.style={%%
x={(-0.353cm,-0.353cm)},
z={(0cm,1cm)},
y={(1cm,0cm)},
}%%
}%
\begin{tikzpicture}[math3d]
\draw node[left] at (0,0,0) {$O$} coordinate (O);
\draw[->](0,0,0)--(4,0,0);
\draw node[below left] at (4,0,0) {$X$};
\draw[->](0,0,0)--(0,5,0);
\draw node[below right] at (0,5,0) {$Y$};
\draw[->](0,0,0)--(0,0,5);
\draw node[below right] at (0,0,5) {$Z$};
% curve as intersection of two cylinders
\draw[variable=\t,samples=30,domain=0:0.5*pi,smooth]
plot ({2*cos(deg(\t))}, {2*sin(deg(\t))},{2*sin(deg(\t))}); %
\end{tikzpicture}
\end{document}
答案1
你选择了一个不合适的投影,曲线看起来像一条线。曲线由以下公式给出
\draw[variable=\t,samples=30,domain=0:0.5*pi,smooth]
plot ({2*cos(deg(\t))}, {2*sin(deg(\t))},{2*sin(deg(\t))});
因此它的y
和z
分量相等。但是,您选择的x
是,使得x
与成比例y+z
,这样您就得到了一条线。为了安装另一个投影,您可以加载库perspective
。以下动画显示,对于某些视角,曲线的投影可能看起来像一条线。
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{perspective}
\begin{document}
\foreach \X in {100,105,...,130,125,120,...,105}
{\begin{tikzpicture}
\path[use as bounding box] (-3,-3) rectangle (6,6);
\begin{scope}[3d view={\X }{20+15*sin((\X-100)*6)}]
\draw node[left] at (0,0,0) {$O$} coordinate (O);
\draw[->](0,0,0)--(4,0,0);
\draw node[below left] at (4,0,0) {$X$};
\draw[->](0,0,0)--(0,5,0);
\draw node[below right] at (0,5,0) {$Y$};
\draw[->](0,0,0)--(0,0,5);
\draw node[below right] at (0,0,5) {$Z$};
% curve as intersection of two cylinders
\draw[dashed] (2,0,0) -- (2,0,4) -- (0,0,4) -- (0,2,4)
-- (0,2,2) -- (0,4,2) -- (0,4,0) -- (2,4,0) -- cycle
plot[variable=\t,samples=30,domain=0:0.5*pi,smooth]
({2*sin(deg(\t))},4,{2*cos(deg(\t))});
\draw
plot[variable=\t,samples=30,domain=0:0.5*pi,smooth]
({2*cos(deg(\t))}, {2*sin(deg(\t))},{2*sin(deg(\t))});
\draw plot[variable=\t,samples=30,domain=0:0.5*pi,smooth]
({2*cos(deg(\t))}, 0,{2*sin(deg(\t))}) --
(0,2,2) -- (0,2,0)
plot[variable=\t,samples=30,domain=0.5*pi:0,smooth]
({2*cos(deg(\t))},{2*sin(deg(\t))},0);
\end{scope}
\end{tikzpicture}}
\end{document}