TikZ 中的网格图案

TikZ 中的网格图案

参考答案我如何用倾斜的平行线填充此区域,图案也生成了网格线。我无法理解这种行为。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \coordinate (B) at (8,0);
  \draw[pattern=north east lines] (A) arc(180:360:2) arc(180:0:2) arc(0:180:4); 
\end{tikzpicture}
\end{document} 

我已在网格中突出显示一个单元格以供参考:

在此处输入图片描述

在此处输入图片描述

根据 Phelype Oleinik 的建议,更新后的代码如下所示:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}

\pgfdeclarepatternformonly{large north east lines}{%
\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{4pt}{4pt}}
{\pgfqpoint{3pt}{3pt}}% 
{% 
\pgfsetlinewidth{0.4pt}% 
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}% 
\pgfpathlineto{\pgfqpoint{31pt}{31pt}}% 
\pgfusepath{stroke}% 
}%

\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \coordinate (B) at (8,0);
  \draw[pattern=large north east lines] (A) arc(180:360:2) arc(180:0:2) arc(0:180:4); 
\end{tikzpicture}
\end{document}

它生成以下输出(仍然不令人满意):

在此处输入图片描述

答案1

人们想要使用模式的原因是它们易于使用且速度快。如果您对结果不满意,因为它在查看器上看起来不太好,您可以随时使用简单的 foreach 循环。这需要更长的时间和更多的努力,但在任何查看器上看起来都应该不错。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \coordinate (B) at (8,0);
  \draw[clip] (A) arc(180:360:2) arc(180:0:2) arc(0:180:4); 
  \foreach \X in {0,0.25,...,12}
  {\draw (\X-6,-2) -- ++ (45:15);}
\end{tikzpicture}
\end{document} 

在此处输入图片描述

尽管你可能无法轻易加快编译时间,但使用path pictures 让这个东西更易于使用却相当简单。然后,绘制区域就变得非常简单了,只需说

\draw[Subham pattern] (A) arc(180:360:2) arc(180:0:2) arc(0:180:4); 

这是定义和使用的代码Subham pattern

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfkeys{/Subham/.cd,
grid/.style={draw=gray!50,thin},
distance/.initial=4mm,
distance/.code={\pgfkeys{/Subham/distance=#1}}
}
\tikzset{Subham pattern/.style={/utils/exec=\pgfkeys{/Subham/.cd,#1},
path picture={
\path[/Subham/grid] let \p1=($(path picture bounding box.north east)-(path picture bounding
box.south west)$),\n1={int(\x1/\pgfkeysvalueof{/Subham/distance})},
\n2={int(\y1/\pgfkeysvalueof{/Subham/distance})},
\n3={sqrt(2)*max(\x1,\y1)} in 
foreach \XX in {0,...,\n1}
{ 
([xshift=\XX*\pgfkeysvalueof{/Subham/distance}]path picture bounding box.south west)
-- ++ (45:\n3)
}
foreach \YY in {1,...,\n2} {
([yshift=\YY*\pgfkeysvalueof{/Subham/distance}]path picture bounding box.south west)
-- ++ (45:\n3)
};
}}}

\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \coordinate (B) at (8,0);
  \draw[Subham pattern] (A) arc(180:360:2) arc(180:0:2) arc(0:180:4); 
\end{tikzpicture}
\end{document} 

在此处输入图片描述

这有几个额外的优点,因为您可以随意改变样式和密度,并且可以轻松调整孵化角度等等。

相关内容