
私の努力は
\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}
この2進数表現を3Dボックスノード型のようにtikzで描画するにはどうすればいいでしょうか。よろしくお願いします
答え1
これは 2 進数の場合の妥当な図のように思えます1111111
が、次のような 2 進数の場合、101010111
図は次のようになると思います。
以下のコードは、\BinaryNumber
コンマで区切られた2進数のリストを受け入れるマクロを定義します。これを定義したら、
\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}
基本的な考え方は、pic
各バイトを描画するために を使用することです(ティックZマニュアル)。pic は というbyte cube
2 つの引数を取ります: {0 or 1, exponent}
。pic は、数字の下の塗りつぶし色をcube 0
またはの対応するスタイルに設定して「バイト キューブ」を描画しますcube 1
。これらのスタイルを変更すると、数字の下の陰影が変わります。(したがって、設計上、スタイルの選択は 2 進数の桁によって異なります。)
の定義では、\BinaryNumber
まずバイトをループして 2 進数の「長さ」を決定し、次に再びループして各「バイト キューブ」を描画します。後続の各バイト キューブは、前のキューブの「不要な」部分を上書きして描画します。その結果、右側のシェーディングはすべてのキューブに対して描画されますが、最も右のキューブに対してのみ表示されます。
答え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}