如何在多边形网格周围添加框架?

如何在多边形网格周围添加框架?

我想基于给出的代码这个帖子,并在两个分组周围添加框架以及组号,大致如下:

分组周围的框架

此外,我理想情况下不需要像上述帖子中那样指定第二个分组的。相反,如果两个分组自然地一个接一个地出现,就像字母自然地一个接一个地呈现一样,而不必每次yshift都明确指定,那就太好了。xshift

如果基于上述帖子构建解决方案太复杂,那么第二好的选择是基于构建解决方案这个帖子,其中多边形明确地排列成矩阵,而不是隐式地自动排列。

答案1

假设您想将所有多边形保持在相同的位置tikzpicture,并且您接受标记每个组by hand并仍然接受每个组的手册yshift

在这些情况下,一个可能的解决方案是为每个 添加一个名称pic,为一些内部坐标 添加一个名称,并为每组多边形 添加一个名称。有了所有这些,就可以使用fit库轻松地在选定 周围绘制框架pics

\documentclass[a4paper]{article}
\usepackage{geometry}

\usepackage{tikz}
\usetikzlibrary{fit, positioning}

\newcommand{\grid}{\foreach \i in {0,1,2} \foreach \j in {0,1,2}
            \fill (\i,\j) coordinate (-\i-\j) circle (1pt);}
            
\tikzset{
    line join=round,
    polygon/.style={thick, blue},
    empty/.pic={
        \grid
    },
   one/.pic={
        \grid
        \draw[polygon] (0,0)--(2,2)|-cycle;
    },
   two/.pic={
        \grid
        \draw[polygon] (0,0)-|(2,1)--(1,2)--(1,1)--cycle;
    },
   three/.pic={
        \grid
        \draw[polygon] (0,0)-|(2,1)--(0,2)--(1,1)--cycle;
    },
   four/.pic={
        \grid
        \draw[polygon] (0,0)-|(2,1)-|(1,2)--cycle;
    },
   five/.pic={
        \grid
        \draw[polygon] (0,0) rectangle ++(2,1);
    },
   six/.pic={
        \grid
        \draw[polygon] (0,0)--(2,0)--(1,2)--(0,2)--(1,1)--cycle;
    },
   seven/.pic={
        \grid
        \draw[polygon] (0,0)--(2,0)--(1,2)--(0,1)--(1,1)--cycle;
    },
   eight/.pic={
        \grid
        \draw[polygon] (0,0)--(2,0)--(1,2)--cycle;
    },
   nine/.pic={
        \grid
        \draw[polygon] (0,0)--(1,0)--(2,1)|-(1,2)--(1,1)--cycle;
    },
   ten/.pic={
        \grid
        \draw[polygon] (0,0)--(1,0)--(2,1)--(2,2)--(1,1)--(1,2)--cycle;
    },
}

\newcommand{\placepics}[3][7]{
    \foreach \i [count=\ni from 0, 
            evaluate=\ni as \col using {int(mod(\ni,#1))},
            evaluate=\ni as \row using {int(\ni/#1)}] in {#3}{
    \pic (#2-\i) at (3*\col,-3*\row) {\i};}
}

\begin{document}

\begin{tikzpicture}[x=.75cm,y=.75cm]
\placepics{A}{one, two, three, four, five, six, seven, eight, nine, ten, two, empty, four, six, nine}
\node[fit=(A-one-0-2) (A-seven-2-0|-A-nine-0-0), rounded corners, label=left:I, draw=red] {};
\begin{scope}[yshift=-9*.75cm]
\placepics[5]{B}{one, two, three, four, five, six, seven, eight, nine, ten, two, empty, four, six, nine}
\end{scope}
\node[fit=(B-one-0-2) (B-nine-2-0), rounded corners, label=left:II, draw=red] {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是对多边形进行分组或呈现的另一种方式。我认为这很赏心悦目,但这只是我的看法。

在这种情况下,每个多边形都插入到 中,tcolorbox并且所有这些框都会自动组织在 内tcbraster。每个框状栅格都包含一个组。

此解决方案可能存在的缺点:各组在视觉上是分开的。内部框根据列数调整宽度。所有行都包含相同数量的多边形。

可能的优势:仍然可以用循环填充它们foreach。每个组都可以有自己的方面。可以应用 提供的所有自定义选项tcolorbox

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{fit, positioning}

\newcommand{\grid}{\foreach \i in {0,1,2} \foreach \j in {0,1,2}
            \fill (\i,\j) coordinate (-\i-\j) circle (1pt);}
            
\tikzset{
    line join=round,
    polygon/.style={thick, blue},
    empty/.pic={
        \grid
    },
   one/.pic={
        \grid
        \draw[polygon] (0,0)--(2,2)|-cycle;
    },
   two/.pic={
        \grid
        \draw[polygon] (0,0)-|(2,1)--(1,2)--(1,1)--cycle;
    },
   three/.pic={
        \grid
        \draw[polygon] (0,0)-|(2,1)--(0,2)--(1,1)--cycle;
    },
   four/.pic={
        \grid
        \draw[polygon] (0,0)-|(2,1)-|(1,2)--cycle;
    },
   five/.pic={
        \grid
        \draw[polygon] (0,0) rectangle ++(2,1);
    },
   six/.pic={
        \grid
        \draw[polygon] (0,0)--(2,0)--(1,2)--(0,2)--(1,1)--cycle;
    },
   seven/.pic={
        \grid
        \draw[polygon] (0,0)--(2,0)--(1,2)--(0,1)--(1,1)--cycle;
    },
   eight/.pic={
        \grid
        \draw[polygon] (0,0)--(2,0)--(1,2)--cycle;
    },
   nine/.pic={
        \grid
        \draw[polygon] (0,0)--(1,0)--(2,1)|-(1,2)--(1,1)--cycle;
    },
   ten/.pic={
        \grid
        \draw[polygon] (0,0)--(1,0)--(2,1)--(2,2)--(1,1)--(1,2)--cycle;
    },
}

\begin{document}

\begin{tcboxedraster}[raster columns=5, raster equal height, tikz upper={x=0.75cm, y=0.75cm}, colback=orange!20]{title=I, fonttitle=\Large\bfseries}
\foreach \i in {one, two, three, four, five, six, seven, eight, nine, ten}{
    \begin{tcolorbox} \pic {\i}; \end{tcolorbox}}
\end{tcboxedraster}

\begin{tcboxedraster}[raster columns=4, raster equal height, tikz upper={x=0.75cm, y=0.75cm}, colback=green!20]{title=II, fonttitle=\Large\bfseries, colback=red!30}
\foreach \i in {one, two, three, four, five, six, seven, eight, nine, ten}{
    \begin{tcolorbox} \pic {\i}; \end{tcolorbox}}
\end{tcboxedraster}

\end{document}

在此处输入图片描述

相关内容