我花了几个小时尝试在 LaTeX 中绘制这两个格子,但毫无进展。有人能提醒我这样做的关键代码吗?非常感谢!新年快乐!
答案1
以下是 Metapost 中 (4,6,12) 的快速尝试,可能为您在 TikZ 中提供起点。它基于重复一个单位形状,如下图粉红色突出显示。
有关 Metapost 的更多详细信息,你可以先阅读以下答案这个问题。
prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(1);
u = 20;
dx = u*(1+cosd(15)/sind(15));
picture unit; unit = image(
path s; s = unitsquare shifted -(1/2,1/2) scaled u shifted (dx/2,0);
for t=0 upto 2:
draw s rotated 120t;
for tt=1 upto 3:
draw subpath(3,4) of s rotated (120t+30tt);
endfor
endfor
);
draw unit withpen pencircle scaled 4 withcolor .8[red,white];
for x=-5dx step dx until 5dx:
for y=-5dx step dx until 5dx:
draw unit shifted (x,0) shifted ((y,0) rotated 60);
endfor
endfor
path box; box = unitsquare shifted -(1/2,1/2) scaled 12u;
clip currentpicture to box; draw box dashed evenly;
endfig;
end.
答案2
以下是 (4,6,12) 的一个 TikZ 解决方案:
\documentclass[border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\tikzset{square/.style= { to path={ let \p1=(\tikztostart), \p2=(\tikztotarget),
\p3=($(\p2)!1!90:(\p1)$), \p4=($(\p1)!1!-90:(\p2)$) in
(\p4) -- (\p1) -- (\p2) -- (\p3) (\p2)}}}
\tikzmath{
function drawone(\x,\y,\size) {
{
\draw[shift={(\x,\y)}] foreach \a in {-120,0,120} {
[rotate=\a] (-15:\size) to[square] (15:\size) -- (45:\size) -- (75:\size) -- (105:\size) };
};
};
}
\begin{document}
\begin{tikzpicture}
\tikzmath{
\size = 2; \nx = 3; \ny = 4;
\xstep = (cos(15)+sin(15))*\size;
\ystep = 3*\xstep/tan(60);
for \i in {1,...,\nx}{
for \j in {1,...,\ny}{
drawone((2*\i+mod(\j,2))*\xstep,\j*\ystep,\size);
};
};
}
\begin{scope}[red,thick]
\tikzmath{drawone(2,3,\size);}
\end{scope}
\end{tikzpicture}
\end{document}
更新:以下是更简单的 (4,8 2 ) 情况:
\documentclass[border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\tikzmath{
function drawone(\x,\y,\size) {
{
\draw[shift={(\x,\y)},rotate=-22.5] (0:\size) foreach \a in {0,45,...,360}{ -- (\a:\size)};
};
};
}
\begin{document}
\begin{tikzpicture}
\tikzmath{
\size = .7; \nx = 8; \ny = 5;
\step = 2*\size*cos(22.5);
for \i in {1,...,\nx}{
for \j in {1,...,\ny}{
drawone(\i*\step,\j*\step,\size);
};
};
}
\end{tikzpicture}
\end{document}
答案3
外行人的方法(无需计算):
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{
myshape/.pic = {
\node[name=s,regular polygon, regular polygon sides=12, minimum width=1cm, draw,
outer sep=0pt] at (0,0){};
\draw (s.corner 3) -- ([shift={(120:2mm)}]s.corner 3) --
coordinate (b)([shift={(120:2mm)}]s.corner 2)
-- (s.corner 2);
\draw (s.corner 7) -- ([shift={(240:2mm)}]s.corner 7) -- ([shift={(240:2mm)}]s.corner 6)
-- (s.corner 6);
\draw (s.corner 11) -- ([shift={(0:2mm)}]s.corner 11) --
coordinate[midway] (a)([shift={(0:2mm)}]s.corner 10)
-- (s.corner 10);
}
}
\begin{document}
\begin{tikzpicture}
\pic (p) at (0,0) {myshape};
\pic[anchor=west] (q) at (pa) {myshape};
\pic[anchor=west] (r) at (qa) {myshape};
\pic[anchor=west] (s) at (ra) {myshape};
\pic[anchor=south east] (pp) at (pb) {myshape};
\pic[anchor=south east] (qq) at (qb) {myshape};
\pic[anchor=south east] (rr) at (rb) {myshape};
\pic[anchor=south east] (ss) at (sb) {myshape};
\pic[anchor=south east] (ppp) at (ppb) {myshape};
\pic[anchor=south east] (qqq) at (qqb) {myshape};
\pic[anchor=south east] (rrr) at (rrb) {myshape};
\pic[anchor=south east] (sss) at (ssb) {myshape};
\pic[anchor=west] at (sssa) {myshape};
\end{tikzpicture}
\end{document}
或者
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{
myshape/.pic = {
\node[name=s,regular polygon, regular polygon sides=12, minimum width=1cm+2.5\pgflinewidth, draw,
outer sep=0pt] at (0,0){};
\draw (s.corner 3) -- ([shift={(120:2mm)}]s.corner 3) --
coordinate (b)([shift={(120:2mm)}]s.corner 2)
-- (s.corner 2);
\draw (s.corner 7) -- ([shift={(240:2mm)}]s.corner 7) -- ([shift={(240:2mm)}]s.corner 6)
-- (s.corner 6);
\draw (s.corner 11) -- ([shift={(0:2mm)}]s.corner 11) --
coordinate[midway] (a)([shift={(0:2mm)}]s.corner 10)
-- (s.corner 10);
}
}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1.2,2.4,3.6,4.8}{
\pic (p) at (\x,0) {myshape};
}
\foreach \x in {0.6,1.8,3,4.2,5.4}{
\pic (p) at (\x,1.05cm-\pgflinewidth) {myshape};
}
\foreach \x in {0,1.2,2.4,3.6,4.8}{
\pic (p) at (\x,2.1cm-2\pgflinewidth) {myshape};
}
\foreach \x in {0.6,1.8,3,4.2,5.4}{
\pic (p) at (\x,3.15cm-3\pgflinewidth) {myshape};
}
\end{tikzpicture}
\end{document}
和
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{
myshape/.pic = {
\node[name=s,regular polygon, regular polygon sides=8, minimum width=1.1cm-\pgflinewidth, draw,
outer sep=0pt] at (0,0){};
}
}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,2,3,4}{
\foreach \y in {0,1,2,3,4}{
\pic (p) at (\x,\y) {myshape};
}
}
\end{tikzpicture}
\end{document}
答案4
另一个 TikZ 解决方案是 (4,6,12)。这次使用以正多边形装饰的倾斜网格。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{shapes.geometric}
\tikzset{
polygon/.style 2 args = { draw, regular polygon, regular polygon sides=#1,
minimum size=#2*1cm, inner sep=0pt, outer sep=0pt
},
p12/.style= {draw=red, opacity=.2,
preaction={decorate, decoration={ markings, mark=between positions 0 and .999 step 1cm
with { \node[polygon={12}{1/(sin(15)+cos(15))}]{}; }}}
},
p4/.style= { draw=blue, opacity=.2,
preaction={decorate, decoration={ markings, mark=between positions .5cm and .999 step 1cm
with { \node[transform shape, polygon={4}{sqrt(2)*sin(15)/(1+tan(15))}]{}; }}}
}
}
\begin{document}
\begin{tikzpicture}
\draw[clip] (2,1) rectangle +(3,3);
\begin{scope}[yscale=sqrt(3)/2,xslant=.5]
% demonstrate p4 and p12 decorations
\scoped[red,ultra thick,opacity=.5] \path[p12] (2,2) -- ++(1,0);
\scoped[green,ultra thick,opacity=.5] \path[p4] (2,2) -- ++(1,0);
% decorate slanted grids
\draw[p4,p12,ystep=10] (0,0) grid +(5,5);
\draw[xslant=-1,p4] (2,0) grid +(5,5);
\end{scope}
\end{tikzpicture}
\end{document}