# Drawing hexagonal prisms

Simmilar to: Drawing spheres

I want to draw multiple 3D hexagonal cylinders like this one: with different sizes and positions.

## 2 Answers

Using Asymptote and the unofficial polyhedron_js asymptote package (available here https://github.com/pivaldi/asymptote-packages) you can define easily such a cylinder and play with it. Please find the code (assuming that you have 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=(point(hexa,0).x,point(hexa,0).y,0);
for(int i=1;i<6;++i) hexa_cyl=hexa_cyl--(point(hexa,i).x,point(hexa,i).y,0);
hexa_cyl=hexa_cyl--cycle;
for(int i=0;i<6;++i)
{
hexa_cyl[i+1]=point(hexa_cyl,i)--point(hexa_cyl,(i+1)%6)--
(shift(0,0,1)*point(hexa_cyl,(i+1)%6))--(shift(0,0,1)*point(hexa_cyl,i))--cycle;
}
hexa_cyl=shift(0,0,1)*hexa_cyl;

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);


Comments : the hexa_cyl has height 1 (the z direction), the base is the regular hexagone constructed on the unit-circle, (0,0,0) is the center of the base. From the documentation

scale(x,y,z) : scales by x in the x direction, by y in the y direction, and by z in the z direction

shift(triple v) : translates by the triple v

rotate(real angle, triple v) : rotates by angle in degrees about an axis v through the origin;

so that a combination of the three routines performed on hexa_cyl gives the hexagonal prism that you want.

• Oh wow! That's really stunning! – user121799 Feb 4 at 5:26
• This seems more like what I'm looking for. I don't fully understand the code, what is positioning part? how rotation works? – Ernesto Iglesias Feb 5 at 15:30
• @ErnestoIglesias: please see the added comments and the asymptote documentation. – O.G. Feb 6 at 8:02

Here is a proposal.

\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} 