# A cylinder above a cuboid

How to draw a cylinder above a cube (with tikz) like this? • It is not a cube. – Tarass Feb 25 '18 at 18:32
• Oh... sorry. I mean cuboid, right? – kalakay Feb 26 '18 at 2:05
• Parallelepipede rectangle in French. – Tarass Feb 26 '18 at 6:24

Improved answer: Taking into account @Tarass comments made me think about shading objects drawn by tikz-3Dplot. My naive attempt wouldn't work because one cannot use the contours there for clips. However, recently I learned a nice trick from a first question (!), namely the use of transparency group. This allows one to combine contours obtained by tikz-3dplot with shadings.

 \documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{110}

\begin{tikzpicture}[scale=0.5,font=\sf]
\filldraw[gray!20,opacity=0.5] (-7,-4,7) rectangle (7,4,7);
\foreach \X in {-7,7}{\foreach \Y in {-4,4}{
\draw[fill=gray!10,opacity=0.5] (\X,\Y,-7) -- (\X,\Y,7) -- (\X,{-1*\Y},7)-- (\X,{-1*\Y},-7) --cycle;
\draw[fill=gray!10,opacity=0.5] (\X,\Y,-7) -- (\X,\Y,7) -- ({-1*\X},\Y,7)--({-1*\X},\Y,-7) --cycle;
}}
\filldraw[gray!20,opacity=0.5] (-7,-4,7) rectangle (7,4,7);
\draw[canvas is xz plane at y = 4]
(0,0) circle(7);
\begin{scope}[opacity=.5, transparency group]
\shade[canvas is xz plane at y = 4,left color=gray!20,right color=gray!60]
(0,0) circle(7);
\node[canvas is xz plane at y = 2,minimum width=7cm,circle] (circle) at (0,0){};
(circle.160)--(circle.-20)--($(circle.-20)+(0,8)$)--
($(circle.160)+(0,8)$)--cycle;
\end{scope}
\draw (circle.160)--($(circle.160)+(0,8)$) node[midway,above,sloped] {8 cm};
\draw (circle.-20)--($(circle.-20)+(0,8)$);
\draw[fill=gray!10,canvas is xz plane at y = 12]   (0,0) circle(7);
\path (7,-4,-7) --(7,4,-7) node[midway,below,sloped] {8 cm};
\path (7,-4,-7) --(7,-4,7) node[midway,below,sloped] {14 cm};
\path (-7,-4,7) --(7,-4,7) node[midway,below,sloped] {14 cm};
\end{tikzpicture}
\end{document} As one can see, there is no need to re-invent the coordinate transformations provided by tikz-3dplot.

 \documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.5,font=\sf]
\filldraw[gray!20,opacity=0.5] (-7,-4,7) rectangle (7,4,7);
\foreach \X in {-7,7}{\foreach \Y in {-4,4}{
\draw[fill=gray!10,opacity=0.5] (\X,\Y,-7) -- (\X,\Y,7) -- (\X,{-1*\Y},7)-- (\X,{-1*\Y},-7) --cycle;
\draw[fill=gray!10,opacity=0.5] (\X,\Y,-7) -- (\X,\Y,7) -- ({-1*\X},\Y,7)--({-1*\X},\Y,-7) --cycle;
}}
\filldraw[gray!20,opacity=0.5] (-7,-4,7) rectangle (7,4,7);
\foreach \Y in {12,4}
\foreach \X in {-6.4,6.4}
{\draw (\X,4,0) --(\X,12,0);
}
\path (-6.4,4,0) --(-6.4,12,0) node[midway,above,sloped] {8 cm};
\path (7,-4,-7) --(7,4,-7) node[midway,below,sloped] {8 cm};
\path (7,-4,-7) --(7,-4,7) node[midway,below,sloped] {14 cm};
\path (-7,-4,7) --(7,-4,7) node[midway,below,sloped] {14 cm};
(6.4,4,0) -- (6.4,12,0)
--(-6.4,12,0) --    (-6.4,4,0);
\end{tikzpicture}
\end{document} • The two volumes are drawn in different perspectives. – Tarass Feb 25 '18 at 20:10
• @Tarass So are the ones provided by the OP. – marmot Feb 25 '18 at 20:13
• I didn't say the contrary. His sketch is a non sens. – Tarass Feb 25 '18 at 20:18
• @Tarass I didn't say you did. ;-) Do you like the second part of my answer better? – marmot Feb 25 '18 at 22:02
• It's better ;-) but the edges of the cylindre seem wrong. See my edit to avoid calculation. Even angle could be set from the perspective. – Tarass Feb 25 '18 at 23:10 \documentclass{article}

\usepackage{tikz,xparse,siunitx}
\usetikzlibrary{calc,intersections}

%################################
%            cotation
% Macro for cotation don't look here
% for the cuboid picture
%################################

\tikzset{%
Cote node/.style={%
midway,
%sloped,
fill=white,
inner sep=1.5pt,
outer sep=2pt
},
Cote arrow/.style={%
<->,
>=latex,
very thin
}
}

\makeatletter
\NewDocumentCommand{\Cote}{%
s       % cotation avec les flèches à l'extérieur
D<>{1.5pt} % offset des traits
O{.75cm}    % offset de cotation
m       % premier point
m       % second point
m       % étiquette
D<>{o}  % () coordonnées -> angle
% h -> horizontal,
% v -> vertical
% o or what ever -> oblique
O{}     % parametre du tikzset
}{%

{\tikzset{#8}

\coordinate (@1) at #4 ;
\coordinate (@2) at #5 ;

\if #7H % Cotation verticale
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@5) at ($#5+(#3,0)$) ;
\coordinate (@4) at ($#4+(#3,0)$) ;
\else
\if #7V % Cotation verticale
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@5) at ($#5+(0,#3)$) ;
\coordinate (@4) at ($#4+(0,#3)$) ;
\else
\if #7v % Cotation verticale
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@4) at (@0|-@1) ;
\coordinate (@5) at (@0|-@2) ;
\else
\if #7h % Cotation horizontale
\coordinate (@0) at ($($#4!.5!#5$) + (0,#3)$) ;
\coordinate (@4) at (@0-|@1) ;
\coordinate (@5) at (@0-|@2) ;
\else % cotation encoche
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
\coordinate (@5) at ($#7!#3!#5$) ;
\coordinate (@4) at ($#7!#3!#4$) ;
\else % cotation oblique
\coordinate (@5) at ($#5!#3!90:#4$) ;
\coordinate (@4) at ($#4!#3!-90:#5$) ;
\fi\fi\fi\fi\fi

\draw[very thin,shorten >= #2,shorten <= -2*#2] (@4) -- #4 ;
\draw[very thin,shorten >= #2,shorten <= -2*#2] (@5) -- #5 ;

\IfBooleanTF #1 {% avec étoile
\draw[Cote arrow,-] (@4) -- (@5)
node[Cote node] {#6\strut};
\draw[Cote arrow,<-] (@4) -- ($(@4)!-6pt!(@5)$) ;
\draw[Cote arrow,<-] (@5) -- ($(@5)!-6pt!(@4)$) ;
}{% sans étoile
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
\draw[Cote arrow] (@5) to[bend right]
node[Cote node] {#6\strut} (@4) ;
\else
\draw[Cote arrow] (@4) -- (@5)
node[Cote node] {#6\strut};
\fi
}}
}
\makeatother

% Begins here

\begin{document}
\begin{tikzpicture}[
line join=round,
x={(0:1cm)},    % horizontal left x vertice
y={(38:.5cm)},  % oblic y vertice : angle reduction for perspective
z={(0cm,1cm)},  % vertical up y vertice
scale=5]

\def\angle{25}      % angle of the main ellipse axe of the cylindre
\def\Hauteur{.7}    % reduction of the higth of the cuboid

\coordinate (A) at (0,0,0) ;    % set bottom face of the cuboid
\coordinate (B) at (1,0,0) ;
\coordinate (C) at (1,1,0) ;
\coordinate (D) at (0,1,0) ;

\coordinate (E) at (0,0,\Hauteur) ; % set top face of the cuboid
\coordinate (F) at (1,0,\Hauteur) ;
\coordinate (G) at (1,1,\Hauteur) ;
\coordinate (H) at (0,1,\Hauteur) ;

% set 3 points in the top face for a new coordinate system
\coordinate (O) at ($(E)!.5!(G)$) ; % set origin
\coordinate (X) at ($(F)!.5!(G)$) ; % OX is x vertice
\coordinate (Y) at ($(G)!.5!(H)$) ;-% OY is y vertice

% same thing for the top face of the cylinder
\coordinate (Otop) at ([yshift=\Hauteur cm]O) ;
\coordinate (Xtop) at ([yshift=\Hauteur cm]X) ;
\coordinate (Ytop) at ([yshift=\Hauteur cm]Y) ;

% let begin by the top face of the cylindre
\begin{scope}
\pgftransformtriangle   % set the new system ccordinate for this scope
{\pgfpointanchor{Otop}{center}} % Otop is origin
{\pgfpointanchor{Xtop}{center}} % OtopYtop OtopYtop are the vertice
{\pgfpointanchor{Ytop}{center}} ; % there length is 1pt

% draw the top circle that will appear as the correct ellipse in the
% new coordinate system
\draw[semithick] (Otop) circle (1pt) ;

%  Calculation of the end of the edges of the cylinder
\coordinate (Ltop) at (\angle-180:1pt) ;    % left one
\coordinate (Rtop) at (\angle:1pt) ;        % right one
\end{scope}

% same thing in the bottom plane of the cylinder
\begin{scope}
\pgftransformtriangle
{\pgfpointanchor{O}{center}}
{\pgfpointanchor{X}{center}}
{\pgfpointanchor{Y}{center}} ;

% Except one needs to cut the circle in two parts
% the front one, thick
\draw[semithick] (\angle-180:1pt) coordinate (L) arc (\angle-180:\angle:1pt) ;

% the back one, dashed.
\draw[thin,dashed] (\angle:1pt) coordinate (R) arc (\angle:\angle+180:1pt) ;
\end{scope}

\begin{scope}[semithick]
% cylinder edges
\draw[name path=P1] (L)--(Ltop) ; % keep pathes for intersections
\draw[name path=P2] (R)--(Rtop) ;

% cuboid visible edges
\draw (A)--(B)--(C) (A)--(E) (B)--(F) (C)--(G) ;
\path[name path=P3] (G)--(H) ; % keep pathes for intersections
\path[name path=P4] (H)--(E) ;

% Intersection points between cuboid edges and cylinder edges
\path[name intersections={of= P2 and P3, by=I}] ; % I
\path[name intersections={of= P1 and P4, by=J}] ; % J

% top visible edges
\draw (J)--(E)--(F)--(G)--(I) ;

\end{scope}

\begin{scope}[thin,dashed] % unvisible - dashed - edges
\draw (A)--(D)--(C) (D)--(H);
\draw (J)--(H)--(I) ;
\end{scope}

% cotations
\Cote[.1cm]{(A)}{(B)}{\SI{14}{cm}}
\Cote[-.1cm]{(B)}{(C)}{\SI{14}{cm}}<V>[Cote node/.append style={sloped}]
\Cote[.1cm]{(C)}{(G)}{\SI{8}{cm}}[Cote node/.append style={sloped}]
\Cote[.1cm]{(Ltop)}{(L)}{\SI{8}{cm}}[Cote node/.append style={sloped,rotate=180}]

\end{tikzpicture}
\end{document}

• This is too complicated for me to understand. Thanks for your answer. – kalakay Feb 26 '18 at 12:52
• @kalakay My code is more basic than marmot's and nearer of what is done by hand. I put commentary in my code, that could follow. Btw my code deals with dashed invisible parts of the drawing as on your post. Don't look at the first cotation macro, it serve only for standart cotations. – Tarass Feb 26 '18 at 14:29