如何用 tikz 绘制凯撒密码图?

如何用 tikz 绘制凯撒密码图?

我刚刚尝试开始重写这样的圆圈代码 使用 tkz-euclide 绘制时钟,但我不知道如何编写循环代码的框架和箭头。我希望显示循环代码如何使用例如凯撒密码来加密/解密单词。 在此处输入图片描述

编辑: 在评论中提出建议后,我重写了示例代码来解决了我的问题。现在,我可以在带有数字的图片上绘制箭头了吗?

\documentclass{article}

\usepackage{chancery}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\tikzset{curved text/.style={
  decoration={text effects along path,
  text/.expanded=#1, text align = center,
  text effects/.cd, text along path, decorate}}
}
\begin{document}

\begin{tikzpicture}[x = 1em, y = 1em]
\draw circle [radius = 15] circle [radius = 12] circle [radius = 9]
  \foreach \i in {0,...,25}{ (\i*13.85:15) -- (\i*13.85:12) -- (\i*13.85:9) };
\node [align=center] {Caesar \\ Cipher};
\foreach [count=\a from 0] \text in {%
{X},
{Y},
{Z},
{A},
{B},
{C},
{D},
{E},
{F},
{G},
{H},
{I},
{J},
{K},
{L},
{M},
{N},
{O},
{P},
{Q},
{R},
{S},
{T},
{U},
{V},
{W}
}
\foreach \t [count=\r] in \text
  \path [curved text=\t, rotate=-\a*13.85]
    (90:15-\r) arc (90:13.85:15-\r);

\foreach [count=\b from 0] \text in {%
{A},
{B},
{C},
{D},
{E},
{F},
{G},
{H},
{I},
{J},
{K},
{L},
{M},
{N},
{O},
{P},
{Q},
{R},
{S},
{T},
{U},
{V},
{W},
{X},
{Y},
{Z}
}
\foreach \t [count=\r] in \text
  \path [curved text=\t, rotate=-\b*13.85]
    (90:12-\r) arc (90:13.85:12-\r);
\end{tikzpicture}

\end{document}

答案1

像这样吗?

案例代码

\documentclass[tikz,border=2mm]{standalone}
\usepackage{libertine} % Change the font to your liking
\usepackage[T1]{fontenc}
\usetikzlibrary{decorations.text,arrows.meta,calc}

\tikzset{curved text/.style={decorate,
        decoration={text effects along path,
            text={#1}, text align=center,
            text effects/.cd, text along path}}}

\begin{document}
    \begin{tikzpicture}[x=1em,y=1em]
%   set up
    \pgfmathsetmacro\angdiv{360/26}
    \pgfmathtruncatemacro\caeser{7} % Input Caeser shift here! (positive for clockwise)
    \coordinate (n-0) at (90+\angdiv/2:7) {};
    \coordinate (m-0) at (90-\caeser*\angdiv+\angdiv/2:5) {};
%   draw Caeser diagram 
    \draw circle [radius=8] circle [radius=6.5] circle [radius=6]  circle [radius=4.5]
        \foreach \i in {0,...,25}{%
            ($({90-(\i-1/2)*\angdiv}:8)$) -- ($(({90-(\i-1/2)*\angdiv}:6.5)$)
            ($({90-(\i-1/2)*\angdiv}:4.5)$) -- ($(({90-(\i-1/2)*\angdiv}:6)$)
        };
    \foreach [count=\a from 0] \text in {A,B,...,Z}{
        \pgfmathtruncatemacro\b{\a+1}%
        \path [curved text=\text] (n-\a) arc [start angle=90-(\a-1/2)*\angdiv, delta angle=-\angdiv, radius=7] node (n-\b) {};
        \path [curved text=\text] (m-\a) arc [start angle=90-(\a+\caeser-1/2)*\angdiv, delta angle=-\angdiv, radius=5] node (m-\b) {}; % Inner circle
    }
%   draw arrows
    \draw [arrows={-Latex}]
 (65:9.5) to[bend left=20,edge label=$+3$] (40:9.5);
    \end{tikzpicture}
\end{document}

您最有可能需要在此代码中更改的是凯撒移位,您可以在 行中进行更改\pgfmathtruncatemacro\caeser{7}。我还在代码中指出了它的确切位置。请注意,正凯撒移位是顺时针平移。


额外内容:(原帖作者未询问)

例如,此方法可扩展到有 5 个字母的凯撒轮。我在代码中指出了进行更改以产生给定输出的位置。

在此处输入图片描述

\documentclass[tikz,border=2mm]{standalone}
\usepackage{libertine} % Change the font to your liking
\usepackage[T1]{fontenc}
\usetikzlibrary{decorations.text,arrows.meta,calc}

\tikzset{curved text/.style={decorate,
        decoration={text effects along path,
            text={#1}, text align=center,
            text effects/.cd, text along path}}}

\begin{document}
    \begin{tikzpicture}[x=1em,y=1em]
    %   set up
    \pgfmathsetmacro\angdiv{360/5} <-----------------
    \pgfmathtruncatemacro\caeser{2} % Input Caeser shift here! (positive for clockwise)
    \coordinate (n-0) at (90+\angdiv/2:7) {};
    \coordinate (m-0) at (90-\caeser*\angdiv+\angdiv/2:5) {};
    %   draw Caeser diagram 
    \draw circle [radius=8] circle [radius=6.5] circle [radius=6]  circle [radius=4.5]
    \foreach \i in {0,...,4}{% <-----------------------
        ($({90-(\i-1/2)*\angdiv}:8)$) -- ($(({90-(\i-1/2)*\angdiv}:6.5)$)
        ($({90-(\i-1/2)*\angdiv}:4.5)$) -- ($(({90-(\i-1/2)*\angdiv}:6)$)
    };
    \foreach [count=\a from 0] \text in {A,B,...,E}{% <------------------
        \pgfmathtruncatemacro\b{\a+1}%
        \path [curved text=\text] (n-\a) arc [start angle=90-(\a-1/2)*\angdiv, delta angle=-\angdiv, radius=7] node (n-\b) {};
        \path [curved text=\text] (m-\a) arc [start angle=90-(\a+\caeser-1/2)*\angdiv, delta angle=-\angdiv, radius=5] node (m-\b) {}; % Inner circle
    }
    %   draw arrows
    \draw [arrows={-Latex}]
    (65:9.5) to[bend left=20,edge label=$+3$] (40:9.5);
    \end{tikzpicture}
\end{document}

答案2

以 shift 作为可选参数的版本(默认值为 3)。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\Ceasar[1][3]{%
  \begin{tikzpicture}
    \def\R{2}
    \def\dR{0.5}
    \def\Ri{1.4}
    \def\dRi{0.5}
    \def\Rotate{#1}%% Number of shifts
    \coordinate (origin) at (0,0);
    \draw[fill=gray!60] (origin) circle (1pt);
    \draw (origin) circle (\R);
    \draw (origin) circle (\R+\dR);
    \draw (origin) circle (\Ri);
    \draw (origin) circle (\Ri+\dRi);
    \foreach \Letter [count=\ind from 0,evaluate=\ind as \ang using 90-\ind*360/26] in {A,B,...,Z}{%
      \node[rotate={\ang-90}] at ($(origin)+(\ang:{\R+\dR/2})$) {\Letter};
      \draw({\ang+0.5*360/26}:\R)--({\ang+0.5*360/26}:\R+\dR);
    }
    \foreach \Letter [count=\ind from 0,evaluate=\ind as \ang using 90-\ind*360/26-\Rotate*360/26] in {A,B,...,Z}{%
      \node[rotate={\ang-90}] at ($(origin)+(\ang:{\Ri+\dRi/2})$) {\Letter};
      \draw({\ang+0.5*360/26}:\Ri)--({\ang+0.5*360/26}:\Ri+\dRi);
    }
    \draw[-latex] (90:\Ri-0.3) arc (90:{90-\Rotate*360/26}:\Ri-0.3)node[pos=0.5,anchor=90-\Rotate*180/26]{$\Rotate$};
  \end{tikzpicture}
}
\begin{document}
\Ceasar \Ceasar[10] \Ceasar[-5]
\end{document}

在此处输入图片描述

答案3

您还可以简化\foreach命令:

\documentclass{article}

\usepackage{chancery}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\tikzset{curved text/.style={
        decoration={text effects along path,
            text/.expanded=#1, text align = center,
            text effects/.cd, text along path, decorate}}
}
\begin{document}

    \begin{tikzpicture}[x = 1em, y = 1em]
    \draw circle [radius = 15] circle [radius = 12] circle [radius = 9]
    \foreach \i in {0,...,25}{ (\i*13.85:15) -- (\i*13.85:12) -- (\i*13.85:9) };
    \node [align=center] {Caesar \\ Cipher};
    \foreach [count=\a from 0] \text in {X,Y,Z,A,B,...,W}
    \foreach \t [count=\r] in \text
    \path [curved text=\t, rotate=-\a*13.85]
    (90:15-\r) arc (90:13.85:15-\r);

    \foreach [count=\b from 0] \text in {A,B,...,Z}
    \foreach \t [count=\r] in \text
    \path [curved text=\t, rotate=-\b*13.85]
    (90:12-\r) arc (90:13.85:12-\r);

    \draw[->](55:16) arc(55:13.85:16) node[midway, above, xshift=6pt]{$+3$};
    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容