如何绘制灰色编码磁盘

如何绘制灰色编码磁盘

我可以使用二进制代码、格雷码或代码表中的任何其他代码绘制编码的reglet。

在此处输入图片描述

\documentclass[border=7pt,10pt]{standalone}  
\usepackage{tikz,ifthen}
\usetikzlibrary{ positioning,calc }

\begin{document}

\begin{tikzpicture}

\foreach \cc [count=\ci]  in { 
{0 , 0 , 0 , 0},
{0 , 0 , 0 , 1},
{0 , 0 , 1 , 0},
{0 , 0 , 1 , 1},
{0 , 1 , 0 , 0}, 
{0 , 1 , 0 , 1},
{0 , 1 , 1 , 0},
{0 , 1 , 1 , 1},
{1 , 0 , 0 , 0},
{1 , 0 , 0 , 1},
{1 , 0 , 1 , 0},
{1 , 0 , 1 , 1},
{1 , 1 , 0 , 0},
{1 , 1 , 0 , 1},
{1 , 1 , 1, 0},
{1 , 1 , 1, 1}%
 }{
\foreach \nn [count=\ni] in \cc {
\ifthenelse{\nn>0}
{\def\mycol{red}}
{\def\mycol{white}}
\node[below right= \ni em and \ci em,fill=\mycol,inner sep=0,minimum size=1em,draw](n-\ci-\ni){ };
}
\pgfmathsetmacro{\NN}{int(\ci-1)}
\node[below=0em of n-\ci-4]{\footnotesize \NN};
}

\end{tikzpicture}

\begin{tikzpicture}

\foreach \cc [count=\ci]  in { 
{0 , 0 , 0 , 0},
{0 , 0 , 0 , 1},
{0 , 0 , 1 , 1},
{0 , 0 , 1 , 0},
{0 , 1 , 1 , 0},
{0 , 1 , 1 , 1},
{0 , 1 , 0 , 1},
{0 , 1 , 0 , 0}, 
{1 , 1 , 0 , 0},
{1 , 1 , 0 , 1},
{1 , 1 , 1, 1},
{1 , 1 , 1, 0},
{1 , 0 , 1 , 0},
{1 , 0 , 1 , 1},
{1 , 0 , 0 , 1},
{1 , 0 , 0 , 0}%
 }{
\foreach \nn [count=\ni] in \cc {
\ifthenelse{\nn>0}
{\def\mycol{red}}
{\def\mycol{white}}
\node[below right= \ni em and \ci em,fill=\mycol,inner sep=0,minimum size=1em,draw](n-\ci-\ni){ };
}
\pgfmathsetmacro{\NN}{int(\ci-1)}
\node[below=0em of n-\ci-4]{\footnotesize \NN};
}


\end{tikzpicture}

\end{document}

我希望能够做同样的事情,用格雷码或二进制代码(像下面那样)绘制光盘,但我迷失在循环中。

在此处输入图片描述

在此处输入图片描述

答案1

更新:线性实现。

\documentclass[border=7pt,10pt]{standalone}  
\usepackage{tikz,ifthen}
\usetikzlibrary{positioning,calc}
\begin{document}

\pgfmathsetmacro{\Rmin}{1.2}
\pgfmathsetmacro{\DeltaR}{0.5}
\pgfmathsetmacro{\StartAng}{180}
\xdef\Lst{ 
{0 , 0 , 0 , 0},
{0 , 0 , 0 , 1},
{0 , 0 , 1 , 0},
{0 , 0 , 1 , 1},
{0 , 1 , 0 , 0}, 
{0 , 1 , 0 , 1},
{0 , 1 , 1 , 0},
{0 , 1 , 1 , 1},
{1 , 0 , 0 , 0},
{1 , 0 , 0 , 1},
{1 , 0 , 1 , 0},
{1 , 0 , 1 , 1},
{1 , 1 , 0 , 0},
{1 , 1 , 0 , 1},
{1 , 1 , 1, 0},
{1 , 1 , 1, 1}%
}
\begin{tikzpicture}
% first count the number of elements
\foreach \cc [count=\ci]  in \Lst {\xdef\Ntot{\ci}}
% now draw
\foreach \cc [count=\ci]  in \Lst {
\foreach \nn [count=\ni] in \cc {
\ifthenelse{\nn>0} % since you are working with integers, you could also
{\def\mycol{red}}  % use \ifnum here
{\def\mycol{white}} % which would spare you from loading ifthenelse
\draw[fill=\mycol] (\StartAng+\ci*360/\Ntot:\Rmin+\ni*\DeltaR) 
arc(\StartAng+\ci*360/\Ntot:{\StartAng+(\ci+1)*360/\Ntot}:\Rmin+\ni*\DeltaR)
-- ({\StartAng+(\ci+1)*360/\Ntot}:{\Rmin+(\ni+1)*\DeltaR}) 
arc({\StartAng+(\ci+1)*360/\Ntot}:\StartAng+\ci*360/\Ntot:{\Rmin+(\ni+1)*\DeltaR}) -- cycle;
\ifnum \ci=1
 \pgfmathtruncatemacro{\Ni}{\ni-1}
 \node[font=\small] at ({\StartAng+(\ci+0.5)*360/\Ntot}:{\Rmin+(\ni+0.5)*\DeltaR})
 {\Ni};
\fi
\pgfmathsetmacro{\Rmax}{\Rmin+\ni*\DeltaR+\DeltaR}
\xdef\Rmax{\Rmax}
}
\pgfmathsetmacro{\NN}{int(\ci-1)}
\node[anchor={\StartAng+180+(\ci+0.5)*360/\Ntot}] at ({\StartAng+(\ci+0.5)*360/\Ntot}:\Rmax){\footnotesize \NN};
}
\end{tikzpicture}

\xdef\Lst{{0 , 0 , 0 , 0},
{0 , 0 , 0 , 1},
{0 , 0 , 1 , 1},
{0 , 0 , 1 , 0},
{0 , 1 , 1 , 0},
{0 , 1 , 1 , 1},
{0 , 1 , 0 , 1},
{0 , 1 , 0 , 0}, 
{1 , 1 , 0 , 0},
{1 , 1 , 0 , 1},
{1 , 1 , 1, 1},
{1 , 1 , 1, 0},
{1 , 0 , 1 , 0},
{1 , 0 , 1 , 1},
{1 , 0 , 0 , 1},
{1 , 0 , 0 , 0}%
}
\begin{tikzpicture}
% first count the number of elements
\foreach \cc [count=\ci]  in \Lst {\xdef\Ntot{\ci}}
% now draw
\foreach \cc [count=\ci]  in \Lst {
\foreach \nn [count=\ni] in \cc {
\ifthenelse{\nn>0} % since you are working with integers, you could also
{\def\mycol{red}}  % use \ifnum here
{\def\mycol{white}} % which would spare you from loading ifthenelse
\draw[fill=\mycol] (\StartAng+\ci*360/\Ntot:\Rmin+\ni*\DeltaR) 
arc(\StartAng+\ci*360/\Ntot:{\StartAng+(\ci+1)*360/\Ntot}:\Rmin+\ni*\DeltaR)
-- ({\StartAng+(\ci+1)*360/\Ntot}:{\Rmin+(\ni+1)*\DeltaR}) 
arc({\StartAng+(\ci+1)*360/\Ntot}:\StartAng+\ci*360/\Ntot:{\Rmin+(\ni+1)*\DeltaR}) -- cycle;
\ifnum \ci=1
 \pgfmathtruncatemacro{\Ni}{\ni-1}
 \node[font=\small] at ({\StartAng+(\ci+0.5)*360/\Ntot}:{\Rmin+(\ni+0.5)*\DeltaR})
 {\Ni};
\fi
\pgfmathsetmacro{\Rmax}{\Rmin+\ni*\DeltaR+\DeltaR}
\xdef\Rmax{\Rmax}
}
\pgfmathsetmacro{\NN}{int(\ci-1)}
\node[anchor={\StartAng+180+(\ci+0.5)*360/\Ntot}] at ({\StartAng+(\ci+0.5)*360/\Ntot}:\Rmax){\footnotesize \NN};
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

当然,你可以将其转换为处理列表的宏。如果你在执行此操作时遇到问题,请告诉我。

原始答案:如果您不认真对待这个答案,我一点也不会责怪您,因为这只是为了好玩。(鉴于此,我想说,它的效果出奇地好。;-)编辑:固定径向尺寸。

\documentclass[border=7pt,10pt]{standalone}  
\usepackage{tikz,ifthen}
\usetikzlibrary{positioning,calc}
\usepgfmodule{nonlineartransformations}

\makeatletter
% from https://tex.stackexchange.com/a/434247/121799
\tikzdeclarecoordinatesystem{polar}{
    \tikz@scan@one@point\relax(#1)
    \polartransformation
}
% from the pgfmanual
\def\polartransformation{% from the pgfmanual section 103.4.2
\pgfmathsincos@{\pgf@sys@tonumber\pgf@x}%
\pgf@x=\pgfmathresultx\pgf@y% 
\pgf@y=\pgfmathresulty\pgf@y%
} % note: the following should work with arbitrary (nonlinear) transformations
\makeatother
\begin{document}

\begin{tikzpicture}
\begin{scope}[transform shape nonlinear=true]
\pgftransformnonlinear{\polartransformation}
\foreach \cc [count=\ci]  in { 
{0 , 0 , 0 , 0},
{0 , 0 , 0 , 1},
{0 , 0 , 1 , 0},
{0 , 0 , 1 , 1},
{0 , 1 , 0 , 0}, 
{0 , 1 , 0 , 1},
{0 , 1 , 1 , 0},
{0 , 1 , 1 , 1},
{1 , 0 , 0 , 0},
{1 , 0 , 0 , 1},
{1 , 0 , 1 , 0},
{1 , 0 , 1 , 1},
{1 , 1 , 0 , 0},
{1 , 1 , 0 , 1},
{1 , 1 , 1, 0},
{1 , 1 , 1, 1}%
 }{
\foreach \nn [count=\ni] in \cc {
\ifthenelse{\nn>0}
{\def\mycol{red}}
{\def\mycol{white}}
\node[below right= \ni em and 2.25*\ci em,fill=\mycol,inner sep=0,minimum
width=2.25em,minimum height=1em,draw](n-\ci-\ni){ };
}
\pgfmathsetmacro{\NN}{int(\ci-1)}
\node[below=0em of n-\ci-4]{\footnotesize \NN};
}
\end{scope}
\end{tikzpicture}

\begin{tikzpicture}
\begin{scope}[transform shape nonlinear=true]
\pgftransformnonlinear{\polartransformation}
\foreach \cc [count=\ci]  in { 
{0 , 0 , 0 , 0},
{0 , 0 , 0 , 1},
{0 , 0 , 1 , 1},
{0 , 0 , 1 , 0},
{0 , 1 , 1 , 0},
{0 , 1 , 1 , 1},
{0 , 1 , 0 , 1},
{0 , 1 , 0 , 0}, 
{1 , 1 , 0 , 0},
{1 , 1 , 0 , 1},
{1 , 1 , 1, 1},
{1 , 1 , 1, 0},
{1 , 0 , 1 , 0},
{1 , 0 , 1 , 1},
{1 , 0 , 0 , 1},
{1 , 0 , 0 , 0}%
 }{
\foreach \nn [count=\ni] in \cc {
\ifthenelse{\nn>0}
{\def\mycol{red}}
{\def\mycol{white}}
\node[below right= \ni em and 2.25*\ci em,fill=\mycol,inner sep=0,minimum
width=2.25em,minimum height=1em,draw](n-\ci-\ni){ };
}
\pgfmathsetmacro{\NN}{int(\ci-1)}
\node[below=0em of n-\ci-4]{\footnotesize \NN};
}
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容