在 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 m, n → a⋅n mod m, 其中m=19, a=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}

在此輸入影像描述

相關內容