我正在尝试使用以下方法生成曲面图pgfplots
:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[declare function = {U(\x,\y)=(abs(\x)*(abs(\x)>abs(\y)) + abs(\y)*(abs(\x)<=abs(\y)) )*((abs(\x)<1) && (abs(\y)<1)) + 1*((abs(\x)>=1) || (abs(\y)>=1)) + max(2*\x-2,0) - min(2*\x+2,0) + max(2*\y-2,0) - min(2*\y+2,0);}]
\begin{axis}[
colormap name = viridis,
width = 8in,
height = 6in,
view = {25}{11},
grid = major,
domain = -2:2,
y domain = -2:2,
]
\addplot3 [surf,samples at={-2,-1.95,...,2}] {U(\x,\y)};
\end{axis}
\end{tikzpicture}
\end{document}
关于如何解决这些问题有什么想法吗?
答案1
解释很简单:没有三角形表面元素,只有矩形。但是,在对角线上,您需要三角形元素才能一次性绘制该物体。这意味着使用patchplots
。假设您不想要那些(如果我错了请告诉我),您可以分两次绘制该物体。
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[declare function = {U(\x,\y)=((abs(\x)<1) && (abs(\y)<1))
+((abs(\x)>=1) || (abs(\y)>=1)) + max(2*\x-2,0) - min(2*\x+2,0)
+ max(2*\y-2,0) - min(2*\y+2,0);}]
\begin{axis}[
colormap name = viridis,
width = 8in,
height = 6in,
view = {25}{11},
grid = major,
domain = -2:2,
y domain = -2:2,
]
\begin{scope}
\clip (0,0,0) -- (-1,-1,1) -- (1,-1,1) -- (1,1,1) -- cycle;
\addplot3 [surf,samples=21,domain=-{sqrt(2)}:{sqrt(2)},
y domain= -{sqrt(2)}:{sqrt(2)}]
({(x+y)/sqrt(2)},{(x-y)/sqrt(2)},%
{max(abs((x+y)/sqrt(2)),abs((x-y)/sqrt(2)))});
\end{scope}
\addplot3 [surf,samples=41] {U(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}
顺便说一句,内部函数,即 for|x|<1
和|y|<1
就是max(abs(x),abs(y))
。