在 LaTeX 中绘制分区

在 LaTeX 中绘制分区

我想像这样绘制 n=4 的分区格子n=4 的分割格

我正在寻找有关如何执行此操作的信息,然后我发现包 tikz-cd 可能很有用,但我自己能做的唯一一件事就是使用以下命令绘制一个矩形:

    \begin{tikzcd}
    \node[regular polygon, regular polygon sides=4, minimum size=1cm] (A){};
    \filldraw[line width=3pt, gray, line join=round] (A.corner 1)--(A.corner 2)--(A.corner 3)--(A.corner 4)--cycle;
    \end{tikzcd}

给出如下输出;

在此处输入图片描述

我还发现了如何使用 tikzcd 绘制箭头:

\begin{tikzcd}
A \arrow[rd] \arrow[r] & B \\
& C
\end{tikzcd}

在此处输入图片描述

现在我不知道如何将这两件事联系起来。也许这是解决这个问题完全没有意义的办法,还有更好的方法。

答案1

我认为肯定有更简单的方法,但这里有一个可能的解决方案。我稍微改变了一些“骰子”的位置,以提高连接它们的线条的可见性。这些线都是直的,有些可以是弯曲的,但必须将它们从中移除\foreach

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

% Colors
\definecolor{colorB} {HTML}{E57FE4}
\definecolor{colorC1}{HTML}{F8B87F}
\definecolor{colorC2}{HTML}{CCADA2}
\definecolor{colorD} {HTML}{7FD47F}
\definecolor{colorE} {HTML}{FF7F7F}

% Layers
\pgfdeclarelayer{dice}
\pgfdeclarelayer{parts}
\pgfdeclarelayer{points}
\pgfsetlayers   {main,dice,parts,points}

% Macros
\newcommand{\die}[2] % name, position
{%
  \node (#1) at #2 {};
  \begin{scope}[shift={#2}]
    \begin{pgfonlayer}{dice}
      \draw[fill=white,rounded corners=0.2cm] (-0.5,-0.5) rectangle (0.5,0.5);
    \end{pgfonlayer}
    \foreach\i in {45,135,225,315}
    {%
      \begin{pgfonlayer}{dice}
        \fill[gray!50] (\i:{0.3*sqrt(2)}) circle (0.15);
      \end{pgfonlayer}
      \begin{pgfonlayer}{points}
        \fill (\i:{0.3*sqrt(2)}) circle (0.1);
      \end{pgfonlayer}
    }
  \end{scope}
}

\newcommand{\twopart}[3] % Node, rotation, color
{%
  \ifthenelse{#2=45 \OR #2=135 \OR #2=225 \OR #2=315}
    {% if the angle is 45 (or equivalent)
      \pgfmathsetmacro\factor{sqrt(2)} % distance between the two points
      \pgfmathsetmacro\delta {0}       % displacement from the origin
    }
    {% for any other angle
      \pgfmathsetmacro\factor{1}
      \pgfmathsetmacro\delta {-0.3}
    }
  \begin{pgfonlayer}{parts}
    \begin{scope}[shift={(#1)}, rotate=#2]
      \fill[#3,rounded corners=0.15cm] (-0.3*\factor-0.15,\delta-0.15) rectangle (0.3*\factor+0.15,\delta+0.15);
    \end{scope}
  \end{pgfonlayer}
}

\newcommand{\threepart}[2] % Node, rotation
{%
  \begin{pgfonlayer}{parts}
    \begin{scope}[shift={(#1)}, rotate=#2]
      \fill[colorD] (-0.3,0.3)++(45:.15) arc (45:180:0.15) --(-0.45,-0.3)
                arc (180:270:0.15) -- (0.3,-0.45) arc (-90:45:0.15) -- cycle;
    \end{scope}
  \end{pgfonlayer}
}

\newcommand{\fourpart}[1] % Node
{%
  \begin{pgfonlayer}{parts}
    \begin{scope}[shift={(#1)}]
      \fill[colorE,rounded corners=0.15cm] (-0.45,-0.45) rectangle (0.45,0.45);
    \end{scope}
  \end{pgfonlayer}
}

\begin{document}
\begin{tikzpicture}
  % DICE
  \die{A1}{( 0, 0)}
  \die{B1}{(-5, 3)}\twopart  {B1}{270}{colorB}
  \die{B2}{(-3, 3)}\twopart  {B2}{ 45}{colorB}
  \die{B3}{(-1, 3)}\twopart  {B3}{  0}{colorB}
  \die{B4}{( 1, 3)}\twopart  {B4}{180}{colorB}
  \die{B5}{( 3, 3)}\twopart  {B5}{135}{colorB}
  \die{B6}{( 5, 3)}\twopart  {B6}{ 90}{colorB}
  \die{C1}{( 0, 9)}\twopart  {C1}{  0}{colorC1}\twopart{C1}{180}{colorC2}
  \die{C2}{(-2, 9)}\twopart  {C2}{ 45}{colorC1}\twopart{C2}{135}{colorC2}
  \die{C3}{( 2, 9)}\twopart  {C3}{270}{colorC1}\twopart{C3}{ 90}{colorC2}
  \die{D1}{(-6,10)}\threepart{D1}{270}
  \die{D2}{(-4,10)}\threepart{D2}{  0}
  \die{D3}{( 4,10)}\threepart{D3}{ 90}
  \die{D4}{( 6,10)}\threepart{D4}{180}
  \die{E1}{( 0,12)}\fourpart {E1}
  % PATHS
  \foreach\i in {1,...,6}
  {% paths from A1
    \path[thick] (A1) edge (B\i);
  }
  \foreach\i in {C3,D1,D2}
  {% paths from B1
    \path[thick] (B1) edge (\i);
  }
  \foreach\i in {C2,D1,D3}
  {% paths from B2
    \path[thick] (B2) edge (\i);
  }
  \foreach\i in {C1,D2,D3}
  {% paths from B3
    \path[thick] (B3) edge (\i);
  }
  \foreach\i in {C1,D1,D4}
  {% paths from B4
    \path[thick] (B4) edge (\i);
  }
  \foreach\i in {C2,D2,D4}
  {% paths from B5
    \path[thick] (B5) edge (\i);
  }
  \foreach\i in {C3,D3,D4}
  {% paths from B6
    \path[thick] (B6) edge (\i);
  }
  \foreach\i in {C1,C2,C3,D1,D2,D3,D4}
  {% paths from E1
    \path[thick] (E1) edge (\i);
  }
\end{tikzpicture}
\end{document}

还有格子: 在此处输入图片描述

相关内容