理解 tikz 中的完整图形示例

理解 tikz 中的完整图形示例

我是 Tikz 的新手,正在尝试理解(和修改)来自 texample.net 的这个 16 节点完整图示例

% A complete graph
% Author: Quintin Jean-Noël
% <http://moais.imag.fr/membres/jean-noel.quintin/>
\documentclass{article}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage{verbatim} 
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\begin{comment}
:Title: A complete graph
:Tags: Foreach;Graphs;To paths
:Author: Jean-Noël Quintin
:Slug: complete-graph
\end{comment}
\usetikzlibrary[topaths]
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{document}
\begin{tikzpicture}[transform shape]
  %the multiplication with floats is not possible. Thus I split the loop in two.
  \foreach \number in {1,...,8}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 45
        \advance\mycount by 0
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {9,...,16}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 45
        \advance\mycount by 22.5
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {1,...,15}{
        \mycount=\number
        \advance\mycount by 1
  \foreach \numbera in {\the\mycount,...,16}{
    \path (N-\number) edge[->,bend right=3] (N-\numbera)  edge[<-,bend
      left=3] (N-\numbera);
  }
}
\end{tikzpicture}
\end{document}

代码生成以下图片 在此处输入图片描述

我正在尝试了解图片中节点和边缘位置的计算方式,以便我可以对其进行修改以减少或增加节点。我尝试将角度加倍以将节点数量减少一半,但这却让我得到了一张非常丑陋的图片。

编辑:添加了我所更改的内容。也许我之前做错了什么,但结果图片并没有那么丑

% A complete graph
% Author: Quintin Jean-Noël
% <http://moais.imag.fr/membres/jean-noel.quintin/>
\documentclass{article}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\begin{comment}
:Title: A complete graph
:Tags: Foreach;Graphs;To paths
:Author: Jean-Noël Quintin
:Slug: complete-graph
\end{comment}
\usetikzlibrary[topaths]
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{document}
\begin{tikzpicture}[transform shape]
  %the multiplication with floats is not possible. Thus I split the loop in two.
  \foreach \number in {1,...,4}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 90
        \advance\mycount by 0
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {5,...,8}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 90
        \advance\mycount by 45
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {1,...,7}{
        \mycount=\number
        \advance\mycount by 1
  \foreach \numbera in {\the\mycount,...,8}{
    \path (N-\number) edge[->,bend right=3] (N-\numbera)  edge[<-,bend
      left=3] (N-\numbera);
  }
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

我不能说我理解了规则,但我想从头开始画会更简单

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\def\n{16}% how many nodes
\node[circle,minimum size=10 cm] (b) {};
\foreach\x in{1,...,\n}{
  \node[minimum size=0.75cm,draw,circle] (n-\x) at (b.{360/\n*\x}){\x};
}
\foreach\x in{1,...,\n}{
  \foreach\y in{1,...,\n}{
    \ifnum\x=\y\relax\else
      \draw (n-\x) edge[->,bend right=3] (n-\y);
    \fi
  }
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容