网格的颜色单元格

网格的颜色单元格

我有以下代码(改编自https://texample.net/tikz/examples/3d-graph-model/

\documentclass[10pt]{article}
\usepackage{tikz}
%transforms all coordinates the same way when used (use it within a scope!)
%(rotation is not 45 degress to avoid overlapping edges)
% Input: point of origins x and y coordinate
\newcommand{\myGlobalTransformation}[2]
{
    \pgftransformcm{1}{0}{0.4}{0.5}{\pgfpoint{#1cm}{#2cm}}
}

% draw a 4x4 helper grid in 3D
% Input: point of origins x and y coordinate and additional drawing-parameters
\newcommand{\gridThreeD}[3]
{
    \begin{scope}
        \myGlobalTransformation{#1}{#2};
        \draw [#3,step=2cm,fill=purple!10] grid (12,12);
        \draw (0,0) rectangle (4,4) [fill=red!20];
    \end{scope}
}

\tikzstyle myBG=[line width=3pt,opacity=1.0]

% draws lines with white background to show which lines are closer to the
% viewer (Hint: draw from bottom up and from back to front)
%Input: start and end point
\newcommand{\drawLinewith}[2]
{
    %%\draw[white,myBG]  (#1) -- (#2);
    \draw[black,very thick] (#1) -- (#2);
}

% draws all horizontal graph lines within grid
\newcommand{\graphLinesHorizontal}
{
    \drawLinewith{2,2}{10,2};
    \drawLinewith{2,6}{10,6};
    \drawLinewith{2,10}{10,10};

    \drawLinewith{2,2}{2,10};
    \drawLinewith{6,2}{6,10};
    \drawLinewith{10,2}{10,10};
}

% draws all vertical graph lines within grid
\newcommand{\graphLinesVertical}
{
    %swaps x and y coordinate (hence vertical lines):
    \pgftransformcm{0}{1}{1}{0}{\pgfpoint{0cm}{0cm}}
    \graphLinesHorizontal;
}

%draws nodes of the grid
%Input: point of origins x and y coordinate
\newcommand{\graphThreeDnodes}
{
    \begin{scope}
        \myGlobalTransformation{0}{0};
        % A_00
        \node at (2,2) [circle, draw=blue!80, thick, fill=red!20] {};
        
        % A_01
        \node at (6,2) [circle, draw=blue!80, thick, fill=red!20] {0};
        \node at (10,2) [circle, draw=blue!80, thick, fill=red!20] {1};
        
        % A_10
        \node at (2,6) [circle, draw=blue!80, thick, fill=red!20] {};
        \node at (2,10) [circle, draw=blue!80, thick, fill=red!20] {};
        
        % A_11
        \node at (6,6) [circle, draw=blue!80, thick, fill=red!20] {};
        \node at (10,6) [circle, draw=blue!80, thick, fill=red!20] {};
        \node at (6,10) [circle, draw=blue!80, thick, fill=red!20] {};
        \node at (10,10) [circle, draw=blue!80, thick, fill=red!20] {};
    \end{scope}
    \begin{scope}
        \myGlobalTransformation{0}{4.25};
        % A_00
        \node at (2,2) [circle, draw=blue!80, thick, fill=blue!20] {};
        
        % A_01
        \node at (6,2) [circle, draw=blue!80, thick, fill=blue!20] {0};
        \node at (10,2) [circle, draw=blue!80, thick, fill=blue!20] {1};
        
        % A_10
        \node at (2,6) [circle, draw=blue!80, thick, fill=blue!20] {};
        \node at (2,10) [circle, draw=blue!80, thick, fill=blue!20] {};
        
        % A_11
        \node at (6,6) [circle, draw=blue!80, thick, fill=blue!20] {};
        \node at (10,6) [circle, draw=blue!80, thick, fill=blue!20] {};
        \node at (6,10) [circle, draw=blue!80, thick, fill=blue!20] {};
        \node at (10,10) [circle, draw=blue!80, thick, fill=blue!20] {};
    \end{scope}
}


\begin{document}
\pagestyle{empty}


\begin{tikzpicture}

    %draws helper-grid:
    \gridThreeD{0}{0}{black!50};
    %\gridThreeD{0}{4.25}{black!50};

    %draws lower graph lines and those in z-direction:
    \begin{scope}
        \myGlobalTransformation{0}{0};
        \graphLinesHorizontal;

        %draws all graph lines in z-direction (reset transformation first!):
        \foreach \x in {2,6,10} {
            \foreach \y in {2,6,10} {
                \node (thisNode) at (\x,\y) {};
                {
                    \pgftransformreset
                    %\draw[black,myBG]  (thisNode) -- ++(0,4.25);
                    \draw[black, opacity=1] (thisNode) -- ++(0,4.25);
                }
            }
        }
    \end{scope}

    %draws upper graph-lines:
    \begin{scope}
        \myGlobalTransformation{0}{4.25};
        \graphLinesVertical;
    \end{scope}

    % draws all graph nodes:
    \graphThreeDnodes;

    % Fill values
    \begin{scope}
        \myGlobalTransformation{0}{0};
        \node at (0.5,0.5) {A};
        \node at (0.5,1.5) {B};
        \node at (1.5,0.5) {C};
        \node at (1.5,1.5) {D};
        %\foreach \x in {2,4,6,8,10,12} {
        %    \foreach \y in {2,4,6,8,10,12} {
        %      \node at (\x-1,\y-1) [fill=red] {A} rectangle(1,1);
        %    }
        %}
      \end{scope}
\end{tikzpicture}

\end{document}

产生

在此处输入图片描述

如您所见,红色矩形隐藏了网格线。如何确保网格线不被隐藏?我基本上想用交替颜色为所有 4x4“子网格”着色。

有没有简单的方法可以将整个图片移到左边?

相关内容