在 tikz 中划分和选择性填充区域

在 tikz 中划分和选择性填充区域

使用 TikZ 绘制一个简单的盾牌形状,如下所示:

\documentclass{article} 
\usepackage{tikz} 
\begin{document} 

\tikz \draw (0,0) --+ (0,-1)
       arc [radius=1, start angle=-180, end angle=0]
       --+ (0,1) -- cycle;

\end{document}   

我正在努力弄清楚如何将这个形状分成六个区域 - 本质上是一个有 3 列和 2 行的棋盘图案,然后用黑色填充 [第 1 行,第 2 列],[第 2 行,第 1 列],[第 2 行,第 3 列]。

答案1

更新:具有任意数量的行和列,以及任意颜色。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\tikzset{checkerboard/.style args={%
    with #1 rows and #2 columns and colors #3 and #4}{path picture={
    \path (path picture bounding box.north west) coordinate (tmpCBNW)
    (path picture bounding box.north east) coordinate (tmpCBNE)
    (path picture bounding box.south west) coordinate (tmpCBSW)
    ($ {1/#2}*(tmpCBNE) - {1/#2}*(tmpCBNW)$) coordinate (tmpCBX)
    ($ {1/#1}*(tmpCBSW) - {1/#1}*(tmpCBNW)$) coordinate (tmpCBY);
    \foreach \X in {1,...,#2}
{\foreach \Y [evaluate=\Y as \Z using int(\X+\Y)]in {1,...,#1}
{\ifodd\Z
 \fill[#3] ($ \X*(tmpCBX) + \Y*(tmpCBY) $) rectangle ++ ($ -1*(tmpCBX) - (tmpCBY) $) ;
\else
 \fill[#4] ($ \X*(tmpCBX) + \Y*(tmpCBY) $) rectangle ++ ($ -1*(tmpCBX) - (tmpCBY) $) ;
\fi
}}}
}}

\begin{document} 
\begin{tikzpicture}
\draw[checkerboard=with 2 rows and 3 columns and colors black and white] (0,0) --+ (0,-1)
       arc [radius=1, start angle=-180, end angle=0]
       --+ (0,1) -- cycle;
\begin{scope}[xshift=4cm]
\draw[checkerboard=with 4 rows and 3 columns and colors blue and red] (0,0) --+ (0,-1)
       arc [radius=1, start angle=-180, end angle=0]
       --+ (0,1) -- cycle;
\end{scope}
\begin{scope}[xshift=8cm]
\draw[checkerboard=with 3 rows and 7 columns and colors yellow and blue] (0,0) --+ (0,-1)
       arc [radius=1, start angle=-180, end angle=0]
       --+ (0,1) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}   

在此处输入图片描述

原始答案

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document} 
\begin{tikzpicture}
\draw[clip] (0,0) --+ (0,-1)
       arc [radius=1, start angle=-180, end angle=0]
       --+ (0,1) -- cycle;
\foreach \X in {1,2,3}
{\foreach \Y [evaluate=\Y as \Z using int(\X+\Y)]in {1,2}
{\ifodd\Z
 \fill ({2*(\X-1)/3},{-(\Y-1)}) rectangle ({2*(\X)/3},{-\Y}) ;
\else
\fi
}}
\end{tikzpicture}
\end{document}   

在此处输入图片描述

答案2

您可以使用path picture和相应的path picture bounding box来绘制背景。

\documentclass[tikz,border=7pt]{standalone}
\tikzset{
  chess/.style={
    path picture={
      \fill (path picture bounding box.west)
          -|(path picture bounding box.south east)
          -|(path picture bounding box.71)
          -|(path picture bounding box.251)
          -|cycle;
    }
  }
}
\begin{document}
  \tikz \draw[chess] (0,0) --+ (0,-1)
       arc [radius=1, start angle=-180, end angle=0]
       |- cycle;
\end{document}

在此处输入图片描述

笔记:对于另一种形状,您应该调整(path picture bounding box.71)(path picture bounding box.251),或者对于一般情况,您可以使用更复杂($(path picture bounding box.north east)!1/3!(path picture bounding box.north west)$)($(path picture bounding box.south east)!2/3!(path picture bounding box.south west)$)需要calc库。

笔记2:您也可以使用此路径直接填充(不带path picture):

\fill (2/3,0) -| (4/3,{-1-sin(acos(1/3))})
   arc [radius=1, start angle=-acos(1/3), end angle=0]
   -- (0,-1)
   arc [radius=1, start angle=-180, end angle=-acos(-1/3)]
   -- cycle;

相关内容