我正在尝试画这幅画
我试过
\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz,tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{100}{70}
\begin{tikzpicture}[tdplot_main_coords]
\pgfmathsetmacro\a{2}
\pgfmathsetmacro\h{5}
\path (-\a,-\a,0) coordinate (A) (-\a,\a,0) coordinate (B)
(\a,\a,0) coordinate (C) (\a,-\a,0) coordinate (D)
(0,0,\h) coordinate (T);
%\foreach \point/\position in {A/below,B/right,C/below,D/above,T/above}{\fill (\point) circle (1.5pt);\node[\position=1.5pt] at (\point) {$\point$};}
\draw[thick] (T) -- (A) -- (B) -- (C) -- cycle (T) -- (B);
\draw[dashed] (T) -- (D) -- (C) (A) -- (D);
\end{tikzpicture}
\end{document}
并得到
如何绘制金字塔中的棱柱堆栈?
答案1
如果您考虑使用不透明度(而不是虚线)来显示可见性的可能性,那么金字塔将非常容易。现在重要的是按顺序排列要绘制的所有多边形,以显示可见性。
例如,创建两个\pic
s,长方体和三角形,并使用3d
和perspective
Ti钾Z 库:
\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{3d,perspective}
\tikzset
{
my cuboid/.style={draw=red,fill=white,fill opacity=0.4},
my pyramid/.style={draw}, % <--- you can fill and change the opacity her too, if you want
pics/cuboid/.style 2 args={% #1 = base side, #2 = height
code={%
\draw[canvas is xy plane at z=0 ,pic actions] (-0.5*#1,-0.5*#1) rectangle ++ (#1,#1);
\draw[canvas is xz plane at y=-0.5*#1,pic actions] (-0.5*#1,0) rectangle ++ (#1,#2);
\draw[canvas is yz plane at x=-0.5*#1,pic actions] (-0.5*#1,0) rectangle ++ (#1,#2);
\draw[canvas is xy plane at z=#2 ,pic actions] (-0.5*#1,-0.5*#1) rectangle ++ (#1,#1);
\draw[canvas is xz plane at y= 0.5*#1,pic actions] (-0.5*#1,0) rectangle ++ (#1,#2);
\draw[canvas is yz plane at x= 0.5*#1,pic actions] (-0.5*#1,0) rectangle ++ (#1,#2);
}},
pics/triangle/.style 2 args={% #1 = side, #2 = height
code={\draw[pic actions] (-0.5*#1,0.5*#1,0) -- (0.5*#1,0.5*#1,0) -- (0,0,#2) -- cycle;}},
}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,3d view={160}{20}]
% dimensions
\def\l{2.5} % pyrmaid side
\def\h{3} % pyramid height
\def\n{9} % number of cuboids
% pyramid, back
\foreach\i in {90,180}
\pic[rotate around z=\i,my pyramid] {triangle={\l}{\h}};
% cuboids
\pgfmathsetmacro\hh{\h/(\n+1)} % cuboid height
\foreach\i in {1,...,\n}
\pgfmathsetmacro\ll{\l*(\n+1-\i)/(\n+1)} % cuboid side
\pgfmathsetmacro\pp{\hh*(\i-1)} % cuboid position
\pic[my cuboid] at (0,0,\pp) {cuboid={\ll}{\hh}};
% pyramid, front
\foreach\i in {0,270}
\pic[rotate around z=\i,my pyramid] {triangle={\l}{\h}};
\end{tikzpicture}
\end{document}