如何绘制一个恰好覆盖正方形的立方体?

如何绘制一个恰好覆盖正方形的立方体?

我想要立方体的顶面是白色的,其他面是黑色的。

\documentclass{article}
\usepackage{tikz}           
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.4]
\draw[ultra thick] 
    (-0.4,0)--(8.4,0)
    (-0.4,2)--(8.4,2) 
    (-0.4,4)--(8.4,4)
    (-0.4,6)--(8.4,6)
    (-0.4,8)--(8.4,8);
\draw[ultra thick]
    (0,-0.4)--(0,8.4)
    (2,-0.4)--(2,8.4)
    (4,-0.4)--(4,8.4)
    (6,-0.4)--(6,8.4)
    (8,-0.4)--(8,8.4);
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

答案1

\documentclass[tikz,border=2mm]{standalone}
\begin{document}

\begin{tikzpicture}

\foreach \i in {-2,-1,...,3}
\draw (\i,0,-3)--(\i,0,2);

\foreach \i in {-3,-2,...,2}
\draw (-2,0,\i)--(3,0,\i);


\draw[fill=white] (0,1,0)--(0,1,1)--(1,1,1)--(1,1,0)--cycle;
\draw[fill=black] (0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle;
\draw[fill=black] (1,0,1)--(1,0,0)--(1,1,0)--(1,1,1)--cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新:添加棋盘和立方体作为图片,并以角落位置和盖子颜色作为参数

\documentclass[tikz,border=2mm]{standalone} 
\begin{document} 
\tikzset{pics/cube/.style 2 args = {
    code = {
        \begin{scope}[shift={#1}]
        \draw[fill=#2] (0,1,0)--(0,1,1)--(1,1,1)--(1,1,0)--cycle; 
        \path[] (0,1,0)--(1,1,1) node[midway, node font=\footnotesize] {#2}; 
        \draw[fill=gray!90!,] (0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle; 
        \draw[fill=gray!60!,] (1,0,1)--(1,0,0)--(1,1,0)--(1,1,1)--cycle; 
        \end{scope}
    }}} 

\begin{tikzpicture}[scale=1.5]
   \foreach \x  in {0,1,...,7}{
      \foreach \z [evaluate=\y as \back using {mod(\z+\x+1,2)*100}] in {0,1,...,7}    
            \draw[fill=black!\back!white] (\x,0,\z) --++(1,0,0) 
                         --++(0,0,1)--++(-1,0,0)--cycle;}
   \draw pic[transform shape] {cube={(2,0,2)}{pink}};
   \draw [ultra thick, red, ->] (2.5,0,3)--++(0,0,1.5)--++(1,0,0)
                         --++(0,0,2)--++(1.5,0,0);
   \draw pic[transform shape] {cube={(5,0,6)}{white}};
\end{tikzpicture}
\end{document} 

在此处输入图片描述

相关内容