帮助在 tkz-graph 包中绘制图形

帮助在 tkz-graph 包中绘制图形

我想在 tkz-graph 包中绘制以下图形。这些分别是单位距离彩色 Golomb 图和 Moser 纺锤图。

在此处输入图片描述

在此处输入图片描述

当我尝试使用正则多边形操作时,它会使边缘错位,并且第二个图形不规则,所以我不知道如何绘制它

答案1

对于第一个,你可以堆叠两个\Vertices命令。但如果所有边的长度相同,则需要进行一些计算:

\documentclass[border=10pt]{standalone} 
\usepackage{tkz-graph}

\begin{document}
\begin{tikzpicture}[rotate=90]
    \pgfmathsetmacro{\u}{3}
    \Vertices[unit=3]{circle}{A,B,C,D,E,F}

    \pgfmathsetmacro{\v}{\u*sqrt(3)/3}
    \pgfmathsetmacro{\r}{acos((\u*\u-\u*\u-\v*\v)/(-2*\v*\u))-120}    
    \begin{scope}[rotate={\r}]
        \Vertices[unit={\v}]{circle}{G,H,I}
    \end{scope}
    \Vertex{K}

    \AddVertexColor{red}{B,E,I} 
    \AddVertexColor{blue}{A,D,G}
    \AddVertexColor{white}{C,F,H}
    \AddVertexColor{green}{K}
    
    \Edges(A,B,C,D,E,F,A)
    \Edges(G,H,I,G)
    \Edges(A,H)
    \Edges(C,I)
    \Edges(E,G)
    \Edges(A,K,D)
    \Edges(B,K,E)
    \Edges(C,K,F)
\end{tikzpicture}
\end{document}

在此处输入图片描述


对于第二个,也需要进行一些计算:

\documentclass[border=10pt]{standalone} 
\usepackage{tkz-graph}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro{\u}{2}
    \pgfmathsetmacro{\r}{acos((\u*sin(60))/(6*\u*sin(30)))}

    \begin{scope}[rotate={\r-60}]
        \Vertices[unit=\u]{circle}{A,B,C}
    \end{scope}
    \coordinate (X) at ($(A)!(C)!(B)$);
    \coordinate (D) at ($(C)!2!(X)$);
    \Vertex[Node]{D}

    \begin{scope}[xshift={2*\u*cos(\r)*2cm}, rotate={120-\r}]
        \Vertices[unit=\u]{circle}{E,F,G}
    \end{scope}

    \AddVertexColor{red}{B,G} 
    \AddVertexColor{blue}{F} 
    \AddVertexColor{green}{A,E} 
    \AddVertexColor{yellow}{C,D} 

    \Edges(A,B,C,A)
    \Edges(E,F,G,E)
    \Edges(A,D,B)
    \Edges(E,D,F)
    \Edges(C,G)
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用库的替代解决方案graphs

对于第一个图,除绿色节点外,所有节点都以圆形排列(由键指定counterclockwise)。计算内部节点集的半径,使它们之间的边与外部节点集的半径一样长(这也是它们之间的距离)。内部节点集的相位是根据右下象限中中间绿色节点、内部蓝色节点和外部红色节点之间的等腰三角形计算得出的。结果是 43.2213451°(尽管 PGFMath 只将其计算为 32.2222°。)

对于第二张图,graphs库的使用没有任何循环放置规则。
事实上,所有节点都是手动放置的。该函数a指定边的长度,所有放置都依赖于它们。

首先放置的节点是底部的两个节点,其他所有节点都经过计算。这基本上又是三角函数,因为您总是有等腰三角形,其他角度可以计算出来。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{calc, graphs.standard}
\tikzset{color or style/.code 2 args={\tikzset{#1 #2/.try}\ifpgfkeyssuccess\else\tikzset{#1=#2}\fi}}
\tikzset{
  my graphs/.style={
    thick,
    fill  blue/.style={fill={rgb,255:red, 0;green,129;blue,255}},
    fill green/.style={fill={rgb,255:red,26;green,193;blue, 26}},
    circ/.style={shape=circle, draw},
    graphs/every graph/.append style={
      nodes={circ, color or style={fill}{\tikzgraphnodetext}},
      fresh nodes, empty nodes}}}
\begin{document}
\tikz[my graphs]\graph {
  subgraph C_n[
    name=outer,
    V={blue, red, white, blue, red, white},
    radius=1.5cm,
    counterclockwise
  ] -- green,
} graph {
  subgraph C_n[
    name=inner, V={blue, white, red},
    phase=asin(.5*sqrt(11/3))-30, radius=1.5cm/sqrt 3, counterclockwise
  ],
  (outer blue) -- (inner white),
  (outer white) -- (inner red),
  (outer red') -- (inner blue)
};

\tikz[my graphs, declare function={a=2;}]
  \graph[no placement]{
    yellow[x=-a/2] -- red[x=a/2],
    yellow[y=sqrt 11*a/2],
    red  [at={($(yellow)+({ 30+acos(.5/sqrt 3)}:a)$)}],
    green[at={($(yellow)+({-30+acos(.5/sqrt 3)}:a)$)}],
    blue [at={($(red)+({180+30-acos(.5/sqrt 3)}:a)$)}],
    green[at={($(red)+({180-30-acos(.5/sqrt 3)}:a)$)}],
    % alternative for the four previous lines above:
%    \foreach[count=\b from 0] \st/\l in {yellow/{red, green}, red/{blue, green}} {
%      \foreach{{[count=\a from 0] \L}}in \l {
%        \L[at={($(\st)+({180*\b+30*(\a?-1:1)+(\b?-1:1)*acos(.5/sqrt 3)}:a)$)}]}},
       (yellow) -- (red') -- (yellow') -- (green') -- (blue)
    -- (yellow') -- (green) -- {(red'), (yellow)},
    (red) -- {(green'), (blue)}
  };
\end{document}

输出

在此处输入图片描述 在此处输入图片描述

相关内容