我需要为棋盘游戏创建一些类似这样的图像:
在 TikZ 中,我可以绘制一个六边形并用从位图中剪切出的六边形区域填充它吗?
[如果有人问为什么是 TeX:每个实际的十六进制上都会印有许多符号(取自位图)、方框、文本、彩色圆圈等。这些都很容易进行大量调整——一个简单的例子可能是在所有十六进制中系统地替换符号。我想使用脚本语言而不是 Photoshop 之类的软件,以阻止十六进制的再生(和新十六进制的创建)变得非常缓慢。我知道 TeX 不是为此而设计的,但我不知道还有更好的选择。]
答案1
TikZ 解决方案。我尝试使代码模块化到足以满足您的“广泛可调”要求,以便您可以稍微更改初始宏定义,并将新设置应用于所有六边形。
\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}
结果:
答案2
\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}
答案3
与其他答案非常相似(但需要 pgf 3.0.0 作为node contents
密钥)...
\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}
答案4
运行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}