长方体上方的圆柱体

长方体上方的圆柱体

如何像这样在立方体上方绘制一个圆柱体(使用 tikz)?

在此处输入图片描述

答案1

改进答案:考虑到@Tarass的评论,我开始思考用tikz-3Dplot绘制的阴影对象。我的天真尝试没有成功,因为不能使用那里的轮廓进行剪辑。然而,最近我从一位第一个问题(!),即使用transparency group.这允许将 tikz-3dplot 获得的轮廓与阴影结合起来。

 \documentclass[tikz,border=2mm]{standalone}
 \usepackage{tikz-3dplot}
 \usetikzlibrary{shadings,calc}
 \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){}; 
 \shade[left color=gray!20,right color=gray!60]
  (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}

在此处输入图片描述

可以看出,没有必要重新发明 tikz-3dplot 提供的坐标变换。

原始答案

 \documentclass[tikz,border=2mm]{standalone}
 \usetikzlibrary{shadings}
 \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}
 {\draw (0,\Y,0) circle[x radius=6.4,y radius=2.7];}
 \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};
 \shade[left color=gray!20,right color=gray!60,opacity=0.5] (-6.4,4,0) arc[x radius=6.4,y radius=2.7,start angle=-180,end angle=0]
 (6.4,4,0) -- (6.4,12,0)
 arc[x radius=6.4,y radius=2.7,start angle=0,end angle=-180]     
 --(-6.4,12,0) --    (-6.4,4,0);
 \fill[gray!10] (0,12,0) circle[x radius=6.4,y radius=2.7];
 \end{tikzpicture}
 \end{document}

在此处输入图片描述

答案2

在此处输入图片描述

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

相关内容