TikZ 没有在网格中绘制某些线条

TikZ 没有在网格中绘制某些线条

当我使用 \draw 创建网格时,缺少几条线。请参阅所附代码和图像,例如,虚线红色网格缺少底部和左侧边界。实际上实线绿色和实线红色网格也缺少它。我不确定我是否遇到了图形错误,或者这是否是预期行为:

缺失的线条

\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}

\pgfdeclarelayer{level0}
\pgfdeclarelayer{level1}
\pgfdeclarelayer{level2}
\pgfsetlayers{main,level0,level1,level2}

\begin{document}

\begin{figure}[h]
  \centering
    \begin{tikzpicture}[scale=2]
      \begin{pgfonlayer}{level0} % Level 0
        \draw[help lines] (0,0) grid (5,5); % Base grid
        \draw[very thick, scale=5] (0,0) grid (1,1); % Darker lines to mark e.g. box boundaries
      \end{pgfonlayer}

      \begin{pgfonlayer}{level1} % Level 1
        \draw[green, dashed] (.5,.5) grid[step=.5] (4.5,4.5); % Ghost cells
        \draw[green] (1,1) grid[step=.5] (4,4);
      \end{pgfonlayer}

      \begin{pgfonlayer}{level2} % Level 2
        \draw[red, dashed] (1.75,1.75) grid[step=.25] (3.25,3.25); % Ghost cells
        \draw[red] (2,2) grid[step=.25] (3,3);
      \end{pgfonlayer}

    \end{tikzpicture}
    \caption{Standard isotropic refinement.}
\end{figure}

\end{document}

谢谢你!

答案1

PGF 手册中有关该grid操作的部分(我的版本为 14.9)指出:

需要注意的是,网格总是“分阶段”的,如果点 (0,0) 恰好位于矩形内,则网格包含该点。因此,网格并不总是在角点处有交点;只有当角点是步进的倍数时才会发生这种情况。注意,由于舍入误差,网格的“最后”几行可能会被省略。在这种情况下,您必须在角点上添加一个 epsilon。[强调添加]

不确定为什么您错过了网格开头而不是结尾的线,但无论如何,类似的解决方法都是有效的。

\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}

\pgfdeclarelayer{level0}
\pgfdeclarelayer{level1}
\pgfdeclarelayer{level2}
\pgfsetlayers{main,level0,level1,level2}

\begin{document}

\begin{figure}[h]
  \centering
    \begin{tikzpicture}[scale=2]
      \begin{pgfonlayer}{level0} % Level 0
        \draw[help lines] (0,0) grid (5,5); % Base grid
        \draw[very thick, scale=5] (0,0) grid (1,1); % Darker lines to mark e.g. box boundaries
      \end{pgfonlayer}

      \begin{pgfonlayer}{level1} % Level 1
        \draw[green, dashed] (.4999,.4999) grid[step=.5] (4.5,4.5); % Ghost cells
        \draw[green] (0.999,0.999) grid[step=.5] (4,4);
      \end{pgfonlayer}

      \begin{pgfonlayer}{level2} % Level 2
        \draw[red, dashed] (1.74999,1.74999) grid[step=.25] (3.25,3.25); % Ghost cells
        \draw[red] (2,2) grid[step=.25] (3,3);
      \end{pgfonlayer}

    \end{tikzpicture}
    \caption{Standard isotropic refinement.}
\end{figure}

\end{document}

示例代码输出

相关内容