我有一个需要在 LaTeX 中绘制六边形网格的情况。我正在考虑从 eps 文件中读取网格。
有没有其他方法可以在 LaTeX 中完全完成此操作?
答案1
这是一个快速选项:
\begin{tikzpicture}
\foreach \i in {0,...,3}
\foreach \j in {0,...,3} {
\foreach \a in {0,120,-120} \draw (3*\i,2*sin{60}*\j) -- +(\a:1);
\foreach \a in {0,120,-120} \draw (3*\i+3*cos{60},2*sin{60}*\j+sin{60}) -- +(\a:1);}
\end{tikzpicture}
其结果是
答案2
使用 TikZ,您可以通过添加以下选项来定义一个允许用六边形网格填充任何形状的图案pattern=hexagons
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\def\hexagonsize{0.5cm}
\pgfdeclarepatternformonly
{hexagons}% name
{\pgfpointorigin}% lower left
{\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize}}% upper right
{\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize}}% tile size
{% shape description
\pgfsetlinewidth{0.4pt}
\pgftransformshift{\pgfpoint{0mm}{0.866025*\hexagonsize}}
\pgfpathmoveto{\pgfpoint{0mm}{0mm}}
\pgfpathlineto{\pgfpoint{0.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{\hexagonsize}{-0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2*\hexagonsize}{-0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{3*\hexagonsize+0.2mm}{0mm}}
\pgfpathmoveto{\pgfpoint{0.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{\hexagonsize}{0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2*\hexagonsize}{0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
\pgfusepath{stroke}
}
\begin{document}
\begin{tikzpicture}
\fill[pattern=hexagons] (0,0) rectangle (10,5);
\end{tikzpicture}
\begin{tikzpicture}
\fill[pattern=hexagons] (0,0) circle (3cm);
\end{tikzpicture}
\end{document}
您可以通过修改宏的值来改变六边形的大小\hexagonsize
。
答案3
另一种方法是在调整后的坐标系上绘制六边形节点。这个想法是适应保罗·加博利的帕斯卡三角形为了我怎样才能绘制帕斯卡三角形及其一些属性?。
shapes.geometric
库有助于绘制六边形,其中minimum size
是外接圆的直径。因此,选择x
(x=1.5*{minimum size}
)和y
(y=\sqrt{.75}*{minimum size}/2
)的调整值,可以绘制六边形网格,并在每对 的中心放置一个节点(x,y)
。
\documentclass[border=2mm, tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
%
% x=3*(minimum size)/2
% x=\sqrt{3/4}*(minimum size)/2
%
\begin{tikzpicture}[x=7.5mm,y=4.34mm]
% some styles
\tikzset{
box/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm,
outer sep=0mm,
rotate=0,
draw
}
}
\foreach \i in {0,...,5}
\foreach \j in {0,...,5} {
\node[box] at (2*\i,2*\j) {};
\node[box] at (2*\i+1,2*\j+1) {};
}
\end{tikzpicture}
\end{document}
答案4
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc,patterns}
\newlength{\dodamnet}
\newcommand{\bankinh}{}
\newcommand{\maumot}{}
\newcommand{\mauhai}{}
\newcommand{\mauba}{}
\newcommand{\maubon}{}
\tikzset{
do dam net/.code={\setlength{\dodamnet}{#1}},
ban kinh/.code={\renewcommand{\bankinh}{#1}},
mau mot/.code={\renewcommand{\maumot}{#1}},
mau hai/.code={\renewcommand{\mauhai}{#1}},
mau ba/.code={\renewcommand{\mauba}{#1}},
mau bon/.code={\renewcommand{\maubon}{#1}}
}
\tikzset{
do dam net=0.5pt,
mau mot=red,
mau hai=blue,
mau ba=pink,
mau bon=yellow,
ban kinh =1
}
\begin{document}
\pgfdeclarepatternformonly[\bankinh,\maumot,\mauhai,\mauba,\maubon,\dodamnet]{luc giac mau}
{\pgfmathsetmacro{\x}{1.5* \bankinh}
\pgfmathsetmacro{\y}{\bankinh *sqrt(3)}
\pgfqpoint{-\x mm}{-\y mm}}%Dưới trái
{\pgfmathsetmacro{\x}{1.5* \bankinh}
\pgfmathsetmacro{\y}{\bankinh *sqrt(3)}
\pgfqpoint{\x mm}{\y mm}}%Trên phải
{\pgfmathsetmacro{\x}{3* \bankinh}
\pgfmathsetmacro{\y}{2*\bankinh *sqrt(3)}
\pgfqpoint{\x mm}{\y mm}}
%======================
{\pgfsetlinewidth{\dodamnet}
\pgfmathsetmacro{\nuabk}{\bankinh/2}
\pgfmathsetmacro{\cao}{\nuabk *sqrt(3)}
\pgfmathsetmacro{\y}{2*\cao}
\pgfmathsetmacro{\x}{1.5* \bankinh}
\pgfsetfillcolor{\maumot}
\pgfpathmoveto{\pgfqpoint{-\nuabk mm}{-\cao mm}}
\pgfpathlineto{\pgfqpoint{-\bankinh mm}{0 mm}}
\pgfpathlineto{\pgfqpoint{-\nuabk mm}{\cao mm}}
\pgfpathlineto{\pgfqpoint{\nuabk mm}{\cao mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{0 mm}}
\pgfpathlineto{\pgfqpoint{\nuabk mm}{-\cao mm}}
\pgfpathlineto{\pgfqpoint{-\nuabk mm}{-\cao mm}}
\pgfusepath{fill}
\pgfsetfillcolor{\mauhai}
\pgfpathmoveto{\pgfqpoint{-\x mm}{-\y mm}}
\pgfpathlineto{\pgfqpoint{-\bankinh mm}{-\y mm}}
\pgfpathlineto{\pgfqpoint{-\nuabk mm}{-\cao mm}}
\pgfpathlineto{\pgfqpoint{-\bankinh mm}{0 mm}}
\pgfpathlineto{\pgfqpoint{-\x mm}{0 mm}}
\pgfpathmoveto{\pgfqpoint{\x mm}{-\y mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{-\y mm}}
\pgfpathlineto{\pgfqpoint{\nuabk mm}{-\cao mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{0 mm}}
\pgfpathlineto{\pgfqpoint{\x mm}{0 mm}}
\pgfusepath{fill}
\pgfsetfillcolor{\mauba}
\pgfpathmoveto{\pgfqpoint{-\x mm}{\y mm}}
\pgfpathlineto{\pgfqpoint{-\bankinh mm}{\y mm}}
\pgfpathlineto{\pgfqpoint{-\nuabk mm}{\cao mm}}
\pgfpathlineto{\pgfqpoint{-\bankinh mm}{0 mm}}
\pgfpathlineto{\pgfqpoint{-\x mm}{0 mm}}
\pgfpathmoveto{\pgfqpoint{\x mm}{\y mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{\y mm}}
\pgfpathlineto{\pgfqpoint{\nuabk mm}{\cao mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{0 mm}}
\pgfpathlineto{\pgfqpoint{\x mm}{0 mm}}
\pgfusepath{fill}
\pgfsetfillcolor{\maubon}
\pgfpathmoveto{\pgfqpoint{-\bankinh mm}{\y mm}}
\pgfpathlineto{\pgfqpoint{-\nuabk mm}{\cao mm}}
\pgfpathlineto{\pgfqpoint{\nuabk mm}{\cao mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{\y mm}}
\pgfpathmoveto{\pgfqpoint{-\bankinh mm}{-\y mm}}
\pgfpathlineto{\pgfqpoint{-\nuabk mm}{-\cao mm}}
\pgfpathlineto{\pgfqpoint{\nuabk mm}{-\cao mm}}
\pgfpathlineto{\pgfqpoint{\bankinh mm}{-\y mm}}
\pgfusepath{fill}
}
\begin{tikzpicture}
\draw[pattern=luc giac mau] (0,0) rectangle (4,1);
\draw[pattern=luc giac mau,mau mot=gray,mau hai=purple,mau ba=orange,mau bon=pink] (0,2) rectangle (4,3);
\end{tikzpicture}
\end{document}