# Aligning x- and y-axis with lattice points on a graph using PGF/TikZ

This question is a consequence of a previous question that I posted on the group regarding the plotting of lattice points or a trellis on an axis (ref. Drawing lattice/trellis graphs using PGF/TikZ).

On the advice of two group members, I have been able to plot something that looks like this:

The code that I used to generate this graph is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{figure}[h!]
\centering
\begin{tikzpicture}
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (-3,0);
\coordinate (XAxisMax) at (5,0);
\coordinate (YAxisMin) at (0,-2);
\coordinate (YAxisMax) at (0,5);

\coordinate (Bone) at (0,2);
\coordinate (Btwo) at (2,-2);

\draw [thin, gray,-latex] (XAxisMin) -- (XAxisMax);% Draw x axis
\draw [thin, gray,-latex] (YAxisMin) -- (YAxisMax);% Draw y axis

\clip (-3,-2) rectangle (10cm,10cm); % Clips the picture...
\pgftransformcm{1}{0.6}{0.7}{1}{\pgfpoint{3cm}{3cm}} % This is actually the transformation
%  matrix entries that gives the slanted
% unit vectors. You might check it on
% MATLAB etc. . I got it by guessing.

\draw[style=help lines,dashed] (-14,-14) grid[step=2cm] (14,14); % Draws a grid in the new coordinates.
%    \filldraw[fill=gray, fill opacity=0.3, draw=black] (0,0) rectangle (2,2); % Puts the shaded rectangle
\foreach \x in {-7,-6,...,7}{                           % Two indices running over each
\foreach \y in {-7,-6,...,7}{                       % node on the grid we have drawn
\node[draw,circle,inner sep=2pt,fill] at (2*\x,2*\y) {}; % Places a dot at those points
}
}

\draw [ultra thick,-latex,red] (0,0) -- (0,2) node [above left] {$b_1$};
\draw [ultra thick,-latex,red] (0,0) -- (2,-2) node [below right] {$b_2$};
\draw [ultra thick,-latex,red] (0,0) -- ($(0,2)+(2,-2)$) node [below right] {$b_1+b_2$};
\draw [ultra thick,-latex,red] (0,0) -- ($2*(0,2)+(2,-2)$) node [above left] {2$b_1+b_2$};
\draw [thin,-latex,red, fill=gray, fill opacity=0.3] (0,0) -- ($2*(0,2)+(2,-2)$) --
($3*(0,2)+2*(2,-2)$) -- ($(0,2)+(2,-2)$) -- cycle;
\end{tikzpicture}
\caption{Babai's algorithm works poorly if the basis is bad''.}
\end{figure}

\end{document}


The problem that I am experiencing is not being able to align the axis with the origin of the lattice. Also, the variables that have been defined for the origin, and other points on the lattice do not appear to have the effect that I expect (that is, they appear to be absolute coordinates relative to the axis, as opposed to points relative to the lattice). Consequently, I have to hard code these when it comes to plotting them.

Any advice as to what I am doing wrong would be appreciated.

-
It seems as f you are doing the shaded area after the transformation, try moving that piece of the code to before. –  Peter Grill Jan 28 '12 at 8:35
@PeterGrill - Thanks for the advice but, while it does solve the problem of plotting the vectors correctly on the axes, it does not align the lattice origin at the origin of the axes, unless I move the transformation as the last statement, in which case, the lattice becomes an orthonormal one (that is, the independent vectors are at right angles to each other, and their length is equal to 1). Any other suggestions would be appreciated. –  Bill Jan 28 '12 at 9:34
@PeterGrill - Problem solved! It turns out that the culprit was the transformation. It moved the origin (a.k.a. \pgfpoint) at (3cm,3cm) from the origin. With reference to the above code, setting it to (0cm,0cm), as well as moving the variable definitions for (Bone) and (Btwo) after the transformation solved the problem. Once again, thank you for your help. If you like, you may post this as the solution to this question. –  Bill Jan 28 '12 at 9:46
Or you can write \filldraw[fill=gray, fill opacity=0.3, draw=black] (2,2) rectangle (4,4); . –  percusse Jan 28 '12 at 11:42
@percusse - Thanks for that. I should have realised that you had moved the origin of the graph in your example relating to the first question I posted. Either way, I'd like to thank you also for your support. –  Bill Jan 28 '12 at 12:34

The reason for the scope environment in the previous version was to keep the transformations local. So you can define your nodes in the scope and refer to them out of that scope later:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{figure}[h!]
\centering
\begin{tikzpicture}[>=latex]
\begin{scope}
\clip (0,0) rectangle (10cm,10cm); % Clips the picture...
\pgftransformcm{1}{0.2}{0.7}{1.5}{\pgfpoint{3cm}{3cm}} % This is actually the transformation
%  matrix entries that gives the slanted
% unit vectors. You might check it on
% MATLAB etc. . I got it by guessing.

\draw[style=help lines,dashed] (-14,-14) grid[step=1.5cm] (14,14); % Draws a grid in the new coordinates.
\filldraw[fill=gray, draw=black] (1.5,1.5) --  (3,3) -- (4.5,3) -- (3,1.5) -- cycle; % Puts the shaded rectangle
\foreach \x in {-7,-6,...,7}{                           % Two indices running over each
\foreach \y in {-7,-6,...,7}{                       % node on the grid we have drawn
\node[draw,circle,inner sep=2pt,fill] at (1.5*\x,1.5*\y) {}; % Places a dot at those points
}
}
\draw[ultra thick,red,->] (1.5,1.5) -- (3,3)  node [above ] {$2b_1+b_2$};
\draw[ultra thick,red,->] (1.5,1.5) -- (3,1.5) node [below right] {$b_1+b_2$};
\draw[ultra thick,red,->] (1.5,1.5) -- (1.5,3) node [left] {$b_1$};
\draw[ultra thick,red,->] (1.5,1.5) -- (3,0) node [below right] {$b_2$};
% We can define some nodes in the transformed coord. for later
\node (O) at (0,0) {};
\end{scope}
% Back to original coordinates, we still know where (O) is!
\draw[->,thick] (O) -- ++(0,6);
\draw[->,thick] (O) -- ++(6,0);
\end{tikzpicture}
\caption{Babai's algorithm works poorly if the basis is bad''.}

I think I forgot to use the opacity settings. Sorry for that. –  percusse Jan 28 '12 at 14:22