绘制带有附加矩形的 3D 立方体

绘制带有附加矩形的 3D 立方体

我想画一个立方体,里面有额外的矩形(见下图)。颜色是次要的,因为无论如何我都必须改变它们。我试过在 2D 中仅通过测量立方体的距离来实现,但没有成功。通过论坛中的其他问题,我能够用 tikz 至少画一个立方体,但我最大的问题是如何获得立方体侧面的矩形。 三面都有额外矩形的立方体

答案1

3d欢迎!这可以通过库和例如来完成tikz-3dplot

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{130}
\begin{tikzpicture}[tdplot_main_coords,line join=round]
 \begin{scope}[canvas is xy plane at z=1]
  \draw[fill=green!60!black] (-1,-1) rectangle (1,1);
  \draw[fill=green] (-1,-0.5) rectangle (1,0.5);
 \end{scope} 
 \begin{scope}[canvas is xz plane at y=1]
  \draw[fill=green!60!black] (-1,-1) rectangle (1,1);
  \draw[fill=white] (0,0.6) rectangle (0.6,0);
 \end{scope} 
 \begin{scope}[canvas is yz plane at x=1]
  \draw[fill=green!60!black] (-1,-1) rectangle (1,1);
  \draw[fill=green] (-0.5,0.6) rectangle (0.5,1);
  \draw[fill=green] (-0.5,-0.6) rectangle (0.5,-1);
 \end{scope} 
\end{tikzpicture}
\end{document}

在此处输入图片描述

perspective或者如果您想要更现实的投影,您可以使用该库。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{perspective}
\tikzset{xy plane at z/.style={insert path={
 (tpp cs:x=\xmin,y=\ymin,z=#1) -- (tpp cs:x=\xmax,y=\ymin,z=#1) 
    -- (tpp cs:x=\xmax,y=\ymax,z=#1) --  (tpp cs:x=\xmin,y=\ymax,z=#1) -- cycle}},
 xz plane at y/.style={insert path={
 (tpp cs:x=\xmin,y=#1,z=\zmin) -- (tpp cs:x=\xmax,y=#1,z=\zmin) 
    -- (tpp cs:x=\xmax,y=#1,z=\zmax) --  (tpp cs:x=\xmin,y=#1,z=\zmax) -- cycle}},
 yz plane at x/.style={insert path={
 (tpp cs:x=#1,y=\ymin,z=\zmin) -- (tpp cs:x=#1,y=\ymin,z=\zmax) 
    -- (tpp cs:x=#1,y=\ymax,z=\zmax) --  (tpp cs:x=#1,y=\ymax,z=\zmin) -- cycle}},
 x domain/.code args={#1:#2}{\def\xmin{#1}\def\xmax{#2}},x domain=-1:1,
 y domain/.code args={#1:#2}{\def\ymin{#1}\def\ymax{#2}},y domain=-1:1,
 z domain/.code args={#1:#2}{\def\zmin{#1}\def\zmax{#2}},z domain=-1:1}
\begin{document}
\begin{tikzpicture}[scale=2,isometric view,line join=round]
 % draw the sides
 \draw[fill=green!60!black,xy plane at z=1];
 \draw[fill=green!60!black,xz plane at y=-1];
 \draw[fill=green!60!black,yz plane at x=-1];
 % draw rectangles on the sides
 \draw[fill=green,y domain=-0.5:0.5,xy plane at z=1];
 \draw[fill=green,y domain=-0.5:0.5,z domain=0.7:1,yz plane at x=-1];
 \draw[fill=green,y domain=-0.5:0.5,z domain=-0.7:-1,yz plane at x=-1];
 \draw[fill=white,x domain=-0.6:0,z domain=0:0.6,xz plane at y=-1];
\end{tikzpicture}
\end{document}

在此处输入图片描述

然后可以灵活地改变3d view(这是第一个例子中的类似物\tdplotsetmaincoords{70}{130})和 的透视图perspective={...},在第一个例子中没有类似物,因为 不支持此功能tikz-3dplot。透视参数的一个可能夸张的说明是

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{perspective}
\tikzset{xy plane at z/.style={insert path={
 (tpp cs:x=\xmin,y=\ymin,z=#1) -- (tpp cs:x=\xmax,y=\ymin,z=#1) 
    -- (tpp cs:x=\xmax,y=\ymax,z=#1) --  (tpp cs:x=\xmin,y=\ymax,z=#1) -- cycle}},
 xz plane at y/.style={insert path={
 (tpp cs:x=\xmin,y=#1,z=\zmin) -- (tpp cs:x=\xmax,y=#1,z=\zmin) 
    -- (tpp cs:x=\xmax,y=#1,z=\zmax) --  (tpp cs:x=\xmin,y=#1,z=\zmax) -- cycle}},
 yz plane at x/.style={insert path={
 (tpp cs:x=#1,y=\ymin,z=\zmin) -- (tpp cs:x=#1,y=\ymin,z=\zmax) 
    -- (tpp cs:x=#1,y=\ymax,z=\zmax) --  (tpp cs:x=#1,y=\ymax,z=\zmin) -- cycle}},
 x domain/.code args={#1:#2}{\def\xmin{#1}\def\xmax{#2}},x domain=-1:1,
 y domain/.code args={#1:#2}{\def\ymin{#1}\def\ymax{#2}},y domain=-1:1,
 z domain/.code args={#1:#2}{\def\zmin{#1}\def\zmax{#2}},z domain=-1:1}
\begin{document}
\begin{tikzpicture}[scale=2,3d view={-40}{30},line join=round,
perspective={p = {(16,0,0)}, q = {(0,10,0)},r={(0,0,14)}}
]
 \draw[fill=green!60!black,xy plane at z=1];
 \draw[fill=green!60!black,xz plane at y=-1];
 \draw[fill=green!60!black,yz plane at x=-1];
 %
 \draw[fill=green,y domain=-0.5:0.5,xy plane at z=1];
 \draw[fill=green,y domain=-0.5:0.5,z domain=0.8:1,yz plane at x=-1];
 \draw[fill=green,y domain=-0.5:0.5,z domain=-0.8:-1,yz plane at x=-1];
 \draw[fill=white,x domain=-0.6:0,z domain=0:0.6,xz plane at y=-1];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容