Tikz 3D 图:切过立方体的平面

Tikz 3D 图:切过立方体的平面

我想画一个与平面相交的立方体,同时突出显示相交区域。到目前为止,我已经完成了下面的图。不幸的是,平面看起来好像在立方体的前面。我希望切角位于平面的前面。

非常感谢您的帮助。

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns,perspective}
%
\begin{document}
    \begin{tikzpicture}[3d view={120}{15},line join=round,
        declare function={a=4;b=2;}]
        \draw[style=dashed, color=black] (a,0,-a) -- (0,0,-a)-- (0,a,-a);
        \draw[style=dashed, color=black] (0,0,0) -- (0,0,-a); 
        \draw[thick,black] (a,0,0) -- (a,0,-a) -- (a,a,-a) -- (a,a,-b);
        \draw[thick,black] (a,a-b,0) -- (a,0,0) -- (0,0,0) -- (0,a,0) -- (a-b,a,0);
        \draw[thick,black] (0,a,0) -- (0,a,-a) -- (a,a,-a);
        \draw[thick,black] (a-b,a,0) -- (a,a,0) -- (a,a-b,0);
        \draw[thick,black] (a,a,0) -- (a,a,-b); 
        \draw[thick,pattern=north east lines] (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle;
        \fill[gray,opacity=0.2] (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle;
    \end{tikzpicture}%
\end{document}%

答案1

我为你做了一点东西:

这是通过在范围内使用剪切路径来实现的,如下所示:

\begin{scope}
    \clip
        (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle % the plane
        (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle; % the triangle cutout 
    \draw[fill=blue, draw=none, opacity=0.5] (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle; 
\end{scope}

通过从平面中减去三角形,您可以定义范围的剪贴蒙版。然后\clip使用 填充/绘制整个平面区域\draw,然后将其绘制在剪贴蒙版上。

整个代码:

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

\begin{document}
    \begin{tikzpicture}[3d view={200}{15},line join=round, thick,
        declare function={a=4;b=2;}]
        
        % the cube edges
        \draw[dashed] (a,0,-a) -- (0,0,-a)-- (0,a,-a);
        \draw[dashed] (0,0,0) -- (0,0,-a); 
        \draw (a,0,0) -- (a,0,-a) -- (a,a,-a) -- (a,a,-b);
        \draw (a,a-b,0) -- (a,0,0) -- (0,0,0) -- (0,a,0) -- (a-b,a,0);
        \draw (0,a,0) -- (0,a,-a) -- (a,a,-a);
        \draw (a-b,a,0) -- (a,a,0) -- (a,a-b,0);
        \draw (a,a,0) -- (a,a,-b); 
        
        % the cube/plane cross-section surface
%       \draw[fill=white, fill opacity=0.2, draw opacity=0.8] (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle;
            
        % the plane
        \begin{scope}
            \clip
            (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle % the plane
            (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle; % cutout A (cross section)
%           (a,a,0) -- (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle; % cutout B (cube corner)
            \draw[fill=blue, draw=none, opacity=0.5] (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle;
        \end{scope}
        
%        % the cube corner surface        
%       \draw[fill=white, opacity=0.8] (a,a,0) -- (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle;

        % the cube corner edges
        \draw (a,a,0) -- (a,a,-b) (a,a,0) -- (a-b,a,0) (a,a,0) -- (a,a-b,0); 
    \end{tikzpicture}
\end{document}

如您所见,我注释掉了一些行;这些是一些可选的修改,供您试验。您可以注释掉“cutout A”,改用“cutout B”。或者/如果您打算用纯色填充立体角,请取消注释绘制立体角表面的行。

最后,我在代码中添加了一些结构和注释。我之所以提到这一点,是因为如果您计划在将来更广泛地使用 TikZ,那么如果您尽早习惯在代码中构造和标记组件,那么这对您来说是一个很大的帮助。

答案2

您可以在平面后画出角,这样它就不会被遮挡。

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{patterns,perspective}
\begin{document}
    \begin{tikzpicture}[3d view={120}{15},line join=round, thick,
        declare function={a=4;b=2;}]
        \draw[dashed] (a,0,-a) -- (0,0,-a)-- (0,a,-a);
        \draw[dashed] (0,0,0) -- (0,0,-a); 
        \draw (a,0,0) -- (a,0,-a) -- (a,a,-a) -- (a,a,-b);
        \draw (a,a-b,0) -- (a,0,0) -- (0,0,0) -- (0,a,0) -- (a-b,a,0);
        \draw (0,a,0) -- (0,a,-a) -- (a,a,-a);
        \draw (a-b,a,0) -- (a,a,0) -- (a,a-b,0);
        \draw (a,a,0) -- (a,a,-b); 
        \fill[lightgray, opacity=0.8] (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle;
        \draw[pattern=north east lines] (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle;
        \draw (a,a,0) -- (a,a,-b) (a,a,0) -- (a-b,a,0) (a,a,0) -- (a,a-b,0);
    \end{tikzpicture}
\end{document}

带平面切角的立方体

答案3

除了 @hpekristiansen 的答案之外,您还可以在图案后面绘制一个半透明的白色背景,使其在平面上脱颖而出。只需添加preaction={fill=white, opacity=0.8}pattern=north east lines

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{patterns,perspective}
\begin{document}
    \begin{tikzpicture}[3d view={120}{15},line join=round, thick,
        declare function={a=4;b=2;}]
        \draw[dashed] (a,0,-a) -- (0,0,-a)-- (0,a,-a);
        \draw[dashed] (0,0,0) -- (0,0,-a); 
        \draw (a,0,0) -- (a,0,-a) -- (a,a,-a) -- (a,a,-b);
        \draw (a,a-b,0) -- (a,0,0) -- (0,0,0) -- (0,a,0) -- (a-b,a,0);
        \draw (0,a,0) -- (0,a,-a) -- (a,a,-a);
        \draw (a-b,a,0) -- (a,a,0) -- (a,a-b,0);
        \draw (a,a,0) -- (a,a,-b); 
        \fill[lightgray, opacity=0.8] (4,0.5,1.5) -- (0.5,4,1.5) -- (2.75,6.25,-3) -- (6.25,2.75,-3) -- cycle;
        \draw[pattern=north east lines, preaction={fill=white, opacity=0.8}] (a,a,-b) -- (a-b,a,0) -- (a,a-b,0) -- cycle;
        \draw (a,a,0) -- (a,a,-b) (a,a,0) -- (a-b,a,0) (a,a,0) -- (a,a-b,0);
    \end{tikzpicture}
\end{document}

结果:

结果

相关内容