21

I need to create some images like these for a board game:

enter image description here

In TikZ, can I draw a hexagon and fill it with a hexagonal area cut out of a bitmap?

[In case anyone asks why TeX: Each actual hex will have a number of symbols (taken from bitmaps), boxes, text, coloured circles, etc. printed on it. These are liable to be extensively adjusted---a simple example might be systematic replacement of a symbol across all hexes. I want to use a scripting language rather than Photoshop-like software, to stop regeneration of hexes (and creation of new hexes) being tortuously slow. I know TeX isn't meant for this, but I don't know of any better options.]

5
  • 11
    If you already have hexagonal areas, this is basically a duplicate of How to clip an external image inside an ellipse: use Kevin's answer and replace the ellipse with a polygon shape. Commented Jan 23, 2014 at 14:19
  • And then, on to tiling with them.
    – Raphael
    Commented Jan 23, 2014 at 17:23
  • @Raphael: No, cutting them out with scissors -- so tiling isn't needed.
    – Mohan
    Commented Jan 23, 2014 at 17:40
  • 1
    Aw, too bad. But wait: don't you want to save paper and cuts?
    – Raphael
    Commented Jan 23, 2014 at 18:08
  • Saving paper... good point.
    – Mohan
    Commented Jan 23, 2014 at 18:11

5 Answers 5

20

A TikZ solution. I tried to make the code modular enough to address your "extesively adjustable" requirement, so that you can change a bit the initial macro definitions, and have the new settings applied to all your hexagons.

\documentclass{article}
\usepackage{tikz,nopageno}

% General macro to draw a shape filled with a bitmap
\newcommand\fillshape[3]{ % #1 = shape, #2 = filename of texture, #3 = includegraphics options
    \begin{scope}
        \clip #1;
        \node {\includegraphics[#3]{#2}};
    \end{scope}
    \draw[line width=1mm] #1;
}

% Particularization for hexagonal shape
\newcommand\fillhexagon[2][]{% #1 (optional) = includegraphics options, #2 = filename of texture
    \fillshape{(0:1) -- (60:1) -- (120:1) -- (180:1) -- (240:1) -- (300:1) -- cycle}{#2}{#1}
}


% Using above macro, I define several hexes with "materials"
\newcommand\grass{
    \fillhexagon[scale=0.5]{grass.jpg}
}   
\newcommand\water{
    \fillhexagon[scale=0.5]{water.jpg}
}    
\newcommand\clouds{
    \fillhexagon[scale=0.5]{clouds.jpg}
}

\begin{document}
% Sample code to draw the three kind of hexagons
\begin{tikzpicture}
    \begin{scope}[xshift=0cm]\grass\end{scope}
    \begin{scope}[xshift=3cm]\water\end{scope}
    \begin{scope}[xshift=6cm]\clouds\end{scope}
\end{tikzpicture}
\end{document}

Result:

Result

9
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\newcommand{\hexclipper}[3]{%
  \begin{tikzpicture} 
    \foreach \ang in {60,120,...,360}{
     \coordinate (\ang) at  (\ang:#1) ;}
     \draw[line width=1pt,magenta] (60) -- (120) -- (180) -- (240) -- (300) -- (360) -- cycle;
     \clip (60) -- (120) -- (180) -- (240) -- (300) -- (360) -- cycle;
     \node{\includegraphics[width=#2]{{#3}}};
  \end{tikzpicture}
}
%% syntax
%%\hexclipper{<size of hexgon>}{<width of image>}{<image file>}
\begin{document}
    \hexclipper{1cm}{3cm}{example-image-a} 
    \hexclipper{1cm}{4cm}{example-image-b} 
    \hexclipper{1cm}{5cm}{example-image-c} 
\end{document}

enter image description here

8

Much the same as the other answers (but needs pgf 3.0.0 for the node contents key)...

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}
\tikzset{hexagon/.style args={#1with image#2[#3]#4(#5)}{%
  regular polygon, regular polygon sides=6, 
  path picture={\node{\includegraphics[#3]{#5}};},
  draw, ultra thick, minimum size=#1, node contents=}
}

\tikz{%
  \node at (0,0) [hexagon={2cm with image [scale=0.25] (example-image-a)}];
  \node at (3,0) [hexagon={2cm with image [scale=0.25] (example-image-b)}];
  \node at (6,0) [hexagon={2cm with image [scale=0.25] (example-image-c)}];
}
\end{document}

enter image description here

6

A solution with PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido,graphicx}

\SpecialCoor
\def\clipper#1#2#3{%
    \begin{pspicture}(-#1,-#1)(#1,#1)
        \psclip{\pscustom[linewidth=3pt]{\moveto(#1,0)\rotate{30}\psLoop{5}{\rlineto(0,#1)\rotate{60}}\closepath}}
            \rput(0,0){\includegraphics[scale=#3]{#2}}
        \endpsclip
    \end{pspicture}}

\begin{document}
    \clipper{4}{tesla}{2.1}
    \clipper{4}{barrack}{.85}
\end{document}

enter image description here

enter image description here

Notes

The original image is as follows

enter image description here

taken from this site.

6

run with xelatex

\documentclass[border=2pt]{standalone}
\usepackage{pst-poly,graphicx}
\def\hexclipper#1#2#3{%
  \begin{pspicture}(-#1,-#1)(#1,#1)
    \psclip{\PstHexagon[unit=#1,PstPicture=false]}
       \rput(0,0){\includegraphics[scale=#3]{#2}}
    \endpsclip
    \PstHexagon[unit=#1,linecolor=red,PstPicture=false]
  \end{pspicture}}

\begin{document}
  \hexclipper{2}{tiger}{0.45}
  \hexclipper{2}{example-image-a}{0.5}
  \hexclipper{2}{example-image-b}{1}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .