我想要一张这样的照片。
下面代码中的 foreach 命令似乎不正确。应该怎么办?
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\r{1/6} % radius of each semicircles
\pgfmathsetmacro\a{1/2*sqrt(3)} % height of each triangles
\foreach \i in {0,1,2,3}
{\foreach \j in {0,1,2}{
\begin{scope}[shift={(\i,\j)}]
\coordinate (A) at (0,0);
\coordinate (B) at (60:1);
\coordinate (C) at (120:1);
\coordinate (K) at ($(A)!1/3!(B)$);
\coordinate (L) at ($(C)!1/3!(A)$);
\coordinate (M) at ($(B)!1/3!(C)$);
\draw (A)--(K) arc(-120:60:\r)--(B)--(M) arc(0:180:\r)--(C)--(L) arc(120:300:\r)--cycle;
\foreach \t in {A,B,C}{
\path[fill=white] (\t) circle (.1);
\draw[shift={(\t)}] (\t)--(30:.05)--(60:.1)--(90:.05) edge (\t)--(120:.1)--(150:.05) edge (\t)--(180:.1)--(210:.05) edge (\t)--(240:.1)--(270:.05) edge (\t)--(300:.1)--(330:.05) edge (\t)--(360:.1)--(30:.05);
}
\end{scope}
}}
\end{tikzpicture}
\end{document}
答案1
您需要相对移动每一行,并选择行之间的距离作为三角形的高度,即您的宏\a
(或sin(60)
)而不是1
。
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\r{1/6} % radius of each semicircles
\pgfmathsetmacro\a{1/2*sqrt(3)} % height of each triangles
\foreach \i in {0,1,2,3}
{\foreach \j in {0,1,2}
{\begin{scope}[shift={({\i+0.25*pow(-1,\j)},{\j*\a})}]
\coordinate (A) at (0,0);
\coordinate (B) at (60:1);
\coordinate (C) at (120:1);
\coordinate (K) at ($(A)!1/3!(B)$);
\coordinate (L) at ($(C)!1/3!(A)$);
\coordinate (M) at ($(B)!1/3!(C)$);
\draw (A)--(K) arc(-120:60:\r)--(B)--(M) arc(0:180:\r)--(C)--(L) arc(120:300:\r)--cycle;
\foreach \t in {A,B,C}{
\path[fill=white] (\t) circle (.1);
\draw[shift={(\t)}] (\t)--(30:.05)--(60:.1)--(90:.05) edge (\t)--(120:.1)--(150:.05) edge (\t)--(180:.1)--(210:.05) edge (\t)--(240:.1)--(270:.05) edge (\t)--(300:.1)--(330:.05) edge (\t)--(360:.1)--(30:.05);
}
\end{scope}}
}
\end{tikzpicture}
\end{document}
如果您希望更接近第一个屏幕截图,请考虑删除白色圆圈。
\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[pics/flower/.style={code={
\draw[fill=white] (0,0) plot[samples at={0,1,...,11},variable=\t]
(\t*30:{0.075+pow(-1,\t)*0.025}) -- cycle
foreach \t in {0,1,2} {(\t*60+30:0.05) edge (\t*60+210:0.05)};
}}]
\pgfmathsetmacro\r{1/6} % radius of each semicircles
\pgfmathsetmacro\a{1/2*sqrt(3)} % height of each triangles
\foreach \i in {0,1,2,3}
{\foreach \j in {0,1,2}
{\begin{scope}[shift={({\i+0.25*pow(-1,\j)},{\j*\a})}]
\coordinate (A) at (0,0);
\coordinate (B) at (60:1);
\coordinate (C) at (120:1);
\coordinate (K) at ($(A)!1/3!(B)$);
\coordinate (L) at ($(C)!1/3!(A)$);
\coordinate (M) at ($(B)!1/3!(C)$);
\draw (A)--(K) arc(-120:60:\r)--(B)--(M)
arc(0:180:\r)--(C)--(L) arc(120:300:\r)--cycle;
\foreach \X in {A,B,C}{
\path (\X) pic{flower};
}
\end{scope}}
}
\end{tikzpicture}
\end{document}
在这里我还把较小的、花形的物体放在了里面pic
。
剪辑这样的图像会产生类似于您的第一个屏幕截图的结果。
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\r{1/6} % radius of each semicircles
\pgfmathsetmacro\a{1/2*sqrt(3)} % height of each triangles
\pgfmathsetmacro\b{3*\r/\a}% radius of the corner points
\clip (-0.5,0.2) rectangle (5,5);
\foreach \j in {0,...,5}
{\ifodd\j
\edef\imax{5}
\else
\edef\imax{4}
\fi
\foreach \i in {0,...,\imax}
{\begin{scope}[shift={({\i+0.25*pow(-1,\j)},{\j*\a})}]
\draw[very thick] (30:\b) -- ++ (180:2*\r) arc(00:180:\r) -- ++ (180:2*\r)
-- ++(300:2*\r) arc(120:300:\r) -- ++(300:2*\r)
-- ++(60:2*\r) arc(240:420:\r) -- cycle;%
\foreach \X in {30,150,270}
{\draw[fill=white,shift={(\X:\b)}] (0,0) plot[samples at={0,1,...,11},variable=\t]
(\t*30:{0.1+pow(-1,\t)*0.033}) -- cycle;
\draw[shift={(\X:\b)}] foreach \Y in {0,1,2} {(\Y*60+30:0.066) --
(\Y*60+210:0.066)};}
\end{scope}}
}
\end{tikzpicture}
\end{document}
此代码已针对速度进行了一些优化。虽然可以通过叠加两个相对移位的图案来实现此目的,但我未能成功构建出一个在大多数查看器上看起来都不错的版本。