自动范围检测和红框绘制

自动范围检测和红框绘制

在以下 tikz 代码中,有三组图形,每组都包含在自己的范围环境中。我希望用矩形框将中间的图形包围起来。虽然我可以使用绝对坐标来实现这一点,但有没有更好的方法来识别范围的范围?

\documentclass[tikz]{standalone}

\begin{document}

\tikzset{unode/.style = {
    circle, 
    draw=blue, 
    thick,
    fill=white,
    inner sep=2.3pt,
    minimum size=2.3pt } }
\tikzset{uedge/.style = {
    draw=cyan!20!black, 
    very thick} }

\begin{tikzpicture} [scale=0.5]
    %first graph
\begin{scope} 
        \foreach \x [count=\i] in {18,90,162,234,306}{
            \coordinate (i\i) at (\x:1cm) {};
             \coordinate (o\i) at (\x:1.8cm) {};
             \path[uedge] (\x:1cm)--(\x:1.8cm);
        }
        \path[uedge] (o1) -- (o2) -- (o3) -- (o4)-- (o5)--(o1);
        \path[uedge] (i1) -- (i3) -- (i5) -- (i2)-- (i4)--(i1);
        \foreach \x [count=\i] in {18,90,162,234,306}{
             \node[unode] at (\x:1cm){};
             \node[unode] at (\x:1.8cm){};
        }
    \end{scope}
%second graph
    \begin{scope}[xshift=5cm]
        \foreach \x [count=\i] in {0,60,120,180,240,300}{
            \coordinate (o\i) at (\x:1.8cm) {};
        }
        \foreach \x [count=\i] in {30,150,270}{
            \coordinate (i\i) at (\x:1cm) {};
        }
        \coordinate (a) at (0,0) {};
        
        \path[uedge] (o1) -- (o2) -- (o3) -- (o4)-- (o5)--(o6)--(o1);
        \path[uedge] (a)--(i1) ;
        \path[uedge] (a)--(i2) ;
        \path[uedge] (a)--(i3) ; 
        \path[uedge] (o6)--(i1)--(o3) ;
        \path[uedge] (o2)--(i2)--(o5) ;
        \path[uedge] (o4)--(i3)--(o1) ;
        \foreach \x [count=\i] in {0,60,120,180,240,300}{
            \node[unode] at (\x:1.8cm){};
        }
        \foreach \x [count=\i] in {30,150,270}{
            \node[unode] at (\x:1cm){};
        }
        \node[unode] at (0,0){};
    \end{scope}
    \draw[red, thick] (2.5,-2) rectangle (7.5,2);
%third graph
    \begin{scope}[xshift=10cm]
        \foreach \x [count=\i] in {10,50,90,...,370}{
            \coordinate (o\i) at (\x:1.8cm) {};
        }
        
        \coordinate (a) at (0,0) {};
        \path[uedge] (o1) -- (o2) -- (o3) -- (o4)-- (o5)--(o6)--(o7)--(o8)--(o9)--(o1);
        \path[uedge] (o1)--(o5) ;
        \path[uedge] (o2)--(o7) ;
        \path[uedge] (o4)--(o8) ;
        \path[uedge] (a)--(o3) ;
        \path[uedge] (a)--(o6) ;
        \path[uedge] (a)--(o9) ;
        \foreach \x [count=\i] in {10,50,90,...,370}{
            \node[unode] at (\x:1.8cm){};
        }
        \node[unode] at (0,0){};
    \end{scope}

   
\end{tikzpicture}
\end{document} 


在此处输入图片描述

答案1

您可以使用local bounding box=<name>然后在框周围绘制,或者使用fitting库在其周围放置一个节点。在这里,我只是2.5pt手动添加填充。

\documentclass[tikz]{standalone}

\begin{document}

\tikzset{unode/.style = {
    circle, 
    draw=blue, 
    thick,
    fill=white,
    inner sep=2.3pt,
    minimum size=2.3pt } }
\tikzset{uedge/.style = {
    draw=cyan!20!black, 
    very thick} }

\begin{tikzpicture} [scale=0.5]
    %first graph
\begin{scope} 
        \foreach \x [count=\i] in {18,90,162,234,306}{
            \coordinate (i\i) at (\x:1cm) {};
             \coordinate (o\i) at (\x:1.8cm) {};
             \path[uedge] (\x:1cm)--(\x:1.8cm);
        }
        \path[uedge] (o1) -- (o2) -- (o3) -- (o4)-- (o5)--(o1);
        \path[uedge] (i1) -- (i3) -- (i5) -- (i2)-- (i4)--(i1);
        \foreach \x [count=\i] in {18,90,162,234,306}{
             \node[unode] at (\x:1cm){};
             \node[unode] at (\x:1.8cm){};
        }
    \end{scope}
%second graph
    \begin{scope}[xshift=5cm,local bounding box=second graph]
        \foreach \x [count=\i] in {0,60,120,180,240,300}{
            \coordinate (o\i) at (\x:1.8cm) {};
        }
        \foreach \x [count=\i] in {30,150,270}{
            \coordinate (i\i) at (\x:1cm) {};
        }
        \coordinate (a) at (0,0) {};
        
        \path[uedge] (o1) -- (o2) -- (o3) -- (o4)-- (o5)--(o6)--(o1);
        \path[uedge] (a)--(i1) ;
        \path[uedge] (a)--(i2) ;
        \path[uedge] (a)--(i3) ; 
        \path[uedge] (o6)--(i1)--(o3) ;
        \path[uedge] (o2)--(i2)--(o5) ;
        \path[uedge] (o4)--(i3)--(o1) ;
        \foreach \x [count=\i] in {0,60,120,180,240,300}{
            \node[unode] at (\x:1.8cm){};
        }
        \foreach \x [count=\i] in {30,150,270}{
            \node[unode] at (\x:1cm){};
        }
        \node[unode] at (0,0){};
    \end{scope}
    \draw[red, thick] ([xshift=-2.5pt,yshift=2.5pt]second graph.north west) -| ([xshift=2.5pt,yshift=-2.5pt]second graph.south east) -| cycle;
%third graph
    \begin{scope}[xshift=10cm]
        \foreach \x [count=\i] in {10,50,90,...,370}{
            \coordinate (o\i) at (\x:1.8cm) {};
        }
        
        \coordinate (a) at (0,0) {};
        \path[uedge] (o1) -- (o2) -- (o3) -- (o4)-- (o5)--(o6)--(o7)--(o8)--(o9)--(o1);
        \path[uedge] (o1)--(o5) ;
        \path[uedge] (o2)--(o7) ;
        \path[uedge] (o4)--(o8) ;
        \path[uedge] (a)--(o3) ;
        \path[uedge] (a)--(o6) ;
        \path[uedge] (a)--(o9) ;
        \foreach \x [count=\i] in {10,50,90,...,370}{
            \node[unode] at (\x:1.8cm){};
        }
        \node[unode] at (0,0){};
    \end{scope}

   
\end{tikzpicture}
\end{document} 

绘制局部边界框

相关内容