我需要修改以下代码。我需要绘制图中所示的红色虚线

我需要修改以下代码。我需要绘制图中所示的红色虚线

在此处输入图片描述

这就是我所做的

         \documentclass{article}       
          \usepackage{tikz}     
          \begin{document} 
        \begin{tikzpicture}[scale=3]
  \tikzstyle{every node}=[circle, fill=black!,inner sep=0pt, minimum width=4pt]
    
        
        \node (n_1) at (-1.1,0)[label=left:{\tiny $x^{(3)}_1$}] {};
        \node (n_2) at (1.1,0)[label=right:{\tiny $x^{(3)}_3$}] {};
        \node (n_3) at (-.5,.5)[label=:{\tiny $x^{(2)}_1$}] {};
        \node (n_4) at (-.167, .65)[label=above left:{\tiny $x^{(2)}_3$}] {};
        \node (n_5) at (.167,.65)[label=above right:{\tiny $x^{(2)}_5$}] {};
        \node (n_6) at (.5,.5)[label=above right:{\tiny $x^{(2)}_7$}] {};
        \node (n_7) at (0,1.4)[label=right:{\tiny $x^{(4)}_1$}] {};
        
        \node (n_8) at (-0.15,2.5)[label=above:{\tiny $x^{(7)}_1$}] {};
        \node (n_9) at (-0.41,2.42)[label=above:{\tiny $x^{(5)}_1$}] {};
        \node (n_{10}) at (-0.65,2.25)[label=above:{\tiny $x^{(3)}_1$}] {};
        \node (n_{11}) at (-0.82,2)[label=above:{\tiny $x^{(1)}_1$}] {};
        \node (n_{12}) at (0.15,2.5)[label=above:{\tiny $x^{(9)}_1$}] {};
        \node (n_{13}) at (0.41,2.42)[label=above:{\tiny $x^{(11)}_1$}] {};
        \node (n_{14}) at (0.65,2.25)[label=above:{\tiny $x^{(13)}_1$}] {};
        \node (n_{15}) at (0.82,2)[label=above:{\tiny $x^{(15)}_1$}] {};
    
        
        \foreach \from/\to in
        {n_1/n_2,n_1/n_3,n_1/n_4,n_1/n_5,n_1/n_6,n_1/n_7,n_2/n_3,n_2/n_4,n_2/n_5,n_2/n_6,n_2/n_7,n_3/n_7,n_4/n_7,n_5/n_7,n_6/n_7,n_7/n_8,n_7/n_9,n_7/n_{10},n_7/n_{11},n_7/n_{12},n_7/n_{13},n_7/n_{14},n_7/n_{15}}
        \draw[thick] (\from) -- (\to);  
    \end{tikzpicture}
     \end{document}

答案1

正如@SebGlav 所说,第一条线必须“手动”绘制,其他线则使用 fit 库绘制。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{
    calc,
    fit,
    shapes.geometric,
}
\begin{document}
    \begin{tikzpicture}[scale=3]
    \tikzset{Ahmad/.style={circle, fill=black!,inner sep=0pt, minimum width=4pt}}  
    \tikzset{Shahnawaz/.style={draw=red,inner sep=0pt,outer sep=0pt,thick,ellipse, dashed}}     
    
    \node [Ahmad] (n_1_3) at (-1.1,0)[label=left:{\tiny $x^{(3)}_1$}] {};
    \node [Ahmad] (n_3_3) at (1.1,0)[label=right:{\tiny $x^{(3)}_3$}] {};
    \node [Ahmad] (n_1_2) at (-.5,.5)[label=:{\tiny $x^{(2)}_1$}] {};
    \node [Ahmad] (n_3_2) at (-.167, .65)[label=above left:{\tiny $x^{(2)}_3$}] {};
    \node [Ahmad] (n_5_2) at (.167,.65)[label=above right:{\tiny $x^{(2)}_5$}] {};
    \node [Ahmad] (n_7_2) at (.5,.5)[label=above right:{\tiny $x^{(2)}_7$}] {};
    \node [Ahmad] (n_1_4) at (0,1.4)[label=right:{\tiny $x^{(4)}_1$}] {};
    
    \node [Ahmad] (n_1_7) at (-0.15,2.5)[label=above:{\tiny $x^{(7)}_1$}] {};
    \node [Ahmad] (n_1_5) at (-0.41,2.42)[label=above:{\tiny $x^{(5)}_1$}] {};
    \node [Ahmad] (n_1_3_2) at (-0.65,2.25)[label=above:{\tiny $x^{(3)}_1$}] {};
    \node [Ahmad] (n_1_1) at (-0.82,2)[label=above:{\tiny $x^{(1)}_1$}] {};
    \node [Ahmad] (n_1_9) at (0.15,2.5)[label=above:{\tiny $x^{(9)}_1$}] {};
    \node [Ahmad] (n_1_11) at (0.41,2.42)[label=above:{\tiny $x^{(11)}_1$}] {};
    \node [Ahmad] (n_1_13) at (0.65,2.25)[label=above:{\tiny $x^{(13)}_1$}] {};
    \node [Ahmad] (n_1_15) at (0.82,2)[label=above:{\tiny $x^{(15)}_1$}] {};
    
    
    \foreach \from/\to in
    {n_1_3/n_3_3,n_1_3/n_1_2,n_1_3/n_3_2,n_1_3/n_5_2,n_1_3/n_7_2,n_1_3/n_1_4,n_3_3/n_1_2,n_3_3/n_3_2,n_3_3/n_5_2,n_3_3/n_7_2,n_3_3/n_1_4,n_1_2/n_1_4,n_3_2/n_1_4,n_5_2/n_1_4,n_7_2/n_1_4,n_1_4/n_1_7,n_1_4/n_1_5,n_1_4/n_1_3_2,n_1_4/n_1_1,n_1_4/n_1_9,n_1_4/n_1_11,n_1_4/n_1_13,n_1_4/n_1_15}
    \draw[thick] (\from) -- (\to);  
    
    % \node[Shahnawaz, fit=(n_1_1) (n_1_3_2)(n_1_5)] {};
    \draw [thick, red, dashed] ($(n_1_1)+(3pt, -3pt)$) .. controls (n_1_7) and (n_1_9) .. ($(n_1_15)-(3pt, 3pt)$) arc [start angle=225,end angle=405,radius=6pt] node [anchor=west] {$V_1$} .. controls ($(n_1_9)+(0,16pt)$) and ($(n_1_7)+(0,16pt)$) .. ($(n_1_1)+(-3pt, 6pt)$) to [out=225, in=135] ($(n_1_1)-(3pt, 3pt)$) to [out=315, in=225] cycle;
    \node[Shahnawaz, inner sep=10pt,fit=(n_1_2)(n_3_2)(n_5_2)(n_7_2), label={[red]20:$V_2$}] {};
    \node[Shahnawaz, fit=(n_1_3) (n_3_3), inner sep=10pt, label={[red]0:$V_3$}] {};
    \node[Shahnawaz, inner sep=10pt,fit=(n_1_4), label={[red]0:$V_4$}] {};
    
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

另一个选择是借助calc库在所需节点周围绘制圆弧。为了实现这一点,我稍微移动了节点,使每组的节点位于相同的圆周上并处于已知角度上。

这是我得到的:

\documentclass {article}       
\usepackage    {tikz}
\usetikzlibrary{calc}

\begin{document} 
\begin{tikzpicture}[scale=3.5]
\tikzstyle{every node}=[circle, fill=black, inner sep=0pt, minimum width=4pt]
% dimensions
\def\tr{1.1} % top radius
\def\br{0.7} % bottom radius
\def\gr{0.2} % group radius
% nodes
\node (n_1)      at (-1.1,0)  [label=left:       {\tiny $x^{(3)}_1$}] {};
\node (n_2)      at  (1.1,0)  [label=right:      {\tiny $x^{(3)}_3$}] {};
\node (n_3)      at  (135:\br)[label=above:      {\tiny $x^{(2)}_1$}] {};
\node (n_4)      at  (105:\br)[label=above left: {\tiny $x^{(2)}_3$}] {};
\node (n_5)      at   (75:\br)[label=above right:{\tiny $x^{(2)}_5$}] {};
\node (n_6)      at   (45:\br)[label=above right:{\tiny $x^{(2)}_7$}] {};
\node (n_7)      at    (0,1.4)[label=right:      {\tiny $x^{(4)}_1$}] {};
\begin{scope}[shift={(n_7)}]
  \node (n_8)    at ( 97:\tr) [label=above:      {\tiny $x^{(7)}_1$}] {};
  \node (n_9)    at (111:\tr) [label=above:      {\tiny $x^{(5)}_1$}] {};
  \node (n_{10}) at (125:\tr) [label=above:      {\tiny $x^{(3)}_1$}] {};
  \node (n_{11}) at (139:\tr) [label=above:      {\tiny $x^{(1)}_1$}] {};
  \node (n_{12}) at ( 83:\tr) [label=above:      {\tiny $x^{(9)}_1$}] {};
  \node (n_{13}) at ( 69:\tr) [label=above:      {\tiny $x^{(11)}_1$}]{};
  \node (n_{14}) at ( 55:\tr) [label=above:      {\tiny $x^{(13)}_1$}]{};
  \node (n_{15}) at ( 41:\tr) [label=above:      {\tiny $x^{(15)}_1$}]{};
\end{scope}
% edges
\foreach \from/\to in {n_1/n_2,n_1/n_3,n_1/n_4,n_1/n_5,n_1/n_6,n_1/n_7,n_2/n_3,%
                       n_2/n_4,n_2/n_5,n_2/n_6,n_2/n_7,n_3/n_7,n_4/n_7,n_5/n_7,%
                       n_6/n_7,n_7/n_8,n_7/n_9,n_7/n_{10},n_7/n_{11},n_7/n_{12},%
                       n_7/n_{13},n_7/n_{14},n_7/n_{15}}
  \draw[thick] (\from) -- (\to);
% groups
\draw[red,dashed,thick] ($(n_{11})+(139:\gr)$) arc (139:319:\gr) arc
                        (139:41:\tr-\gr)       arc (221:401:\gr) arc (41:139:\tr+\gr);
\draw[red,dashed,thick] (n_7) circle (\gr);
\draw[red,dashed,thick] ($(n_3)+(135:\gr)$)    arc (135:315:\gr) arc (135:45:\br-\gr)
                                               arc (225:405:\gr) arc (45:135:\br+\gr);
\draw[red,dashed,thick] ($(n_1)+(0,\gr)$)      arc (90:270:\gr) -- ($(n_2)+(0,-\gr)$)
                                               arc (-90:90:\gr) -- cycle;
% group labels
\node[red,fill=none] at ($(n_{15})+(0.3,0)$) [right] {$V_1$};
\node[red,fill=none] at ($(n_6)+(0.3,0)$)    [right] {$V_2$};
\node[red,fill=none] at ($(n_2)+(0.3,0)$)    [right] {$V_3$};
\node[red,fill=none] at ($(n_7)+(0.3,0)$)    [right] {$V_4$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑1:我改变了一些距离(半径,现在已参数化)和比例,以避免一些标签与边缘重叠。

编辑2:放置节点、标签和线条的另一种方法。也更具可定制性。

\documentclass[border=2mm]{standalone}       
\usepackage    {tikz}
\usetikzlibrary{calc}

\begin{document} 
\begin{tikzpicture}[scale=1.5]
% dimensions
\def\tr{3}    % top group radius
\def\br{2}    % bottom group radius
\def\gr{0.6}  % grouping radius
\def\ch{4.5}  % v4 height
\def\sd{3}    % v3 nodes semidistance
\def\nd{0.05} % nodes radius
\def\ld{0.3}  % label distance
% styles
\tikzset%
{%
   myline/.style={shorten <= 2*\nd cm, shorten >= 2*\nd cm},
  mygroup/.style={red,dashed,thick,fill=red,fill opacity=0.025}
}
% coordinates, labels and lines
\coordinate (v3x1) at (-\sd,0);
\coordinate (v3x3) at (\sd,0);
\coordinate (v4x1) at (0,\ch);
\fill (v3x1) circle (\nd);
\fill (v3x3) circle (\nd);
\fill (v4x1) circle (\nd);
\node at ($(v3x1)-(\ld,0)$) {$x_1^{(3)}$};
\node at ($(v3x3)+(\ld,0)$) {$x_3^{(3)}$};
\node at ($(v4x1)+(\ld,0)$) {$x_1^{(4)}$};
\draw[myline] (v3x1) -- (v3x3);
\foreach\i in {1,3,...,15}
{% v1
  \pgfmathsetmacro\j{146-7*\i} % angle
  \coordinate (v1x\i) at ($(v4x1)+(\j:\tr)$);
  \node at ($(v4x1)+(\j:\tr+\ld)$) {$x_1^{(\i)}$};
  \fill (v1x\i) circle (\nd);
  \draw[myline] (v1x\i) -- (v4x1);
}
\foreach\i in {1,3,5,7}
{% v2
  \pgfmathsetmacro\j{150-15*\i} % angle
  \coordinate (v2x\i) at (\j:\br);
  \ifnum\i < 5
    \node at ($(v2x\i)-(\ld,0)$) {$x_\i^{(2)}$};
  \else
    \node at ($(v2x\i)+(\ld,0)$) {$x_\i^{(2)}$};
  \fi
  \fill (v2x\i) circle (\nd);
  \draw[myline] (v2x\i) -- (v4x1);
  \foreach\l in {1,3}
  {%
    \draw[myline] (v2x\i) -- (v3x\l);
  }
}
% groups
\draw[mygroup] ($(v1x1)+(139:\gr)$) arc (139:319:\gr) arc (139:41:\tr-\gr)
                                    arc (221:401:\gr) arc (41:139:\tr+\gr);
\draw[mygroup] ($(v2x1)+(135:\gr)$) arc (135:315:\gr) arc (135:45:\br-\gr)
                                    arc (225:405:\gr) arc (45:135:\br+\gr);
\draw[mygroup] ($(v3x1)+(0,\gr)$)   arc (90:270:\gr)  --  ($(v3x3)+(0,-\gr)$)
                                    arc (-90:90:\gr)  --  cycle;
\draw[mygroup] (v4x1) circle (\gr);
% groups labels
\node[red] at ($(v1x15)+(-30:\gr)$) [right] {$V_1$};
\node[red] at ($(v2x7) +(45:\gr)$)  [right] {$V_2$};
\node[red] at ($(v3x3) +(0,\gr)$)   [above] {$V_3$};
\node[red] at ($(v4x1) -(\gr,0)$)   [left]  {$V_4$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容