# How to draw triangular grid in TikZ?

Is there an easy way to draw a triangular grid in TikZ, like this?

-
Just a pointer. Sorry for not offering real code. I'd draw it just like there were no grid. Use foreach to draw the three sets of lines. –  Leo Liu Aug 18 '10 at 12:54

Like Leo said: use \foreach and some math:

\usetikzlibrary{calc}

\newcommand*\rows{10}
\begin{tikzpicture}
\foreach \row in {0, 1, ...,\rows} {
\draw ($\row*(0.5, {0.5*sqrt(3)})$) -- ($(\rows,0)+\row*(-0.5, {0.5*sqrt(3)})$);
\draw ($\row*(1, 0)$) -- ($(\rows/2,{\rows/2*sqrt(3)})+\row*(0.5,{-0.5*sqrt(3)})$);
\draw ($\row*(1, 0)$) -- ($(0,0)+\row*(0.5,{0.5*sqrt(3)})$);
}
\end{tikzpicture}

-
If I'm not mistaken, for an equilateral triangle, those should all be sqrt(3), not sqrt(2). (Though maybe you didn't want an equilateral one, in which case, ignore me.) –  Ben Alpert Aug 19 '10 at 4:28
Well, that is rather embarrassing. Don't tell anyone! ;) –  Caramdir Aug 19 '10 at 8:39

A funny solution (have you ever used lindenmayersystems library?):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelindenmayersystem{triangular grid}{\rule{F->F-F+++F--F}}
\path[draw=black,
l-system={triangular grid,step=1cm,
angle=-60,axiom=F--F--F,order=4,
}]
lindenmayer system -- cycle;
\end{tikzpicture}
\end{document}

-
Wow, that's pretty clever! –  Jake Jun 26 '12 at 0:25
Great example of lateral thinking -- congratulations! –  Brent.Longborough Jun 26 '12 at 10:30
The question is not "have you ever used lindenmayersystems library", but "did you even know about it's existence". Very nice! –  Tom Bombadil Jun 26 '12 at 10:42
@TomBombadil How to use TikZ without reading the manual? How to read pgfmanual without reading all chapters? ;-) –  Paul Gaborit Jun 26 '12 at 22:10

A slightly different solution using a matrix transformation and clipping:

\newcommand*{\rows}{10}
\pgfmathsetmacro{\xcoord}{cos(60)}
\pgfmathsetmacro{\ycoord}{sin(60)}

\begin{tikzpicture}
\pgftransformcm{1}{0}{\xcoord}{\ycoord}{\pgfpointorigin}

\path[clip,preaction = {draw=black}] (\rows,0) -- (0,0) -- (0,\rows) -- cycle;
\draw (0,0) grid (\rows,\rows);
\foreach \x in {1,2,...,\rows} {
\draw (0,\x) -- (\x,0);
}
\end{tikzpicture}

-
Very beautiful! –  Paul Gaborit Jun 26 '12 at 0:28
Nice. (nitpick: the last (\rows,0) can be cycle :P) –  percusse Jun 26 '12 at 9:44

Probably not the most elegant, but slightly simplified (from my perspective) variant of Caramdir's answer.

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.misc}

\begin{document}

\newcommand*\gridsize{10}

\begin{figure}[!htb]
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\tikzstyle{every node}=[draw,thin]

\foreach \a in {1,...,\gridsize}{
\draw[dotted] (0, 0) -- (\a, 0) -- ({cos(60)*\a}, {sin(60)*\a}) -- cycle;
\draw[dashed] ({\gridsize-\a}, 0) -- (\gridsize, 0) -- ({\gridsize-cos(60)*(\a)}, {sin(60)*(\a)}) -- cycle;
\draw[red] ({cos(60)*(\gridsize-\a)}, {sin(60)*(\gridsize-\a)}) -- ({(\gridsize/2)+cos(60)*\a}, {sin(60)*(\gridsize-\a)}) -- ({cos(60)*\gridsize}, {sin(60)*\gridsize}) -- cycle;
}
\end{tikzpicture}
}

\caption{Triangular grid}
\end{figure}

\end{document}


Charles Staats contributed to this solution in Drawing scaled triangles with their bottom left corner at the same coordinates in TikZ as I became fixated on using polygons.

-