在tikz中从不同的坐标点进行相对绘制

在tikz中从不同的坐标点进行相对绘制

这是一个有点复杂的问题,我很难解释,我会尽力。

为此,我将给出一个我在 tikz 上已有的绘图示例。在其中,我绘制了一个长方体,里面有一个子长方体,然后我将子长方体从绘图中取出并放置在旁边:

cuboids image

我所用的代码如下:

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}[scale=0.3]
        % Cuboide
        \draw[-] (0,0) -- (0,10);
        \draw[-] (0,10) -- (11.2,10);
        \draw[-] (11.2,10) -- (11.2,0);
        \draw[-] (11.2,0) -- (0,0) node at (6,-1.5) {112};

        \draw[stealth-stealth] (-1,3) -- (-1,10) node at (-2.5,6.5) {85};

        \draw[-] (4.1,4.1) -- (4.1,15.3);
        \draw[-] (4.1,15.3) -- (15.3,15.3);
        \draw[-] (15.3,15.3) -- (15.3,4.1) node at (14,1.4) {71};
        \draw[-] (15.3,4.1) -- (4.1,4.1);       

        \draw[-] (0,0) -- (4.1,4.1);
        \draw[-] (0,10) -- (4.1,15.3);
        \draw[-] (11.2,10) -- (15.3,15.3);
        \draw[-] (11.2,0) -- (15.3,4.1);

        % Subcuboide
        \draw[-] (0,3) -- (11.2,3);
        \draw[-] (4.1,7.1) -- (15.3,7.1);

        \draw[-] (0,3) -- (4.1,7.1);
        \draw[-] (11.2,3) -- (15.3,7.1);

        % Subcuboide independiente
        \draw[-stealth] (16,3) -- (20,3);

        \draw[-] (21,0) -- (32.2,0);
        \draw[-] (21,0) -- (21,3) node at (20,1.4) {15};
        \draw[-] (21,3) -- (32.2,3);        
        \draw[-] (32.2,3) -- (32.2,0);  

        \draw[-] (25.1,4.1) -- (36.3,4.1);
        \draw[-] (25.1,4.1) -- (25.1,7.1);
        \draw[-] (25.1,7.1) -- (36.3,7.1);
        \draw[-] (36.3,7.1) -- (36.3,4.1);

        \draw[-] (21,0) -- (25.1,4.1);
        \draw[-] (32.2,0) -- (36.3,4.1);
        \draw[-] (21,3) -- (25.1,7.1);
        \draw[-] (32.2,3) -- (36.3,7.1);

    \end{tikzpicture}
\end{figure}

如您所见,这样做的方法非常粗糙,从轴的坐标绘制每条线。对于绘图来说没有问题,但在这种情况下,绘制第二个长方体时变得非常繁琐(我必须计算与之对应的坐标)。

是否有某种方法可以绘制相同的图形,但两个长方体(或其他任何图形)都从坐标开始0,0

答案1

scope可以使用A来实现这一点。还可以考虑使用 3D 坐标,如下所示:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[line join=round]
\draw (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle; 
\draw (0,2,0) -- (2,2,0) -- (2,2,2) -- (0,2,2) -- cycle;
\draw (0,0,0) -- (0,2,0) (2,0,0) -- (2,2,0) (0,0,2) -- (0,2,2) (2,0,2) -- (2,2,2); 
\draw (0,0.5,0) -- (2,0.5,0) -- (2,0.5,2) -- (0,0.5,2) -- cycle;
\begin{scope}[xshift=3cm]
\draw (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle; 
\draw (0,0.5,0) -- (2,0.5,0) -- (2,0.5,2) -- (0,0.5,2) -- cycle;
\draw (0,0,0) -- (0,0.5,0) (2,0,0) -- (2,0.5,0) (0,0,2) -- (0,0.5,2) (2,0,2) -- (2,0.5,2); 
\end{scope}
\end{tikzpicture}
\end{document}

A cube and a cuboid

编辑:彩色版本显示了什么:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[line join=round, very thick]
\draw[red]   (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle; 
\draw[green] (0,2,0) -- (2,2,0) -- (2,2,2) -- (0,2,2) -- cycle;
\draw[blue]  (0,0,0) -- (0,2,0) (2,0,0) -- (2,2,0) (0,0,2) -- (0,2,2) (2,0,2) -- (2,2,2); 
\draw[black] (0,0.5,0) -- (2,0.5,0) -- (2,0.5,2) -- (0,0.5,2) -- cycle;
\begin{scope}[xshift=3cm]
\draw[red]   (0,0,0) -- (2,0,0) -- (2,0,2) -- (0,0,2) -- cycle; 
\draw[black] (0,0.5,0) -- (2,0.5,0) -- (2,0.5,2) -- (0,0.5,2) -- cycle;
\draw[teal]  (0,0,0) -- (0,0.5,0) (2,0,0) -- (2,0.5,0) (0,0,2) -- (0,0.5,2) (2,0,2) -- (2,0.5,2); 
\end{scope}
\end{tikzpicture}
\end{document}

A cube and a cuboid with colored lines

相关内容