Take the 2-minute tour ×
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

8 Answers 8

up vote 26 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
3  
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
8  
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
6  
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
1  
Nice. (nitpick: the last (\rows,0) can be cycle :P) –  percusse Jun 26 '12 at 9:44
    
Very nice (and very convenient when one needs to draw something on triangular grid). –  Grigory M Dec 8 '14 at 15:43

A MetaPost version, as a complement. It is based on a recursion inspired by Paul Gaborit's use of the lindenmayersystems library. I borrowed much code from my answer to a related question.

Note that at each recursion only the midpoints triangle is drawn, in order not to draw the same segments more than once (which would result in a much thicker rendering of these segments).The main, biggest triangle being drawn only once, before the recursion takes place.

To be run with LuaLaTeX.

\documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
vardef triangular_grid(expr A, B, C, n) = % Recursive macro à la MetaPost
   if n>0:
      save midAC, midBC, midAB; pair midAC, midBC, midAB;
      midAC = .5[A, C]; midBC = .5[B, C]; midAB = .5[A, B];
      triangular_grid(A, midAB, midAC, n-1);
      triangular_grid(midAB, B, midBC, n-1);
      triangular_grid(midBC, midAC, midAB, n-1);
      triangular_grid(midAC, midBC, C, n-1);
      draw midAB--midAC--midBC--cycle;
   fi;
enddef;
beginfig(1);
   u := 14cm; pair A, B, C; A = origin; B =  (u, 0); C = u*dir 60;
   draw A--B--C--cycle; 
   triangular_grid(A, B, C, 4);
endfig;
\end{mplibcode}  
\end{document}

enter image description here

share|improve this answer

You could abuse the pgfplots tenary plot feature for this. On the plus side it allows you to easily plot data in that grid.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{ternary}
\begin{document}
\begin{tikzpicture}
  \begin{ternaryaxis}[
    xticklabels={},
    yticklabels={},
    zticklabels={},
    major tick length=0,
    minor tick length=0,
    minor tick num=2,
    every axis grid/.style={},
    grid=both,
    ]
  \end{ternaryaxis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Next code defines command \grid which draws a triangular grid made with triangular nodes. \grid's parameter is the number of rows. minimum size in tri/.style is the diameter of triangle's circumscribed circle.

With nodes instead of just lines, it's easier to use the grid as base for nice TiKZ drawings.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes}

\newcommand{\grid}[1]{
\foreach \i [count=\row from 0, remember=\row as \lastrow (initially 0)] in {0,...,#1}{
    \foreach \j [count=\column from 0, remember=\column as \lastcolumn (initially 0)] in {0,...,\i}{
        \ifnum\row=0
            \node[tri](0-0){0-0};
        \else
            \ifnum\column=0
                \node[tri, anchor=north](\row-0) at (\lastrow-0.corner 2) {\row-0};
            \else
                \node[tri, anchor=north](\row-\column) at (\lastrow-\lastcolumn.corner 3) {\row-\column};
            \fi
        \fi}}
}

\begin{document}
\begin{tikzpicture}[tri/.style={draw=gray, regular polygon, regular polygon sides=3, 
                            minimum size=2cm, inner sep=0pt, outer sep=0pt}]

\grid{5}

\begin{scope}[draw=yellow!30!black, very thick, fill=yellow!80!black]
\filldraw[fill opacity=.7] (5-2.corner 2)--(4-0.corner 1)--(4-1.corner 1)--
         (2-1.corner 1)--(3-2.corner 3)--(4-4.corner 1)--(5-4.corner 2)--cycle;

\draw (4-1.corner 1) -- (4-3.corner 1) (4-2.corner 1)--(2-1.corner 1);
\end{scope}


\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

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.

share|improve this answer

At risk of starting a "code golf" war, but in the spirit of "TIMTOWTDI",

\documentclass{standalone}\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[yscale=sqrt(.75),xslant=.5]
\def\n{12}
\clip[preaction=draw](0,0)--(\n,0)--(0,\n)--cycle;
\draw(0,0)grid(\n,\n)[xslant=-1](0,0)grid(\n,\n);
\end{tikzpicture}
\end{document}

If you want to avoid double-drawn grid lines, then line 4 is

\draw(0,0)grid(\n,\n)[xslant=-1](0,0)grid[ystep=\n](\n,\n);

Notes:

  • No point including a picture here, as it looks like those above.
  • Yes, this is similar to answer contributed by @finite
share|improve this answer

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.