如何绘制两个循环

如何绘制两个循环

我如何使用 tikz 绘制以下图形?两个循环相连

答案1

嗯,画图对我来说很适合玩代码高尔夫

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\def\myarray{{1,2,5,6,3,4}}
\begin{document}
\begin{tikzpicture}[sty/.style={regular polygon,regular polygon sides=6,minimum size=4cm,#1}]
\foreach\x/\xo/\xs in{1/xscale=-1/0,2/rotate=60/6}{
\path node[sty/.expanded=\xo]at(\xs,0)(h\x){} node[circle,draw]at (h\x.corner 1)(h\x-1){1};}
\foreach \x[evaluate={\xa=\myarray[\x-1]}, remember=\x as \xp (initially 1)] in {2,...,6,1}{
  \foreach \y/\ys in {1/right,2/left}{
    \draw node[circle, draw] (h\y-\x) at (h\y.corner \x) {\xa} (h\y-\xp) -- (h\y-\x) 
                            node[midway,auto=\ys,swap] {\ifnum\xa=5 $\lambda$\else1\fi};}}
\draw (h1-3) -- (h2-2) node[midway,above]{1};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

在某种程度上比@percusse 的更容易(在水平上)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcounter{tempc}
\begin{document}

\def\rpol{2 cm}
\def\rcirc{1cm}
\begin{tikzpicture}
\foreach \angle/\num[count=\i from 1] in {0/5,60/2,120/1,180/4,240/3,300/6,360/0}
{
\ifnum\i<7
\path ($(0,0)+(\angle:\rpol)$) node[draw,circle,minimum width=\rcirc ] (A\i) {\num};
\fi
\setcounter{tempc}{\i}
\addtocounter{tempc}{-1}
\ifnum\i=1
\relax
\else 
\ifnum\i=7
\draw(A6)--(A1)node[midway,shift=({\angle-15}:0.3cm)]{ 1};
\else
\draw (A\i)--(A\thetempc)node[midway,shift=({\angle-15}:0.3cm)]{\ifnum\i=2 $\lambda$\else 1\fi};
\fi
\fi
}
\foreach \angle/\num[count=\i from 1] in {180/2,240/5,300/6,360/3,60/4,120/1,180/0}
{
\ifnum\i<7
\path ($({3*\rpol},0)+(\angle:\rpol)$) node[draw,circle,minimum width=\rcirc ] (B\i) {\num};
\fi
\setcounter{tempc}{\i}
\addtocounter{tempc}{-1}
\ifnum\i=1
\relax
\else 
\ifnum\i=7
\draw(B6)--(B1)node[midway,shift=({\angle-15}:0.3cm)]{ 1};
\else
\draw (B\i)--(B\thetempc)node[midway,shift=({\angle-15}:0.3cm)]{\ifnum\i=2 $\lambda$\else 1\fi};
\fi
\fi
}
\draw(A1)--(B1) node[midway,yshift=0.3cm] {1};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

打高尔夫球?使用单个参数循环:

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{shapes.geometric,math}
\tikzset{
  hexleft/.style = {xscale=-1},
  hexright/.style = {rotate=60},
  hex/.pic={
    \node[hex#1,draw,fill=white,regular polygon,regular polygon sides=6,minimum size=4cm](-h){};
    \tikzmath{
      let \k = $\lambda$;
      int \i,\l,\n,\m; \n=2;
      for \i in{0 ,...,5}{
        \l=mod(\i+1,6)+1; \m = mod(\n,6) + 1;
        {\path (-h.corner \n) node[draw,circle,fill=white] {\l} -- (-h.corner \m) node[midway,auto=#1]{\k};};
        \n = mod(\n+mod(\i+1,2)*2,6)+1;\k=1;
      };
    };
  }
}
\begin{document}
  \begin{tikzpicture}
     \draw (0,0) pic(A){hex={left}} -- node[above]{1} ++(7,0) pic(B){hex={right}};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容