TikZ-3DPlot – How to Draw a Cylinder Above a Cube with Dashed Lines

tikz-3dplot

I am trying to draw this figures with dashed lines

enter image description here

With Mathematica, It easy to draw.

enter image description here

How to draw the second picture with TeX?

I only draw the cube. My code

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
    \tdplotsetmaincoords{70}{100}
\begin{tikzpicture} [tdplot_main_coords,c/.style={circle,fill,inner sep=1pt},line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
        declare function={a=3;}]
        \path
        (a,-a,-a) coordinate (A)
        (a,a,-a) coordinate (B)
        (-a,a,-a) coordinate (C)
        (-a,-a,-a) coordinate (D)
        (a,-a,a) coordinate (E)
        (a,a,a) coordinate (F)
        (-a,a,a) coordinate (G)
        (-a,-a,a) coordinate (H)
        (0,0,0)  coordinate (O) ;
    \draw[] (C) -- (F) -- (H) (A) -- (F) (A) -- (B) -- (C) (E) -- (F) -- (G)-- (H)--cycle (A) -- (E) (B) --  (F) (C) -- (G);
        \draw[dashed] (A) -- (D) -- (C) (C) -- (A) -- (H)--cycle (D) -- (H);
        \path foreach \p/\g in {A/-90,B/-90,C/0,D/0,E/90,F/90,G/90,H/90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};  
    \end{tikzpicture}
    \end{document}

Best Answer

With the second picture, you can you 3dtools.

 \documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\foreach \Angle in {5, 10, ..., 355}
{   \begin{tikzpicture}[3d/install view={phi=\Angle,theta=70},line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
        declare function={a=2.5;},same bounding box=A]
        \path
        (a,-a,-a) coordinate (A)
        (a,a,-a) coordinate (B)
        (-a,a,-a) coordinate (C)
        (-a,-a,-a) coordinate (D)
        (a,-a,a) coordinate (E)
        (a,a,a) coordinate (F)
        (-a,a,a) coordinate (G)
        (-a,-a,a) coordinate (H)
        (0,0,0)  coordinate (O)
        (0,0,a) coordinate (O')
        ;   
        \tikzset{3d/polyhedron/.cd,O={(O)},
            back/.style={3d/polyhedron/complete dashes,fill=none,3d/hidden},
            fore/.style={3d/visible,fill=none},draw face with corners={{(B)},{(C)},{(G)},{(F)}},
            draw  face with corners={{(D)},{(C)},{(G)},{(H)}},
            draw  face with corners={{(A)},{(B)},{(C)},{(D)}},
            draw  face with corners={{(A)},{(B)},{(F)},{(E)}},
            draw  face with corners={{(A)},{(E)},{(H)},{(D)}}}  
        \path (O')  pic[3d/visible/.append style={save named path=cyl}]{3d/frustum={r=a,R=a,h=a}};
        \path[save named path=EFGH] (E) -- (F) -- (G) -- (H) -- cycle;
        \tikzset{3d/ordered paths/.cd,cyl/.style={draw=none}}
        % draw ordered objects  
        \tikzset{3d/draw ordered paths={EFGH,cyl}}
        \path foreach \p/\g in {A/-90,B/180,C/-90,D/-90,E/0,F/90,G/-30,H/0,O'/90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}}; 
    \end{tikzpicture}}
\end{document}    

enter image description here

I tried with GeoGebra and put the file at here. You can rotate the figure with mouse.