使用循环创建图形组合

使用循环创建图形组合

左侧的蓝色图表中有 5 个连接。从此图表中,可以使用例如 3 个连接创建子图。总共有 10 种组合。

有没有一种聪明的方法来创建和绘制这些组合?可能是通过使用\foreach

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}




\begin{document}

                \begin{tikzpicture}[>=stealth',auto,node distance=2cm,
                  thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]
                  
                  
                  
                    \node[main node,blue] at (0, 0)   (1) {1};
                    \node[main node,blue] at (1, 1)  (2) {2};
                    \node[main node,blue] at (1, -1)  (3) {3};
                    \node[main node,blue] at (2, 0)  (4) {4};
                
                    \draw [blue] (1) -- (2);
                    \draw [blue] (1) -- (3);
                    \draw [blue] (2) -- (4);
                    \draw [blue] (2) -- (3);
                    \draw [blue] (3) -- (4);

                    \node[main node] at (3, 0)   (1) {1};
                    \node[main node] at (4, 1)  (2) {2};
                    \node[main node] at (4, -1)   (3) {3};
                    \node[main node] at (5, 0)   (4) {4};
                

                    \foreach \k in {(1) -- (2),(2) -- (3),(2) -- (4)} {
                      \draw \k ;
                    }
                    
                    \node[main node] at (6, 0)   (1) {1};
                    \node[main node] at (7, 1)  (2) {2};
                    \node[main node] at (7, -1)   (3) {3};
                    \node[main node] at (8, 0)   (4) {4};

                    \foreach \k in {(1) -- (3),(2) -- (3),(3) -- (4)} {
                      \draw \k ;
                    }
                \end{tikzpicture}
                
                

\end{document}

在此处输入图片描述

编辑

在收到@Excelsior 的非常有帮助的评论后,问题的代码如下:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}

                % \begin{tikzpicture}[>=stealth',auto,node distance=2cm,
                %   thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]
                  
                  
                  
                %     \node[main node,blue] at (0, 0)   (1) {1};
                %     \node[main node,blue] at (1, 1)  (2) {2};
                %     \node[main node,blue] at (1, -1)  (3) {3};
                %     \node[main node,blue] at (2, 0)  (4) {4};
                
                %     \draw [blue] (1) -- (2);
                %     \draw [blue] (1) -- (3);
                %     \draw [blue] (2) -- (4);
                %     \draw [blue] (2) -- (3);
                %     \draw [blue] (3) -- (4);

                %     \node[main node] at (3, 0)   (1) {1};
                %     \node[main node] at (4, 1)  (2) {2};
                %     \node[main node] at (4, -1)   (3) {3};
                %     \node[main node] at (5, 0)   (4) {4};
                

                %     \foreach \k in {(1) -- (2),(2) -- (3),(2) -- (4)} {
                %       \draw \k ;
                %     }
                    
                %     \node[main node] at (6, 0)   (1) {1};
                %     \node[main node] at (7, 1)  (2) {2};
                %     \node[main node] at (7, -1)   (3) {3};
                %     \node[main node] at (8, 0)   (4) {4};

                %     \foreach \k in {(1) -- (3),(2) -- (3),(3) -- (4)} {
                %       \draw \k ;
                %     }

                % \end{tikzpicture}
                
                \begin{tikzpicture}[
        >=stealth', auto, node distance=2cm, thick, 
        main node/.style={circle, draw, font=\sffamily\Large\bfseries}, dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]     
        
        \node[main node,blue] at (0, 0) (1-0) {1};
        \node[main node,blue] at (1, 1) (2-0) {2};
        \node[main node,blue] at (1,-1) (3-0) {3};
        \node[main node,blue] at (2, 0) (4-0) {4};
        
        \draw [blue] (1-0) -- (2-0);
        \draw [blue] (1-0) -- (3-0);
        \draw [blue] (2-0) -- (4-0);
        \draw [blue] (2-0) -- (3-0);
        \draw [blue] (3-0) -- (4-0);
        
        \foreach [evaluate=\i as \x using {3*(\i)}] \i in {1,...,11} {
            \foreach \n in {1,...,4}{
                \node[main node] at ($ (\n-0) + (\x,0) $) (\n-\i) {\n};
            }
        }
        
        % node system 1

        \foreach \k in {(1-1) -- (2-1),
                        (2-1) -- (3-1),
                        (2-1) -- (4-1)} {
            \draw \k ;
        }
        
        % node system 2
        \draw (1-2) -- (2-2);
        \draw (2-2) -- (3-2);
        \draw (3-2) -- (4-2);
    \end{tikzpicture}

\end{document}

结果如下:

在此处输入图片描述

答案1

我尝试绘制 10 个额外的节点系统,并根据其编号进行标记。您可能最了解可能的组合,因此我将它们留空。

在此处输入图片描述

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}
    
    \begin{tikzpicture}[
        >=stealth', auto, node distance=2cm, thick, 
        main node/.style={circle, draw, font=\sffamily\Large\bfseries}, dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]     
        
        \node[main node,blue] at (0, 0) (1-0) {1};
        \node[main node,blue] at (1, 1) (2-0) {2};
        \node[main node,blue] at (1,-1) (3-0) {3};
        \node[main node,blue] at (2, 0) (4-0) {4};
        
        \draw [blue] (1-0) -- (2-0);
        \draw [blue] (1-0) -- (3-0);
        \draw [blue] (2-0) -- (4-0);
        \draw [blue] (2-0) -- (3-0);
        \draw [blue] (3-0) -- (4-0);
        
        \foreach [evaluate=\i as \x using {3*(\i)}] \i in {1,...,11} {
            \foreach \n in {1,...,4}{
                \node[main node] at ($ (\n-0) + (\x,0) $) (\n-\i) {\n};
            }
        }
        
        % node system 1
        \draw (1-1) -- (2-1);
        \draw (2-1) -- (3-1);
        \draw (2-1) -- (4-1);
        
        % node system 2
        \draw (1-2) -- (2-2);
        \draw (2-2) -- (3-2);
        \draw (3-2) -- (4-2);
    \end{tikzpicture}
\end{document}

如果需要更多节点系统,只需增加最大值\i

编辑

添加了可能的组合:

节点 v2

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}
    
    \begin{tikzpicture}[
        >=stealth', auto, node distance=2cm, thick, 
        main node/.style={circle, draw, font=\sffamily\Large\bfseries}, dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]     
        
        \node[main node,blue] at (0, 0) (1-0) {1};
        \node[main node,blue] at (1, 1) (2-0) {2};
        \node[main node,blue] at (1,-1) (3-0) {3};
        \node[main node,blue] at (2, 0) (4-0) {4};
        
        \draw [blue] (1-0) -- (2-0);
        \draw [blue] (1-0) -- (3-0);
        \draw [blue] (2-0) -- (4-0);
        \draw [blue] (2-0) -- (3-0);
        \draw [blue] (3-0) -- (4-0);
        
        \foreach [evaluate=\i as \x using {3*(\i)}] \i in {1,...,10} {
            \foreach \n in {1,...,4}{
                \node[main node] at ($ (\n-0) + (\x,0) $) (\n-\i) {\n};
            }
        }
        
        % node system 1
        \draw (1-1) -- (2-1);
        \draw (2-1) -- (4-1);
        \draw (2-1) -- (3-1);
        
        % node system 2
        \draw (1-2) -- (2-2);
        \draw (2-2) -- (4-2);
        \draw (1-2) -- (3-2);
        
        % node system 3
        \draw (1-3) -- (2-3);
        \draw (2-3) -- (4-3);
        \draw (3-3) -- (4-3);
        
        % node system 4
        \draw (1-4) -- (2-4);
        \draw (2-4) -- (3-4);
        \draw (1-4) -- (3-4);
        
        % node system 5
        \draw (1-5) -- (2-5);
        \draw (2-5) -- (3-5);
        \draw (3-5) -- (4-5);
        
        % node system 6
        \draw (1-6) -- (2-6);
        \draw (1-6) -- (3-6);
        \draw (3-6) -- (4-6);
        
        % node system 7
        \draw (1-7) -- (3-7);
        \draw (2-7) -- (3-7);
        \draw (3-7) -- (4-7);
        
        % node system 8
        \draw (2-8) -- (4-8);
        \draw (2-8) -- (3-8);
        \draw (3-8) -- (4-8);
        
        % node system 9
        \draw (2-9) -- (4-9);
        \draw (2-9) -- (3-9);
        \draw (1-9) -- (3-9);
        
        % node system 10
        \draw (2-10) -- (4-10);
        \draw (3-10) -- (4-10);
        \draw (1-10) -- (3-10);
    \end{tikzpicture}
\end{document}

相关内容