marmotghost / tikz-3dtools

Experimental library for 3d operations with TikZ
8 stars 0 forks source link

How can I use 3dtools to draw `n polydron` like this #10

Closed johnpaulpeter closed 1 year ago

johnpaulpeter commented 1 year ago

How can I use 3dtools to draw n polydron like this https://tex.stackexchange.com/questions/684305/can-i-general-this-code-to-draw-a-regular-polyhedron

marmotghost commented 1 year ago

I think that you already got an excellent answer in https://tex.stackexchange.com/questions/684305/can-i-general-this-code-to-draw-a-regular-polyhedron.

marmotghost commented 1 year ago

You can make this nice answer slightly shorter and the hidden edges prettier, though.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{3dtools}%
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
    \begin{scope}[3d/install view={phi=110,theta=70},
                declare function={R=3.5;R1=2;H=3;}]
            \pgfmathtruncatemacro{\Ncorners}{7} 
            \path
            (0,0,0) coordinate (O)  
            (0,0,H) coordinate (S)
            (0,0,H/2) coordinate (C)        
            foreach \X in {1,...,\Ncorners}
            {({R*cos(\X*360/\Ncorners)},{R*sin(\X*360/\Ncorners)},0)
                coordinate (A\X)
            ({R1*cos(\X*360/\Ncorners)},{R1*sin(\X*360/\Ncorners)},H) coordinate (A'\X)
            };
            \edef\mycorners{{(A'1)}}        
            \tikzset{add corner/.code={\edef\mycorners{\mycorners,{(A'#1)}}},
                add corner/.list={2,...,\Ncorners}}     
            \begin{scope}[3d/polyhedron/.cd,O={(C)},
                back/.append style={3d/polyhedron/complete dashes,3d/hidden},
                fore/.append style={3d/visible,fill=none}]
                \foreach \X [remember=\X as \LastX (initially \Ncorners)] in {1,...,\Ncorners}
                {\tikzset{3d/polyhedron/draw face with corners/.expanded={{(A\LastX)},{(A'\LastX)},{(A'\X)},(A\X)}}}
                \tikzset{3d/polyhedron/draw face with corners/.expanded={\mycorners}}
            \end{scope}         
        \end{scope}
    \end{tikzpicture}
\end{document}
Screenshot 2023-05-08 at 8 48 21 AM