3d tikz 노드 상자의 이진수 표현

3d tikz 노드 상자의 이진수 표현

나의 노력은

\documentclass[margin=1in]{standalone}

\usepackage{tikz}
\usetikzlibrary[arrows, decorations.pathmorphing, backgrounds, positioning, fit, petri]


\begin{document}
    \begin{tikzpicture}[node distance=0pt,
        box/.style={draw, minimum size=1cm, inner sep=0.5cm},
        value/.style={yshift=-1cm}]
        \node[box] (b7) {1};
        \node[box] (b6) [right=of b7] {1};
        \node[box] (b5) [right=of b6] {1};
        \node[box] (b4) [right=of b5] {1};
        \node[box] (b3) [right=of b4] {1};
        \node[box] (b2) [right=of b3] {1};
        \node[box] (b1) [right=of b2] {1};
        \node[box] (b0) [right=of b1] {1};
        
        \node[value] [below of=b0] {$2^0$};
        \node[value] [below of=b1] {$2^1$};
        \node[value] [below of=b2] {$2^2$};
        \node[value] [below of=b3] {$2^3$};
        \node[value] [below of=b4] {$2^4$};
        \node[value] [below of=b5] {$2^5$};
        \node[value] [below of=b6] {$2^6$};
        \node[value] [below of=b7] {$2^7$};
    \end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

3D 상자 노드 유형과 같은 tikz에서 이 이진수 표현을 어떻게 그릴 수 있습니까? 감사해요

여기에 이미지 설명을 입력하세요

답변1

이것은 이진수에 대한 합리적인 그림처럼 보이지만 1111111이진수에 대한 101010111그림은 다음과 같아야 한다고 생각합니다.

여기에 이미지 설명을 입력하세요

아래 코드는 \BinaryNumber쉼표로 구분된 이진수 목록을 허용하는 매크로를 정의합니다. 이것이 정의되면 다음을 사용할 수 있습니다.

  \BinaryNumber{1,1,1,1,1,1,1,1,1}
  \BinaryNumber{1,0,1,0,1,1,1}
  \BinaryNumber{1,0,1,1,1,0,1,0,0,1,1}

생산하는:

여기에 이미지 설명을 입력하세요

코드는 다음과 같습니다. 아래에서 어떻게 작동하는지 조금 설명하겠습니다.

\documentclass{article}
\usepackage{tikz}

\tikzset{
  pics/byte cube/.style args = {#1,#2}{
      code = {
         \draw[fill=white] (0,0) rectangle (1,1);
         \node at (0.5,0.5){#1};
         \draw[cube #1] (0,0)--(-60:2mm)--++(1,0)--++(0,1)--++(120:2mm)--(1,0)--cycle;
         \draw(1,0)--++(-60:2mm);
         \node at (0.5,-0.5){$2^{#2}$};
      }
    },
    cube 1/.style = {fill=gray!30}, % style for bytes that are "on"
    cube 0/.style = {fill=white},   % style for bytes that are "off"
}

\newcommand\BinaryNumber[1]{%
  \begin{tikzpicture}
     % count the number of bytes and store as \C
     \foreach \i [count=\c] in {#1} { \xdef\C{\c} }
     \foreach \i [count=\c, evaluate=\c as \ex using {int(\C-\c)}] in {#1} {
       \pic at (\c, 1) {byte cube={\i,\ex}};
     }
  \end{tikzpicture}

}
\begin{document}

  \BinaryNumber{1,1,1,1,1,1,1,1,1}          \bigskip

  \BinaryNumber{1,0,1,0,1,1,1}              \bigskip

  \BinaryNumber{1,0,1,1,1,0,1,0,0,1,1}      \bigskip

\end{document}

주요 아이디어는 a를 사용하여 pic각 바이트를 그리는 것입니다(섹션 18.2 참조).TikZ수동). pic이라는 이름은 byte cube두 가지 인수를 취합니다: {0 or 1, exponent}. 그림은 cube 0또는 의 해당 스타일로 설정된 숫자 아래에 채우기 색상이 있는 "바이트 큐브"를 그립니다 cube 1. 이러한 스타일을 변경하면 숫자 아래의 음영이 변경됩니다. (그래서 설계상 스타일 선택은 이진수에 따라 달라집니다.)

첫 번째 정의는 \BinaryNumber바이트를 반복하여 이진수의 "길이"를 결정한 다음 이를 다시 반복하여 각 "바이트 큐브"를 그립니다. 각 연속 바이트 큐브는 "원하지 않는" 이전 큐브 부분을 그립니다. 결과적으로 오른쪽의 음영은 모든 큐브에 대해 그려지더라도 가장 오른쪽 큐브에만 표시됩니다.

답변2

첫 번째 쉬운 해결책은 다음과 같습니다.

\documentclass[margin=1in]{standalone}

\usepackage{tikz}
\usetikzlibrary[arrows, decorations.pathmorphing, backgrounds, positioning, fit, petri]

\begin{document}
    \begin{tikzpicture}[node distance=0pt,
        box/.style={draw, minimum size=1cm, inner sep=0.5cm},
        value/.style={yshift=-1cm}]
        \node[box] (b7) {1};
        \node[box] (b6) [right=of b7] {1};
        \node[box] (b5) [right=of b6] {1};
        \node[box] (b4) [right=of b5] {1};
        \node[box] (b3) [right=of b4] {1};
        \node[box] (b2) [right=of b3] {1};
        \node[box] (b1) [right=of b2] {1};
        \node[box] (b0) [right=of b1] {1};
\def\xasn{1mm}% x direction of the box
\def\yasn{-1.5mm}% y direction of the box
\fill[lightgray] (b0.north east) -- ([shift={(\xasn,\yasn)}]b0.north east) -- ([shift={(\xasn,\yasn)}]b0.south east) -- (b0.south east);
\draw (b0.north east) -- ([shift={(\xasn,\yasn)}]b0.north east) -- ([shift={(\xasn,\yasn)}]b0.south east);

\foreach \boxnr in {0,1,...,7} {
\draw[fill=lightgray] (b\boxnr.south west) -- +(\xasn,\yasn) -- ([shift={(\xasn,\yasn)}]b\boxnr.south east) -- (b\boxnr.south east);
}
        
        \node[value] [below of=b0] {$2^0$};
        \node[value] [below of=b1] {$2^1$};
        \node[value] [below of=b2] {$2^2$};
        \node[value] [below of=b3] {$2^3$};
        \node[value] [below of=b4] {$2^4$};
        \node[value] [below of=b5] {$2^5$};
        \node[value] [below of=b6] {$2^6$};
        \node[value] [below of=b7] {$2^7$};
    \end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보