Naming intersection points of a grid

If you remove the units from the steps and do, say, \begin{tikzpicture}[x=2cm,y=2cm] this will increase the step of the grid while keeping the same intersections (or lattice nodes) connected.

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{N}
\begin{center}
\begin{tikzpicture}[x=2cm,y=2cm]
\draw[step=.5,color=gray] (0,0) grid (5,4);
\draw<2->[ultra thick, green, -latex] (1,1) -- (2,2);
\draw[blue!40!black, thick, fill=blue!40!white, opacity=.5] (3,3) circle (.2cm) node (c){};
\draw<3->[ultra thick, red, -latex] (c.center) -- (4,3);
\node at (1,1){a};
\node at (2,2){b};
\node at (3,3){x};
\node at (4,3){z};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

enter image description here


Second try at it. I hope I understood you: what I am doing here is building the grid by hand, and automatically naming the intersections. You can change the grid parameters (x and y step) and let everything else untouched; the intersections are called g-<column>-<row>.

Another possibility is to use a TikZ matrix of nodes for this.

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
    \frametitle{N}
    \begin{center}
        \begin{tikzpicture}
            %  \draw[step=.5cm,color=gray] (0,0) grid (8,8);
            \def\mystepx{0.8cm}
            \def\mystepy{0.5cm}
            \foreach  \x in {0,...,8} {
                % draw rows and columns
                \draw [red] (\x*\mystepx, 0) -- (\x*\mystepx,8*\mystepy);
                \draw [blue] (0,\x*\mystepy) -- (8*\mystepx,\x*\mystepy);
                \foreach \y in {0,...,8} {
                        \coordinate (g-\x-\y) at (\x*\mystepx,\y*\mystepy);
                    }
                }
            %
            \node at (g-1-1){a};
            \node at (g-2-2){b};
            \node at (g-3-3){x};
            \node at (g-4-3){z};
            \draw<2->[ultra thick, green, -latex] (g-1-1) -- (g-2-2);
            \draw[blue!40!black, thick, fill=blue!40!white, opacity=.5] (g-3-3) circle (.2cm) node (c){};
            \draw<3->[ultra thick, red, -latex] (c.center) -- (g-4-3);
        \end{tikzpicture}
    \end{center}
\end{frame}
\end{document}

result of above code


Here is an example of to path style that name all intersections of a grid. When you set named grid=test the intersections will be named as test-1-1, test-1-2, ... and so on.

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{calc}
% The x-step and the y-step lengths
\newlength{\dx}\setlength{\dx}{1cm}
\newlength{\dy}\setlength{\dy}{1cm}
% recover the steps
\makeatletter
\let\pgf@pathgrid@original\pgf@pathgrid
\def\pgf@pathgrid[#1]#2#3{%
  \pgfset{#1}%
  \pgfmathsetlength\dx{\pgfkeysvalueof{/pgf/stepx}}%
  \global\dx=\dx
  \pgfmathsetlength\dy{\pgfkeysvalueof{/pgf/stepy}}%
  \global\dy=\dy
  \pgf@pathgrid@original[#1]{#2}{#3}
}
\makeatother
% define the `named grid` style
\tikzset{
  named grid/.style={
    to path={
      (\tikztostart) grid (\tikztotarget)
      let \p1=(\tikztostart), \p2=(\tikztotarget),
      \n1={min(\x1,\x2)},\n2={max(\x1,\x2)},
      \n3={ceil(\n1/\dx)},\n4={\n3+1},\n5={int(\n2/\dx)},
      \n6={min(\y1,\y2)},\n7={max(\y1,\y2)},
      \n8={ceil(\n6/\dy)},\n9={\n8+1},\n{10}={int(\n7/\dy)}
      in
      foreach[count=\nx from 0] \x in {\n3,\n4,...,\n5}{
        foreach[count=\ny from 0] \y in {\n8,\n9,...,\n{10}}{
          (\x*\dx,\y*\dy) coordinate (#1-\nx-\ny) % <- name the intersections
        }
      }
    }
  },
  named grid/.default=grid
}
\begin{document}
  \begin{tikzpicture}
    \draw[rotate=35,xstep=.7,ystep=.35] (-.5,-1.5) to[named grid=test] (3.5,1.5)
      (test-1-1) edge[-latex,red,thick] (test-5-7)
      (test-0-0) circle(3pt) (test-5-8) circle(3pt);
  \end{tikzpicture}
\end{document}

enter image description here

Notes :

  • This code is note very stable : it doesn't work well with non linear transforms.
  • In a strange way, the most difficult part for me was to recover the steps.

Tags:

Tikz Pgf