在 TikZ 中绘制 n→n+1, n→a⋅n mod m 的图

在 TikZ 中绘制 n→n+1, n→a⋅n mod m 的图

我有个新手问题。我想用TikZ绘制一个顶点0,..,m-1在圆形内,边为 的图形n → n+1 mod mn → a⋅n mod m其中m=19a=5,比如说。

以下尝试几乎成功了 - 但是,箭头落在了错误的位置(总是在节点周围圆圈的最右边):

\begin{tikzpicture}
\def \R {60}
\def \r {9}
\def \radius {\R mm}
\tikzmath{
  \m = 19; \a = 5;
  \margin = 2*atan(\r/(4*\R));
  \mm = \m - 1;
}
\foreach \n in {0,...,\mm}
{
  \node[draw, circle,minimum size=\r mm] (v_\n) at ({90-360/\m * \n}:\radius) {$\n$};
  \draw[->, >=latex, ] ({90-(360/\m * \n+\margin)}:\radius) 
    arc ({90-(360/\m * \n+\margin)}:{90-(360/\m * (\n+1)-\margin)}:\radius);
}

\begin{scope}[->,>=latex,shorten >=1pt,color=red]
\foreach \n in {1,...,\mm}
{
  \tikzmath{\ntimes = Mod(\n*\a,\m);}      
  \draw (v_\n) edge (v_\ntimes);         
}
\path (v_0) edge [loop above] (v_0);
\end{scope}
\end{tikzpicture}

在此处输入图片描述

(我知道我也应该让短边弯曲。)

下面的代码根本不能编译:

  \begin{tikzpicture}
    \def \R {60}
    \def \r {9}
    \def \radius {\R mm}
    \tikzmath{
      \m = 19; \a = 5;
      \margin = 2*atan(\r/(4*\R));
      \mm = \m - 1;
    }
    \graph[clockwise, radius=6cm] {subgraph C_n [n=\m, name=A]};
    \foreach \n in {1,...,\mm} {
        \tikzmath{\ntimes = Mod(\n*\a,\m); \np = Mod(\n+1,\m);}      
        \draw (A \n) -- (A \ntimes);
        \draw (A \n) -- (A \np);
      }
  \end{tikzpicture}

我收到消息:“!包 pgf 错误:没有已知名为 A 0 的形状。”

各个案件的具体情况是什么?

答案1

Torbjørn T. 也在评论中注意到了这一点(似乎在我之前...),问题是Mod返回 1.0 这样的数字,所以你正在将节点绘制到角度为零的点。我也会放弃环境scope,而是使用

[evaluate=\n as \na using {int(Mod(\n*\a,\m))}]]

(并且\tikzset)生产:

在此处输入图片描述

以下是代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,math,positioning}

\def \R {60}
\def \r {9}
\def \radius {\R mm}
\tikzset{
myedge/.style={->, >=latex, shorten >=1pt, color=red}
}

\newcommand\macircle[2]{% m=#1, a=#2
  \begin{tikzpicture}
    \tikzmath{
      \margin = 2*atan(\r/(4*\R));
      \mm = #1 - 1;
    }
    \foreach \n in {0,...,\mm}
    {
      \node[draw=blue, circle,minimum size=\r mm] (v\n) at ({90-360/#1 * \n}:\radius) {$\n$};
      \draw[->, >=latex, ] ({90-(360/#1 * \n+\margin)}:\radius)
        arc ({90-(360/#1 * \n+\margin)}:{90-(360/#1 * (\n+1)-\margin)}:\radius);
    }

    \foreach \n [evaluate=\n as \na using {int(Mod(\n*#2,#1))}] in {1,...,\mm}
    {
        \draw[myedge] (v\n) -- (v\na);
    }
    \path[myedge] (v0) edge [loop above] (v0);
  \end{tikzpicture}
}

\begin{document}

\macircle{19}{5}
\macircle{17}{6}

\end{document}

我已经创建了你的通用宏并做了一些调整。

答案2

@Andrew 答案的一个小变化:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}[
every edge/.style = {draw=red,-{Straight Barb[angle=60:3pt 3]}, semithick},
every loop/.style = {draw=red,-{Straight Barb[angle=60:3pt 3]}, semithick}
                        ] 

\foreach \i in {0,...,18}
{
  \node (v\i) [circle, draw, minimum size=9mm] at ({90-\i*360/19}:6) {\i};
  \pgfmathsetmacro{\margin}{atan(9/120)}
  \path[every edge]
    ({90+\i*360/19+\margin}:6) arc ({90+\i*360/19+\margin}:{90+(\i+1)*360/19-\margin}:6);
}
\foreach \n in {1,...,18}
{
\pgfmathsetmacro{\nn}{int(Mod(\n*5,19))}
\pgfmathsetmacro{\j}{int(abs(\nn-\n))}
    \ifnum\j<3
        \pgfmathsign{\nn-\n}
        \ifnum\pgfmathresult>0
            \path (v\n) edge[bend right] (v\nn);
        \else
            \path (v\n) edge[bend  left] (v\nn);
        \fi
    \else
        \path (v\n)  edge[blue] (v\nn);
    \fi
}
\path (v0)  edge [loop above] ();
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容