pattern=grid,如何简单增加网格步骤

pattern=grid,如何简单增加网格步骤

我有以下想法:

\documentclass[tikz, margin=3]{standalone}
\usetikzlibrary{patterns}

\begin{document}
    \begin{tikzpicture}[pattern color=gray!50]
\node (s)   [draw=gray!50, thick,
             preaction={pattern=grid},
             minimum width=30mm, minimum height=20mm,
             below right]  {some content};
\end{tikzpicture}
\end{document}

是否存在简单的方法来增加网格图案的网格步骤?例如,线条之间的距离为 5 毫米,线条为very thin?或者我应该定义新的网格图案?如果这是唯一的解决方案,请有人告诉我如何做到这一点?我希望改变图案密度,但我无法将此解决方案适应网格。

答案1

从您的评论中我了解到您想要一个随节点或其他路径移动的网格。这可以通过路径图轻松获得。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\pgfkeys{/Zarko/.cd,
grid/.style={draw=gray!50,thin},
x distance/.initial=8mm,
y distance/.initial=8mm,
distance/.code={\pgfkeys{/Zarko/x distance=#1,/Zarko/y distance=#1}}
}
\tikzset{Zarko grid/.style={/utils/exec=\pgfkeys{/Zarko/.cd,#1},
path picture={
\path[/Zarko/grid] let \p1=($(path picture bounding box.north east)-(path picture bounding
box.south west)$),\n1={int(\x1/\pgfkeysvalueof{/Zarko/x distance})},
\n2={int(\y1/\pgfkeysvalueof{/Zarko/y distance})} in 
foreach \XX in {1,...,\n1}
{ 
([xshift=\XX*\pgfkeysvalueof{/Zarko/x distance}]path picture bounding box.south west)
--
([xshift=\XX*\pgfkeysvalueof{/Zarko/x distance}]path picture bounding box.north west)
}
foreach \YY in {1,...,\n2} {
([yshift=\YY*\pgfkeysvalueof{/Zarko/y distance}]path picture bounding box.south west)
--
([yshift=\YY*\pgfkeysvalueof{/Zarko/y distance}]path picture bounding box.south east)
};
}}}
\begin{document}
    \begin{tikzpicture}
\node (s)   [Zarko grid,draw=gray!50, thick,         
             minimum width=30mm, minimum height=20mm]  {some content};

\node (s')   [Zarko grid={distance=3mm},draw=gray!50, thick,         
             minimum width=30mm, minimum height=24mm]  at (0,-4) {some content};
\draw[blue,Zarko grid={x distance=4mm,y distance=2mm,grid/.style={draw=red}}] (4,0) --(6,3) -- (5,-2) -- cycle;           
\end{tikzpicture}
\end{document}

在此处输入图片描述

可以通过添加一些来使其更加灵活x offsety offset但我想您可以在需要时这样做。

至于“诚实”的网格:我复制了我的旧回答看起来效果不错。(我只需要将颜色添加到网格的选项中,不知道为什么我没有在原始答案编辑:我清理了我的代码,当我写答案时我不知道,例如,calc 包(不是库)重新定义\setlength等等。所以我希望我的代码能不那么危险。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns}
\makeatletter
\newlength{\flex@pattern@density}
\newlength{\flex@pattern@linewidth}
\newlength{\flex@pattern@auxlength}
\newlength{\flex@pattern@auxlengthtwo}
\tikzset{/tikz/.cd,
    pattern density/.code={\flex@pattern@density=#1
    \flex@pattern@auxlength=1.1\flex@pattern@density
    \flex@pattern@auxlengthtwo=\flex@pattern@density
    \advance\flex@pattern@auxlengthtwo by 0.1pt
    %\typeout{\the\flex@pattern@density,\the\flex@pattern@auxlength}
    },
    pattern density=3pt,
    pattern line width/.code={\flex@pattern@linewidth=#1},
    pattern line width=0.4pt,
}


\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\tikz@pattern@color]{flexible horizontal lines}{\pgfpointorigin}{\pgfqpoint{100pt}{1pt}}{\pgfqpoint{100pt}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0pt}{0.5pt}}
  \pgfpathlineto{\pgfqpoint{100pt}{0.5pt}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\tikz@pattern@color]{flexible vertical lines}{\pgfpointorigin}{\pgfqpoint{1pt}{100pt}}{\pgfqpoint{\flex@pattern@density}{100pt}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@auxlengthtwo,\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength,\tikz@pattern@color]{flexible north east lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{\flex@pattern@auxlengthtwo}{\flex@pattern@auxlengthtwo}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@auxlengthtwo,\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength,\tikz@pattern@color]{flexible north west lines}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{\flex@pattern@auxlengthtwo}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{\flex@pattern@auxlengthtwo}}
  \pgfusepath{stroke}
}


% Crossed lines in different directions

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength,\tikz@pattern@color]{flexible grid}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{\flex@pattern@density}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{\flex@pattern@density}{0pt}}
  \pgfusepath{stroke}
}

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\flex@pattern@auxlength,\tikz@pattern@color]{flexible crosshatch}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{\flex@pattern@auxlength}{\flex@pattern@auxlength}}{\pgfqpoint{3pt}{3pt}}%
{
  \pgfsetlinewidth{\flex@pattern@linewidth}
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathmoveto{\pgfqpoint{\flex@pattern@density}{0pt}}
  \pgfpathlineto{\pgfqpoint{0pt}{\flex@pattern@density}}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}
  \pgfusepath{stroke}
}


% Dotted regions

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\tikz@pattern@color]{flexible dots}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{1pt}{1pt}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathcircle{\pgfqpoint{0pt}{0pt}}{\flex@pattern@linewidth}
  \pgfusepath{fill}
}

\pgfdeclarepatternformonly[\flex@pattern@density,\flex@pattern@linewidth,\tikz@pattern@color]{flexible crosshatch dots}{\pgfqpoint{-1pt}{-1pt}}{\pgfqpoint{2.5pt}{2.5pt}}{\pgfqpoint{\flex@pattern@density}{\flex@pattern@density}}%
{
  \pgfsetcolor{\tikz@pattern@color}
  \pgfpathcircle{\pgfqpoint{0pt}{0pt}}{\flex@pattern@linewidth}
  \pgfpathcircle{\pgfqpoint{1.5pt}{1.5pt}}{\flex@pattern@linewidth}
  \pgfusepath{fill}
}

\makeatother


\begin{document}
 \begin{tikzpicture}[>=stealth,every node/.style={font=\tiny}]
  \draw[very thick,
   pattern=flexible grid,pattern line
   width=0.4pt,pattern color=blue,
   pattern density=5mm] (0,5) -- (0,0) -- (5,0) -- (5,5);
  \begin{scope}[pattern density=5mm,pattern line
   width=0.4pt]
    \node (s)  [draw=gray!50, thick,pattern color=gray!50,
             preaction={pattern=flexible grid},
             minimum width=30mm, minimum height=20mm,
             below right] at (7,0)  {some content};
  \end{scope}               
 \end{tikzpicture}

\end{document}

在此处输入图片描述

附录:如果我用同样的序言

\begin{document}
\foreach \X in {2,2.033,...,4,3.967,...,2}
{\begin{tikzpicture}[>=stealth,every node/.style={font=\tiny}]
  \draw (0,-3) rectangle (8,5);
  \begin{scope}[pattern density=5mm,pattern line
   width=0.4pt]
    \node (s)  [draw=gray!50, thick,pattern color=gray!50,
             preaction={pattern=flexible grid},
             minimum width=30mm, minimum height=20mm,
             below right] at (\X,{sin(\X*90)})  {some content};
    \node (s')  [draw=gray!50, thick,pattern color=gray!50,
             preaction={pattern=grid},
             minimum width=30mm, minimum height=20mm,
             below right] at (\X,{3+sin(\X*90)})  {some content};
  \end{scope}               
\end{tikzpicture}}
\end{document}

我得到了这个“美丽”的动画:

在此处输入图片描述

它表明,从原则上讲,各个模式在击中节点边界方面是相等的,只是从统计上讲,更精细的模式有更好的机会做到这一点,但除此之外,各个模式都处于相同的基础上。

相关内容