使用 tikzpicture 移动网格中的线条

使用 tikzpicture 移动网格中的线条

我在制作网格时遇到了麻烦tikzpicture。垂直线和水平线有些错位。这类似于TikZ 没有在网格中绘制某些线条。我一直在尝试向角点添加少量(“epsilons”),但无济于事。这是我的代码:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

%% Rulers -------------------------
\draw [black!80, dotted] (0, 0) -- (9, 0);
\draw [black!80, dotted] (0, 4) -- (9, 4);
\draw (-0.25, 0) -- (.25, 0);
\draw (-0.25, 4) -- (.25, 4);
\draw [black!50, thick, dashed] (0, .5) -- (1.5, .5);
\draw [black!50, thick, dashed] (0, .25) -- (3.25, .25);
\node (A) at (0,2) {$\Delta r$};
\draw[->] (A) -- (0,4);
\draw[->] (A) -- (0,0);

% First and second grids
\draw[step=1,black,thin] (1,0) grid (2,4);
\draw[step=0.5, black,thin] (2.99,0) grid (3.5,4);
% cell centers
\foreach \r in {.5, 1.5,..., 3.5}
\fill [] (1.5, \r)  circle [radius=1pt];
\foreach \r in {.25, .75,..., 3.75}
\fill [] (3.25, \r)  circle [radius=1pt];

% -----------------------------------------------
% Third anf fourth grids
\draw[step=4/3,black,thin] (5-2/3,-2/3) grid (5+2/3,4+2/3);
\draw[step=2/3,black,thin] (6+2/3,-1/3) grid (7+1/3,4+1/3);
% cell centers
\foreach \r in {0, 4/3, 8/3, 4}
\fill [] (5, \r)  circle [radius=1pt];
\foreach \r in {0, 0.6666, ..., 4}
\fill [] (7, \r)  circle [radius=1pt];


\node (notea) at (1.5,-1) {$(a)$};
\node (noteb) at (3.25,-1) {$(b)$};
\node (notec) at (5,0-1) {$(c)$};
\node (noted) at (7,-1) {$(d)$};

\end{tikzpicture}
\end{document}

我得到的输出如下所示:

在此处输入图片描述

但我希望它看起来像这样(红色表示差异):

在此处输入图片描述

我怀疑这与我选择的长度单位有关,因为我有 1/3 的倍数,但最终它们加起来不是整数。\draw[step=4/3,black,thin] (5-2/3,-2/3) grid (5+2/3,4+2/3);

答案1

这是一个可能的解决方案。我使用了nodes而不是grid。这样,就可以很容易地将所有需要完成的工作存储到foreach-循环中,而不必手动进行计算。

\documentclass[tikz, border=6mm]{standalone}

\pgfmathsetmacro{\mya}{4/3}
\pgfmathsetmacro{\myb}{2/3}

\begin{document}
  \begin{tikzpicture}[>=latex]
    \draw [<->] (.5,0) -- ++(0,4) node [midway, fill=white] {$\Delta r$};

    \foreach \y in {0,4}    {
        \draw (.25,\y) --++ (.5,0);
        \draw [dotted] (.25,\y) --++ (8.5,0);
    }

    \draw [blue, dashed] (.5,.5) -- ++(1.5,0);
    \draw [blue, dashed] (.5,.25) -- ++(3.5,0);

    \begin{scope}[every node/.style={draw, rectangle}]]
        \foreach \y in {0,...,3} \fill (2,\y+.5) circle (1pt) node [minimum size=1cm] {};
        \foreach \y in {0,.5,...,3.5} \fill (4,\y+.25) circle (1pt) node [minimum size=.5cm] {};    
        \foreach \y in {0,\mya,...,4} \fill (6,\y) circle (1pt) node [minimum size=\mya cm] {}; 
        \foreach \y in {0,\myb,...,4} \fill (8,\y) circle (1pt) node [minimum size=\myb cm] {}; 
    \end{scope}

    \foreach \x\l in {2/a,4/b,6/c,8/d} \node at (\x,-1.5) (note\l) {$(\l)$};
  \end{tikzpicture}
\end{document}

渲染图像

答案2

您可以创建一个pic绘制网格和点的窗口。然后将其放置在您想要的位置。

\documentclass[tikz,border=5]{standalone}
% define pic that draw a grid and dots
% #1 = number of squares, #2 = scale factor
\tikzset{
  pics/mygrid/.style 2 args={
    code={
      \draw[pic actions,scale=#2,shift={(-.5,-.5)}] (0,0) grid (1,#1);
      \fill[pic actions] foreach \i in {1,...,#1}{(0,{#2*(\i-1)}) circle (1pt)};
    }
  }
}
\begin{document}
\begin{tikzpicture}
  % Rulers -------------------------
  \draw [black!80, dotted] (0, 0) -- (9, 0) (0, 4) -- (9, 4);
  \draw [black!50, thick, dashed] (0, .5) -- (1.5, .5) (0, .25) -- (3.25, .25);
  \draw[|<->|] (0,0) -- node[fill=white]{$\Delta r$} (0,4);
  % Grids -------------------------
  \draw (1.5,.5) pic{mygrid={4}{1}}
        (3.25, .25) pic{mygrid={8}{.5}}
        (6, 0) pic{mygrid={4}{4/3}}
        (8, 0) pic{mygrid={7}{4/6}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容