我通过代码描绘了一个五面体
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset
{
plotOptsA/.style={surf,patch,patch type=triangle,shader=faceted interp},
plotOptsB/.style={surf,patch,patch type=rectangle,shader=faceted interp},
}
% Plot pentahedron.
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
view={45}{20},
colorbar,
]
\newcommand{\Ax}{-1/sqrt(3)}
\newcommand{\Ay}{-1}
\newcommand{\Bx}{-2/sqrt(3)}
\newcommand{\By}{-2}
\newcommand{\Cx}{0}
\newcommand{\Cy}{-2}
\newcommand{\ABCz}{1}
\addplot3[plotOptsA] coordinates{(\Ax,\Ay,0) (\Bx,\By,0) (\Cx,\Cy,0)}; % x, xy ground plane
\addplot3[plotOptsA] coordinates{(\Ax,\Ay,\ABCz) (\Bx,\By,\ABCz) (\Cx,\Cy,\ABCz)}; % x xy plane
\addplot3[plotOptsB] coordinates{(\Ax,\Ay,0) (\Bx,\By,0) (\Bx,\By,\ABCz) (\Ax,\Ay,\ABCz)}; % x, xz plane
\addplot3[plotOptsB] coordinates{(\Bx,\By,0) (\Cx,\Cy,0) (\Cx,\Cy,\ABCz) (\Bx,\By,\ABCz)}; % x, xz plane
\addplot3[plotOptsB] coordinates{(\Ax,\Ay,0) (\Cx,\Cy,0) (\Cx,\Cy,\ABCz) (\Ax,\Ay,\ABCz)}; % x, xz plane
\end{axis}
\end{tikzpicture}
\end{document}
结果如下
我想有效地重复这个五面体多次,但对于不同的,,,,,\Ax
和\Ay
,但我不知道在 LaTeX / / PGF / TikZ / PGFplots 中执行此操作的好方法是什么。\Bx
\By
\Cx
\Cy
\ABCz
在 Matlab 中,我将使用代码
% Clean up.
clear all;
clear global;
close all;
clc;
format compact;
% Figure definitions.
figure;
ha=gca;
set(ha,'NextPlot','add');
az=225;
el=10;
view(az,el);
ABCz=1;
for yn=1:4
Ay=-1*(yn-1);
By=-yn;
Cy=-yn;
for q=-(yn-1):2:(yn-1)
Ax=q/sqrt(3);
Bx=Ax-1/sqrt(3);
Cx=Ax+1/sqrt(3);
pentahedron(Ax,Ay,Bx,By,Cx,Cy,ABCz);
ABCz=ABCz+1;
end
end
xlabel('x');
ylabel('y');
zlabel('z');
其中函数pentahedron
定义为
function pentahedron(Ax,Ay,Bx,By,Cx,Cy,ABCz)
X=[Ax;Bx;Cx];Y=[Ay;By;Cy];Z=[0;0;0];fill3(X,Y,Z,Z);
X=[Ax;Bx;Cx];Y=[Ay;By;Cy];Z=[ABCz;ABCz;ABCz];fill3(X,Y,Z,Z);
X=[Ax;Bx;Bx;Ax];Y=[Ay;By;By;Ay];Z=[0;0;ABCz;ABCz];fill3(X,Y,Z,Z);
X=[Bx;Cx;Cx;Bx];Y=[By;Cy;Cy;By];Z=[0;0;ABCz;ABCz];fill3(X,Y,Z,Z);
X=[Ax;Cx;Cx;Ax];Y=[Ay;Cy;Cy;Ay];Z=[0;0;ABCz;ABCz];fill3(X,Y,Z,Z);
,重复五面体。是否有可能在 LaTeX 中构建一些宏或新环境来模仿这个 Matlab 函数?
答案1
您的 matlab 脚本的直接翻译可以如下
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset
{
plotOptsA/.style={surf,patch,patch type=triangle,shader=faceted interp},
plotOptsB/.style={surf,patch,patch type=rectangle,shader=faceted interp},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xmin=-4/sqrt(3),
xmax=4/sqrt(3),
ymin=-4,
ymax=0,
zmin=0,
view={225}{10}
]
\foreach\yn
[
evaluate=
{
\Ay=int(1-\yn);
\By=-\yn;
\Cy=-\yn;%
}
] in {1,...,4}
{
\foreach\q
[
evaluate=
{
\Ax=(1/sqrt(3)*\q);
\Bx=\Ax-(1/sqrt(3));
\Cx=\Ax+(1/sqrt(3));%
},
count=\ABCz
] in{\Ay,\numexpr\Ay+2,...,-\Ay}
{
\addplot3[plotOptsA] coordinates{(\Ax,\Ay,0) (\Bx,\By,0) (\Cx,\Cy,0)};
\addplot3[plotOptsA] coordinates{(\Ax,\Ay,\ABCz) (\Bx,\By,\ABCz) (\Cx,\Cy,\ABCz)};
\addplot3[plotOptsB] coordinates{(\Ax,\Ay,0) (\Bx,\By,0) (\Bx,\By,\ABCz) (\Ax,\Ay,\ABCz)};
\addplot3[plotOptsB] coordinates{(\Bx,\By,0) (\Cx,\Cy,0) (\Cx,\Cy,\ABCz) (\Bx,\By,\ABCz)};
\addplot3[plotOptsB] coordinates{(\Ax,\Ay,0) (\Cx,\Cy,0) (\Cx,\Cy,\ABCz) (\Ax,\Ay,\ABCz)};
}
}
\end{axis}
\end{tikzpicture}
\end{document}
我希望它看起来应该是这样的