答案1
使用 Asymptote 和非官方的 polyhedron_js asymptote 包(可在此处获取)https://github.com/pivaldi/asymptote-packages)您可以轻松定义这样的圆柱体并使用它。请找到代码(假设您有 polyhedron_js.asy)
import polyhedron_js;
// comment the following line for OpenGl
//settings.render=0;
settings.tex="pdflatex";
settings.outformat="pdf"; // for opacity
size(10cm);
currentprojection=perspective(7,6,4); //if you want perspectivic look
//currentprojection=orthographic(1,1,0.5); //if you want othographic look
currentlight=(1,1,2);
// currentlight=nolight;
polyhedron hexa_cyl;
guide hexa=polygon(6);
hexa_cyl[0]=(point(hexa,0).x,point(hexa,0).y,0);
for(int i=1;i<6;++i) hexa_cyl[0]=hexa_cyl[0]--(point(hexa,i).x,point(hexa,i).y,0);
hexa_cyl[0]=hexa_cyl[0]--cycle;
for(int i=0;i<6;++i)
{
hexa_cyl[i+1]=point(hexa_cyl[0],i)--point(hexa_cyl[0],(i+1)%6)--
(shift(0,0,1)*point(hexa_cyl[0],(i+1)%6))--(shift(0,0,1)*point(hexa_cyl[0],i))--cycle;
}
hexa_cyl[6]=shift(0,0,1)*hexa_cyl[0];
polyhedron[] p1={scale(1,1,3)*hexa_cyl};
polyhedron[] p2={shift(4,1,1)*rotate(60,(2,1,3))*hexa_cyl};
polyhedron[] p3={shift(-2,-2,-2)*rotate(90,(1,0,0))*scale(.5,.5,2)*hexa_cyl};
filldraw(p1,new pen[]{0.8green},op=0.9);
filldraw(p2,new pen[]{0.8blue},op=0.9);
filldraw(p3,new pen[]{0.8yellow},op=0.9);
注释:hexa_cyl
高度为 1(方向z
),底面是在单位圆上构造的正六边形,(0,0,0)
是底面的中心。来自文档
scale(x,y,z)
:在 x 方向上按 x 缩放,在 y 方向上按 y 缩放,在 z 方向上按 z 缩放
shift(triple v)
:由三重 v 翻译
rotate(real angle, triple v)
:绕通过原点的轴 v 旋转一定角度;
这样,通过上述三个步骤的组合,hexa_cyl
就可以得到您想要的六角柱体。
答案2
这是一个建议。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\pgfkeys{/hexagonal prism/.cd,
rotation angle/.initial=0,
height/.initial=4,
diameter/.initial=2
}
\tikzset{pics/.cd,
hexi/.style={code={
\pgfmathsetmacro{\myangle}{mod(\pgfkeysvalueof{/hexagonal prism/rotation angle},60)}
\pgfmathsetmacro{\phieff}{(\tdplotmainphi-mod(\tdplotmainphi+690,60)+30)}
\draw[fill=gray!60] ({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(0+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(0+\myangle+\phieff)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-60+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-60+\myangle+\phieff)},0)
--({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-60+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-60+\myangle+\phieff)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(0+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(0+\myangle+\phieff)},\pgfkeysvalueof{/hexagonal prism/height}) -- cycle;
\draw[fill=gray!20] ({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-60+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-60+\myangle+\phieff)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-120+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-120+\myangle+\phieff)},0)
--({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-120+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-120+\myangle+\phieff)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-60+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-60+\myangle+\phieff)},\pgfkeysvalueof{/hexagonal prism/height}) -- cycle;
\draw[fill=gray!10] ({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-120+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-120+\myangle+\phieff)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-180+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-180+\myangle+\phieff)},0)
--({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-180+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-180+\myangle+\phieff)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-120+\myangle+\phieff)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-120+\myangle+\phieff)},\pgfkeysvalueof{/hexagonal prism/height}) -- cycle;
\ifdim\tdplotmaintheta pt<90pt
\draw[fill=gray!15] ({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(0+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(0+\myangle)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-60+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-60+\myangle)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-120+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-120+\myangle)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-180+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-180+\myangle)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-240+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-240+\myangle)},\pgfkeysvalueof{/hexagonal prism/height}) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-300+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-300+\myangle)},\pgfkeysvalueof{/hexagonal prism/height}) -- cycle;
\else
\draw[fill=gray!85] ({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(0+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(0+\myangle)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-60+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-60+\myangle)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-120+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-120+\myangle)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-180+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-180+\myangle)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-240+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-240+\myangle)},0) --
({\pgfkeysvalueof{/hexagonal prism/diameter}*cos(-300+\myangle)},{\pgfkeysvalueof{/hexagonal prism/diameter}*sin(-300+\myangle)},0) -- cycle;
\fi
}}}
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords,font=\sffamily]
% \path[tdplot_screen_coords,use as bounding box] (-3,-1) rectangle (3,5);
\pic {hexi};
\pic[shift={(5,0,0)},/hexagonal prism/height=3] {hexi};
\end{tikzpicture}
\end{document}