TikZ:用点网格填充矩形

TikZ:用点网格填充矩形

我正在尝试以规则的方式用点填充两点之间的矩形。听起来像是一种模式,但显然,模式在绝对位置方面会带来困难。因此,我尝试了以下方法:

\documentclass{scrartcl}
\usepackage{tikz}
\tikzset{
    grid/.style={
        to path={
            coordinate (from)
            ++(1,1) coordinate (step)
            (\tikztotarget) coordinate (to)
            \pgfextra{
                \path (from);
                \pgfgetlastxy{\XFrom}{\YFrom}
                \path (step);
                \pgfgetlastxy{\XStep}{\YStep}
                \path (to);
                \pgfgetlastxy{\XTo}{\YTo}
                \edef\List{\XFrom, \XStep, ...pt, \XTo}
                \show\List
                \foreach \i in \List {
                    \fill[black] (\i, 0pt) circle (0.3pt);
                }
            }
        }
    }
}
\begin{document}
    \begin{tikzpicture}
        \draw (-1,-1) edge[grid] (5,5);
    \end{tikzpicture}
\end{document}

这不起作用:尽管\show\List给出了->-28.45274pt, 0.0pt, ...pt, 142.26372pt.,但以下内容\foreach无法解析模式。为什么?

答案1

您可以使用 和griddash pattern执行此操作line cap=round

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}

\tikzset{mygrid/.style={line width=3pt, dash pattern=on 0mm off 10mm, line cap=round}}

\begin{document}

\begin{tikzpicture}
\fill[gray!20] (-1.4,-1.5) rectangle (5.3,5.2);
\draw[mygrid] (-1,-1) grid (5,5);
\end{tikzpicture}

\end{document}

答案2

您可以使用path picture来实现这一点:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\tikzset{
    pseudo pattern/.style={
        fill=yellow,
        path picture={
            \path (path picture bounding box.south west);
            \pgfgetlastxy{\xsw}{\ysw}
            \path (path picture bounding box.north east);
            \pgfgetlastxy{\xne}{\yne}
            \pgfmathsetmacro{\w}{\xne / 1cm - \xsw / 1cm + 1}
            \pgfmathsetmacro{\h}{\yne / 1cm - \ysw / 1cm + 1}
            \begin{scope}[shift={(path picture bounding box.south west)}]
                \foreach \x in {0, 1, ..., \h} {
                    \foreach \y in {0, 1, ..., \w} { 
                        \fill[red] (\x,\y) circle[radius=1pt];
                    }
                }
            \end{scope}
        }
    }
}
\begin{document}
    \begin{tikzpicture}
        \fill[pseudo pattern] (-1,-1) rectangle (5,5);
    \end{tikzpicture}
\end{document}

在此处输入图片描述


请注意,图案在页面上是绝对定位的,但它们的位置不一定总是在原点tikzpicture。请参阅以下 MWE:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns.meta}
\begin{document}
\begin{tikzpicture} 
    \draw[pattern={Dots[distance=1cm]}] (0.5,-1) rectangle (5,5);
    \draw[red, pattern={Dots[distance=1cm]}, pattern color=red] (1,-1) rectangle (5.5,5);
    %\path (-0.25,0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

您可以清楚地看到,第一个和第二个图案的点完全位于同一位置,尽管其中一个矩形偏移了 0.5 厘米。如果您取消命令\path,您将看到点向左移动,但两个矩形的图案再次匹配。这样一来,您说得对,判断图案的原点并不完全可靠。

在此处输入图片描述


为了确保模式的一个点始终位于(0,0),我原来的答案可以修改如下:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\newlength{\pseudopatternstep}
\setlength{\pseudopatternstep}{1cm}
\tikzset{
    pseudo pattern step/.code={\pgfmathsetlength{\pseudopatternstep}{#1}},
    pseudo pattern color/.style={.},
    pseudo pattern/.style={
        fill=none,
        path picture={
            \path (path picture bounding box.south west);
            \pgfgetlastxy{\xsw}{\ysw}
            \path (path picture bounding box.north east);
            \pgfgetlastxy{\xne}{\yne}
            \pgfmathsetmacro{\ox}{int(\xsw / \the\pseudopatternstep) * \the\pseudopatternstep / 1cm}
            \pgfmathsetmacro{\oy}{int(\ysw / \the\pseudopatternstep) * \the\pseudopatternstep / 1cm}
            \pgfmathsetmacro{\sx}{\the\pseudopatternstep / 1cm + \ox}
            \pgfmathsetmacro{\sy}{\the\pseudopatternstep / 1cm + \oy}
            \pgfmathsetmacro{\w}{\xne / 1cm - \xsw / 1cm + 1}
            \pgfmathsetmacro{\h}{\yne / 1cm - \ysw / 1cm + 1}
            \begin{scope}
                \foreach \x in {\ox, \sx, ..., \h} {
                    \foreach \y in {\oy, \sy, ..., \w} { 
                        \fill[pseudo pattern color] (\x,\y) circle[radius=1pt];
                    }
                }
            \end{scope}
        }
    }
}
\begin{document}
    \begin{tikzpicture}
        \draw (0,0) circle[radius=5pt];
        \fill[draw, pseudo pattern, pseudo pattern color/.style=black!20] (0,0) rectangle (5,5);
        \fill[draw=red, pseudo pattern, pseudo pattern color/.style=black!40] (-0.5,-0.5) rectangle (4.5,4.5);
        \fill[draw=blue, pseudo pattern] (-2.25,-2.25) rectangle (2.75,2.75);
    \end{tikzpicture}
    \begin{tikzpicture}[pseudo pattern step=10pt]
        \draw (0,0) circle[radius=5pt];
        \fill[draw, pseudo pattern, pseudo pattern color/.style=black!20] (0,0) rectangle (5,5);
        \fill[draw=red, pseudo pattern, pseudo pattern color/.style=black!40] (-0.5,-0.5) rectangle (4.5,4.5);
        \fill[draw=blue, pseudo pattern] (-2.25,-2.25) rectangle (2.75,2.75);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容