[Tex/LaTex] How to color several transparent planes in a cube

tikz-pgf

I'm trying to reproduce the following two figures in TikZ.

Fig 1
Fig 2

My attempted code for Fig 1 is

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    \draw [fill=black, ultra thick, red]  (0.0,0.0) -- (0.5, 0.5) -- (0.5, 1.5) -- (0.0, 1.0) -- cycle;
    \draw [fill=black, ultra thick, blue] (1.0,0.0) -- (1.5, 0.5)-- (1.5, 1.5)--(1.0, 1.0)--cycle;
    \draw  (0.0,0.0)  -- (1.0, 0.0)-- (1.5, 0.5)--(0.5, 0.5)--cycle;
    \draw  (0.0,1.0) -- (1.0, 1.0)-- (1.5, 1.5)--(0.5, 1.5)--cycle;
    \end{tikzpicture}
\end{document}

My Fig 1

and for the second figure the code is

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    \draw [fill=black, ultra thick, red]  (1.0,0.0) -- (1.0, 1.0) -- (0.5, 1.5) -- (0.5, 0.5) -- cycle;
    \draw [fill=black, ultra thick, blue] (0.0,0.0) -- (0.0, 1.0)-- (1.5, 1.5)--(1.5, 0.5)--cycle;
    \draw  (0.0,0.0)  -- (1.0, 0.0)-- (1.5, 0.5)--(0.5, 0.5)--cycle;
    \draw  (0.0,1.0) -- (1.0, 1.0)-- (1.5, 1.5)--(0.5, 1.5)--cycle;
    \draw  (1.0, 0.0) -- (1.0, 1.0)  (0.5, 0.5) -- (0.5, 1.5);
    \end{tikzpicture}
\end{document}

My Fig 2

I'm a TikZ newbie and have troubles to produce exactly the same figures with more efficient code. I've a problem with overlays and exact coordinates. I'd highly appreciate if you could help me to fix these problems and get similar figures with more robust code.

Best Answer

It is often forgotten that tikZ also has an xyz coordinate system. I think it lets you express the coordinates in a more natural way. (I also added the circles.)

\documentclass{minimal}
\usepackage{tikz}      
\usetikzlibrary{calc}  

\begin{document}
    \begin{tikzpicture}[scale=4,fill opacity=0.4,thick,
                        line cap=round,line join=round]
    %% Define coordinate labels.
    % t(op) and b(ottom) layers
    \path \foreach \layer/\direction in {b/{0,0,0},t/{0,1,0}} {
        (\direction)
        \foreach \point/\label in {{0,0,0}/ll,{1,0,0}/lr,{1,0,-1}/ur,{0,0,-1}/ul} {
            +(\point) coordinate (\layer\label)
        }
        ($(\layer ll)!0.5!(\layer ur)$) coordinate (\layer md)
    };

    % Put text next to the labels as requested.
    % Funilly enough we need to set fill opacity to 1.
    \draw \foreach \text/\label/\anchor in {%
        one/bll/east,
        two/bul/east,
        three/tll/east,
        four/tul/east,
        five/blr/west,
        six/bur/west,
        seven/tlr/west,
        eight/tur/west} {
        (\label) node[anchor=\anchor,fill opacity=1] {\text}
    };

    % Draw left cube.
    \fill (0,0,-1) circle (0.5pt);
    \foreach \direction in {(0,0,1),(0,1,0),(1,0,0)} {
        \draw[dashed,black] (bul) -- + \direction;
    }
    \fill[blue!60] (bmd) -- (bur) -- (tur) -- (tmd);
    \fill[red!60]  (blr) -- (tlr) -- (tul) -- (bul);
    \fill[blue!60] (bll) -- (bmd) -- (tmd) -- (tll);
    \draw (bll) -- (blr) -- (tlr) -- (tll) -- cycle;
    \draw (blr) -- (bur) -- (tur) -- (tlr) -- cycle;
    \draw (tll) -- (tlr) -- (tur) -- (tul) -- cycle;
    \foreach \point in {bll,blr,bur,tll,tlr,tul,tur} {
        \fill[fill opacity=1] (\point) circle (0.75pt);
    }

    % Draw right cube.
    \path (blr) + (0.65,0) coordinate (pos);
    \foreach \direction in {(0,0,1),(0,1,0),(1,0,0)} {
        \draw[dashed] (pos) ++ (bul) -- + \direction;
    }
    \fill[blue!60] (pos) +(blr) -- +(bur) -- +(tur) -- +(tlr);
    \fill[red!60]  (pos) +(bll) -- +(bul) -- +(tul) -- +(tll);
    \draw (pos) +(tll) -- +(tlr) -- +(tur) -- +(tul) -- cycle
                +(tll) -- +(bll) -- +(blr) -- +(bur) -- +(tur)
                +(blr) -- +(tlr);
    \end{tikzpicture}
\end{document}

cube with shaded sides