使用 tikz 绘制透视三维形状

使用 tikz 绘制透视三维形状

是否可以绘制如下图所示的透视平行六面体(带有颜色、透明度、边缘等)?使用 tikz 是否可以轻松实现,还是需要其他库?

注意:左上角和右上角的红色和蓝色圆圈是消失点。我将原点放在一个角(点 O),这样定义高度(Oz)、宽度(Ox)和长度(Oy)就足以获得形状。也许消失点可以由一对坐标(例如(半径,角度))相对于 2D 轴系统来定义。我只是在推测这些想法,我真的不知道如何做到这一点。这就是我无法提供任何 MWE 的原因(抱歉)。

在此处输入图片描述

编辑:

在@HenriMenke 针对 tikz 手册发表评论后,我尝试了直接从文档中提取的以下简单代码。但是,在 Overleaf 中进行编译不会打印任何内容,并且会导致错误。

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{perspective}

\newcommand\simplecuboid[3]{%
    \fill[gray!80!white]
        (tpp cs:x=0,y=0,z=#3)
            -- (tpp cs:x=0,y=#2,z=#3)
            -- (tpp cs:x=#1,y=#2,z=#3)
            -- (tpp cs:x=#1,y=0,z=#3) -- cycle;
    \fill[gray]
        (tpp cs:x=0,y=0,z=0)
            -- (tpp cs:x=0,y=0,z=#3)
            -- (tpp cs:x=0,y=#2,z=#3)
            -- (tpp cs:x=0,y=#2,z=0) -- cycle;
    \fill[gray!50!white]
        (tpp cs:x=0,y=0,z=0)
            -- (tpp cs:x=0,y=0,z=#3)
            -- (tpp cs:x=#1,y=0,z=#3)
            -- (tpp cs:x=#1,y=0,z=0) -- cycle;
    }

\begin{document}

\begin{tikzpicture}[3d view, perspective={p={(2,0,0)},q={(0,2,0)},r={(0,0,2)}}]
    \simplecuboid{3}{1}{2}
\end{tikzpicture}

\end{document}

答案1

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{perspective}

\newcommand\simplecuboid[3]{%
    \fill[red!50,opacity=0.5]
        (tpp cs:x=0,y=0,z=#3)
            -- (tpp cs:x=0,y=#2,z=#3)
            -- (tpp cs:x=#1,y=#2,z=#3)
            -- (tpp cs:x=#1,y=0,z=#3) -- cycle;
    \fill[red,opacity=0.5]
        (tpp cs:x=0,y=0,z=0)
            -- (tpp cs:x=0,y=0,z=#3)
            -- (tpp cs:x=0,y=#2,z=#3)
            -- (tpp cs:x=0,y=#2,z=0) -- cycle;
    \fill[red!80,opacity=0.5]
        (tpp cs:x=0,y=0,z=0)
            -- (tpp cs:x=0,y=0,z=#3)
            -- (tpp cs:x=#1,y=0,z=#3)
            -- (tpp cs:x=#1,y=0,z=0) -- cycle;
    }

\begin{document}

\begin{tikzpicture}[
    3d view={-55}{80},
    perspective={
    p = {(2,0,0)},
    q = {(0,2,0)}},
    scale=3,
    vanishing point/.style={draw,circle,inner sep=1pt},
    cuboid point/.style={fill,circle,inner sep=1pt}]
    
    \node[vanishing point,fill=cyan!50,label = right:p] (p) at (2,0,0){};
    \node[vanishing point,fill=red,label = left:q] (q) at (0,2,0){};

    \foreach \y in {0,1.5}{
        \draw[red!20!black] (tpp cs:x=0,y=\y,z=0) -- (tpp cs:x=0,y=\y,z=2);
        \draw[red!20!black] (tpp cs:x=1,y=\y,z=0) -- (tpp cs:x=1,y=\y,z=2);
        \foreach \z in {0,2}{
            \draw[dotted] (tpp cs:x=1,y=\y,z=\z) -- (p.center);
            \draw[red!30!black] (tpp cs:x=0,y=\y,z=\z) -- (tpp cs:x=1,y=\y,z=\z);}}
    \foreach \x in {0,1}{
        \foreach \z in {0,2}{
            \draw[dotted] (tpp cs:x=\x,y=1.5,z=\z) -- (q.center);
            \draw[red!20!black] (tpp cs:x=\x,y=0,z=\z) -- (tpp cs:x=\x,y=1.5,z=\z);}}

    
    \simplecuboid{1}{1.5}{2}

    \begin{scope}[every node/.style={cuboid point, anchor=center}]
        \node[label=above:{$x$}] at (tpp cs:x=0,y=1.5,z=2) {};
        \node[label=above:{$y$}] at (tpp cs:x=1,y=1.5,z=2) {};
        \node[label=above:{$z$}] at (tpp cs:x=1,y=0,z=2) {};
        \node[label=below:{$O$}] at (tpp cs:x=0,y=0,z=0) {};
    \end{scope}
    \node[label={[label distance=-3pt,rotate=60]below:{\small$Ox$}}] at (tpp cs:x=0.5,y=0,z=0) {};
    \node[label={[label distance=-3pt,rotate=-30]below:{\small$Oy$}}] at (tpp cs:x=0,y=0.75,z=0) {};
    \node[label={[rotate=90]above:{\small$Oz$}}] at (tpp cs:x=0,y=0,z=1) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容