Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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

3 Answers

up vote 20 down vote accepted

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}
share|improve this answer
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
5  
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}
share|improve this answer
Wow, that's pretty clever! – Jake Jun 26 '12 at 0:25
2  
Great example of lateral thinking -- congratulations! – Brent.Longborough Jun 26 '12 at 10:30
3  
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
1  
@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}
share|improve this answer
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.