如何绘制两个长方体并将它们合并到同一个图形中

如何绘制两个长方体并将它们合并到同一个图形中

我想表示一个托盘。托盘是一个高 0.15m、宽 0.8m、深 1.2m 的地下室,上面有箱子,我用一个大箱子来表示。

我已经使用过代码“如何缩小一组 tikzpictures 并单独标记文本”

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}

\newif\ifcuboidshade
\newif\ifcuboidemphedge

\tikzset{
  cuboid/.is family,
  cuboid,
  shiftx/.initial=0,
  shifty/.initial=0,
  dimx/.initial=3,
  dimy/.initial=3,
  dimz/.initial=3,
  scale/.initial=1,
  densityx/.initial=1,
  densityy/.initial=1,
  densityz/.initial=1,
  rotation/.initial=0,
  anglex/.initial=0,
  angley/.initial=90,
  anglez/.initial=225,
  scalex/.initial=1,
  scaley/.initial=1,
  scalez/.initial=0.5,
  xedgelabel/.store in=\xedgelabel,
  yedgelabel/.store in=\yedgelabel,
  zedgelabel/.store in=\zedgelabel,
  xedgelabel={},
  yedgelabel={},
  zedgelabel={},
  edgelabel/.style={},
  front/.style={draw=black,fill=white},
  top/.style={draw=black,fill=white},
  right/.style={draw=black,fill=white},
  shade/.is if=cuboidshade,
  shadecolordark/.initial=black,
  shadecolorlight/.initial=white,
  shadeopacity/.initial=0.15,
  shadesamples/.initial=16,
  emphedge/.is if=cuboidemphedge,
  emphstyle/.style={thick},
}

\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}

% Commands
\newcommand{\tikzcuboid}[1]{
    \tikzset{cuboid,#1} % Process Keys passed to command
  \pgfmathsetlengthmacro{\vectorxx}{\tikzcuboidkey{scalex}*cos(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectorxy}{\tikzcuboidkey{scalex}*sin(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectoryx}{\tikzcuboidkey{scaley}*cos(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectoryy}{\tikzcuboidkey{scaley}*sin(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectorzx}{\tikzcuboidkey{scalez}*cos(\tikzcuboidkey{anglez})*28.452756}
  \pgfmathsetlengthmacro{\vectorzy}{\tikzcuboidkey{scalez}*sin(\tikzcuboidkey{anglez})*28.452756}
  \begin{scope}[xshift=\tikzcuboidkey{shiftx}, yshift=\tikzcuboidkey{shifty}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
    \pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{densityx}}
  \pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{densityy}}
  \pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{densityz}}
  \newcommand{\dimx}{\tikzcuboidkey{dimx}}
  \newcommand{\dimy}{\tikzcuboidkey{dimy}}
  \newcommand{\dimz}{\tikzcuboidkey{dimz}}
  \pgfmathsetmacro{\secondx}{2*\steppingx}
  \pgfmathsetmacro{\secondy}{2*\steppingy}
  \pgfmathsetmacro{\secondz}{2*\steppingz}
  \foreach \x in {\steppingx,\secondx,...,\dimx}
  {\ifnum\dimy=1
   \def\lsty{\dimy}
   \else
   \def\lsty{\steppingy,\secondy,...,\dimy}
   \fi
    \foreach \y in \lsty
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;
    }
    }
  \ifnum\dimx=1
  \def\lstx{\dimx}
  \else
  \def\lstx{\steppingx,\secondx,...,\dimx}
  \fi
  \foreach \x in \lstx
  { \ifnum\dimz=1
   \def\lstz{\dimz}
   \else
   \def\lstz{\steppingz,\secondz,...,\dimz}
   \fi
    \foreach \z in \lstz
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
        }
    }
    \ifnum\dimy=1
    \def\lsty{\dimy}
    \else
    \def\lsty{\steppingy,\secondy,...,\dimy}
    \fi
    \foreach \y in \lsty
  { \foreach \z in {\steppingz,\secondz,...,\dimz}
    {   \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
    }
  }
    \path (0,0,\dimz) -- (0,\dimy,\dimz) node[midway,above,edgelabel]{\yedgelabel};
    \path (0,0,\dimz) -- (\dimx,0,\dimz) node[midway,below,edgelabel]{\xedgelabel};
    \path (\dimx,0,\dimz) -- (\dimx,0,0) node[midway,below,edgelabel]{\zedgelabel};
  \ifcuboidemphedge
    \draw[cuboid/emphstyle] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
    \draw[cuboid/emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
    \draw[cuboid/emphstyle] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
    \fi
    \ifcuboidshade
    \pgfmathsetmacro{\cstepx}{\dimx/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepy}{\dimy/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepz}{\dimz/\tikzcuboidkey{shadesamples}}
    \foreach \s in {1,...,\tikzcuboidkey{shadesamples}}
    {   \pgfmathsetmacro{\lows}{\s-1}
        \pgfmathsetmacro{\cpercent}{(\lows)/(\tikzcuboidkey{shadesamples}-1)*100}
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\s*\cstepy,\dimz) -- (\s*\cstepx,\s*\cstepy,\dimz) -- (\s*\cstepx,0,\dimz) -- (\lows*\cstepx,0,\dimz) -- (\lows*\cstepx,\lows*\cstepy,\dimz) -- (0,\lows*\cstepy,\dimz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,\lows*\cstepz) -- (0,\dimy,\lows*\cstepz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (\dimx,0,\s*\cstepz) -- (\dimx,\s*\cstepy,\s*\cstepz) -- (\dimx,\s*\cstepy,0) -- (\dimx,\lows*\cstepy,0) -- (\dimx,\lows*\cstepy,\lows*\cstepz) -- (\dimx,0,\lows*\cstepz) -- cycle;
    }
    \fi 

  \end{scope}
}

\makeatother

\begin{document}


\begin{tikzpicture}[scale=1.6]

    \tikzcuboid{%
    shiftx=0cm,%
    shifty=0cm,%
    scale=1.00,%
    rotation=0,%
    densityx=1,%
    densityy=1,%
    densityz=1,%
    dimx=1,%
    dimy=1,%
    dimz=1,%
    front/.style={draw=brown!75!black,fill=brown!25!white},%
    right/.style={draw=brown!25!black,fill=brown!60!white},%
      top/.style={draw=brown!50!black,fill=brown!30!white},%
    % anglex=-7,%
    anglex=0,%
    angley=90,%
    anglez=205,%
    scalex=1.5,%
    scaley=3,%
    scalez=1.2,%
    emphedge=false,%
    shade,%
    shadeopacity=0.15,%
    % xedgelabel={0.8 m},%
    yedgelabel={2 m},%
%     zedgelabel={1.2 m},%
    /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
    }


\end{tikzpicture}

\begin{tikzpicture}[scale=1.6]
        \tikzcuboid{%
        shiftx=0cm,%
        shifty=0cm,%
        scale=1.00,%
        rotation=0,%
        densityx=1,%
        densityy=1,%
        densityz=1,%
        dimx=1,%
        dimy=1,%
        dimz=1,%
        front/.style={draw=brown!75!black,fill=brown!55!white},%
        right/.style={draw=brown!25!black,fill=brown!90!white},%
          top/.style={draw=brown!50!black,fill=brown!60!white},%
        anglex=0,%
        angley=90,%
        anglez=205,%
        scalex=1.5,%
        scaley=0.2,%
        scalez=1.2,%
        emphedge=false,%
        shade,%
        shadeopacity=0.15,%
        xedgelabel={0.8 m},%
        yedgelabel={0.15 m},%
        zedgelabel={1.2 m},%
        /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
        }
\end{tikzpicture}

%   \caption{Cuboid}
%   \label{fig:block}
% \end{figure}

\end{document}

在此处输入图片描述 我不知道如何去除不属于长方体的边以及如何将大盒子(长方体)放置在小长方体(托盘)上

答案1

\x是我的错,抱歉!当我给出上一个答案时,我忽略了实际上有两个循环的事实。相应地,我只调整了一个循环,以便也允许一个立方体在x方向上。在你之前的帖子中,这并不重要,但在这里却很重要。除此之外,你只需将较大的长方体添加到较小的长方体之上,方法是最后绘制它并将其向上移动。

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}

\newif\ifcuboidshade
\newif\ifcuboidemphedge

\tikzset{
  cuboid/.is family,
  cuboid,
  shiftx/.initial=0,
  shifty/.initial=0,
  dimx/.initial=3,
  dimy/.initial=3,
  dimz/.initial=3,
  scale/.initial=1,
  densityx/.initial=1,
  densityy/.initial=1,
  densityz/.initial=1,
  rotation/.initial=0,
  anglex/.initial=0,
  angley/.initial=90,
  anglez/.initial=225,
  scalex/.initial=1,
  scaley/.initial=1,
  scalez/.initial=0.5,
  xedgelabel/.store in=\xedgelabel,
  yedgelabel/.store in=\yedgelabel,
  zedgelabel/.store in=\zedgelabel,
  xedgelabel={},
  yedgelabel={},
  zedgelabel={},
  edgelabel/.style={},
  front/.style={draw=black,fill=white},
  top/.style={draw=black,fill=white},
  right/.style={draw=black,fill=white},
  shade/.is if=cuboidshade,
  shadecolordark/.initial=black,
  shadecolorlight/.initial=white,
  shadeopacity/.initial=0.15,
  shadesamples/.initial=16,
  emphedge/.is if=cuboidemphedge,
  emphstyle/.style={thick},
}

\newcommand{\tikzcuboidkey}[1]{\pgfkeysvalueof{/tikz/cuboid/#1}}

% Commands
\newcommand{\tikzcuboid}[1]{
    \tikzset{cuboid,#1} % Process Keys passed to command
  \pgfmathsetlengthmacro{\vectorxx}{\tikzcuboidkey{scalex}*cos(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectorxy}{\tikzcuboidkey{scalex}*sin(\tikzcuboidkey{anglex})*28.452756}
  \pgfmathsetlengthmacro{\vectoryx}{\tikzcuboidkey{scaley}*cos(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectoryy}{\tikzcuboidkey{scaley}*sin(\tikzcuboidkey{angley})*28.452756}
  \pgfmathsetlengthmacro{\vectorzx}{\tikzcuboidkey{scalez}*cos(\tikzcuboidkey{anglez})*28.452756}
  \pgfmathsetlengthmacro{\vectorzy}{\tikzcuboidkey{scalez}*sin(\tikzcuboidkey{anglez})*28.452756}
  \begin{scope}[xshift=\tikzcuboidkey{shiftx}, yshift=\tikzcuboidkey{shifty}, scale=\tikzcuboidkey{scale}, rotate=\tikzcuboidkey{rotation}, x={(\vectorxx,\vectorxy)}, y={(\vectoryx,\vectoryy)}, z={(\vectorzx,\vectorzy)}]
    \pgfmathsetmacro{\steppingx}{1/\tikzcuboidkey{densityx}}
  \pgfmathsetmacro{\steppingy}{1/\tikzcuboidkey{densityy}}
  \pgfmathsetmacro{\steppingz}{1/\tikzcuboidkey{densityz}}
  \newcommand{\dimx}{\tikzcuboidkey{dimx}}
  \newcommand{\dimy}{\tikzcuboidkey{dimy}}
  \newcommand{\dimz}{\tikzcuboidkey{dimz}}
  \pgfmathsetmacro{\secondx}{2*\steppingx}
  \pgfmathsetmacro{\secondy}{2*\steppingy}
  \pgfmathsetmacro{\secondz}{2*\steppingz}
  \ifnum\dimx=1
  \def\lstx{\dimx}
  \else
  \def\lstx{\steppingx,\secondx,...,\dimx}
  \fi
  \foreach \x in \lstx
  {\ifnum\dimy=1
   \def\lsty{\dimy}
   \else
   \def\lsty{\steppingy,\secondy,...,\dimy}
   \fi
    \foreach \y in \lsty
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \filldraw[cuboid/front] (\lowx,\lowy,\dimz) -- (\lowx,\y,\dimz) -- (\x,\y,\dimz) -- (\x,\lowy,\dimz) -- cycle;
    }
    }
  \ifnum\dimx=1
  \def\lstx{\dimx}
  \else
  \def\lstx{\steppingx,\secondx,...,\dimx}
  \fi
  \foreach \x in \lstx
  { \ifnum\dimz=1
   \def\lstz{\dimz}
   \else
   \def\lstz{\steppingz,\secondz,...,\dimz}
   \fi
    \foreach \z in \lstz
    {   \pgfmathsetmacro{\lowx}{(\x-\steppingx)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/top] (\lowx,\dimy,\lowz) -- (\lowx,\dimy,\z) -- (\x,\dimy,\z) -- (\x,\dimy,\lowz) -- cycle;
        }
    }
    \ifnum\dimy=1
    \def\lsty{\dimy}
    \else
    \def\lsty{\steppingy,\secondy,...,\dimy}
    \fi
    \foreach \y in \lsty
  { \ifnum\dimz=1
    \def\lstz{\dimz}
    \else
    \def\lstz{\steppingz,\secondz,...,\dimz}
    \fi
    \foreach \z in \lstz
    {   \pgfmathsetmacro{\lowy}{(\y-\steppingy)}
      \pgfmathsetmacro{\lowz}{(\z-\steppingz)}
      \filldraw[cuboid/right] (\dimx,\lowy,\lowz) -- (\dimx,\lowy,\z) -- (\dimx,\y,\z) -- (\dimx,\y,\lowz) -- cycle;
    }
  }
    \path (0,0,\dimz) -- (0,\dimy,\dimz) node[midway,above,edgelabel]{\yedgelabel};
    \path (0,0,\dimz) -- (\dimx,0,\dimz) node[midway,below,edgelabel]{\xedgelabel};
    \path (\dimx,0,\dimz) -- (\dimx,0,0) node[midway,below,edgelabel]{\zedgelabel};
  \ifcuboidemphedge
    \draw[cuboid/emphstyle] (0,\dimy,0) -- (\dimx,\dimy,0) -- (\dimx,\dimy,\dimz) -- (0,\dimy,\dimz) -- cycle;%
    \draw[cuboid/emphstyle] (0,\dimy,\dimz) -- (0,0,\dimz) -- (\dimx,0,\dimz) -- (\dimx,\dimy,\dimz);%
    \draw[cuboid/emphstyle] (\dimx,\dimy,0) -- (\dimx,0,0) -- (\dimx,0,\dimz);%
    \fi
    \ifcuboidshade
    \pgfmathsetmacro{\cstepx}{\dimx/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepy}{\dimy/\tikzcuboidkey{shadesamples}}
    \pgfmathsetmacro{\cstepz}{\dimz/\tikzcuboidkey{shadesamples}}
    \foreach \s in {1,...,\tikzcuboidkey{shadesamples}}
    {   \pgfmathsetmacro{\lows}{\s-1}
        \pgfmathsetmacro{\cpercent}{(\lows)/(\tikzcuboidkey{shadesamples}-1)*100}
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\s*\cstepy,\dimz) -- (\s*\cstepx,\s*\cstepy,\dimz) -- (\s*\cstepx,0,\dimz) -- (\lows*\cstepx,0,\dimz) -- (\lows*\cstepx,\lows*\cstepy,\dimz) -- (0,\lows*\cstepy,\dimz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (0,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,\s*\cstepz) -- (\s*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,0) -- (\lows*\cstepx,\dimy,\lows*\cstepz) -- (0,\dimy,\lows*\cstepz) -- cycle;
        \fill[opacity=\tikzcuboidkey{shadeopacity},color=\tikzcuboidkey{shadecolorlight}!\cpercent!\tikzcuboidkey{shadecolordark}] (\dimx,0,\s*\cstepz) -- (\dimx,\s*\cstepy,\s*\cstepz) -- (\dimx,\s*\cstepy,0) -- (\dimx,\lows*\cstepy,0) -- (\dimx,\lows*\cstepy,\lows*\cstepz) -- (\dimx,0,\lows*\cstepz) -- cycle;
    }
    \fi 

  \end{scope}
}

\makeatother

\begin{document}



\begin{tikzpicture}[scale=1.6]
        \tikzcuboid{%
        shiftx=0cm,%
        shifty=0cm,%
        scale=1.00,%
        rotation=0,%
        densityx=1,%
        densityy=1,%
        densityz=1,%
        dimx=1,%
        dimy=1,%
        dimz=1,%
        front/.style={draw=brown!75!black,fill=brown!55!white},%
        right/.style={draw=brown!25!black,fill=brown!90!white},%
          top/.style={draw=brown!50!black,fill=brown!60!white},%
        anglex=0,%
        angley=90,%
        anglez=205,%
        scalex=1.5,%
        scaley=0.2,%
        scalez=1.2,%
        emphedge=false,%
        shade,%
        shadeopacity=0.15,%
        xedgelabel={0.8 m},%
        yedgelabel={0.15 m},%
        zedgelabel={1.2 m},%
        /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
        }
% big cuboid
\begin{scope}[yshift=2mm]
    \tikzcuboid{%
    shiftx=0cm,%
    shifty=0cm,%
    scale=1.00,%
    rotation=0,%
    densityx=1,%
    densityy=1,%
    densityz=1,%
    dimx=1,%
    dimy=1,%
    dimz=1,%
    front/.style={draw=brown!75!black,fill=brown!25!white},%
    right/.style={draw=brown!25!black,fill=brown!60!white},%
      top/.style={draw=brown!50!black,fill=brown!30!white},%
    % anglex=-7,%
    anglex=0,%
    angley=90,%
    anglez=205,%
    scalex=1.5,%
    scaley=3,%
    scalez=1.2,%
    emphedge=false,%
    shade,%
    shadeopacity=0.15,%
    xedgelabel={},%
    yedgelabel={2 m},%
    zedgelabel={},%
    /tikz/edgelabel/.style={sloped,scale=0.7,transform shape},%
    }
\end{scope} 
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容