在边长为 的正方形中5*sqrt(2)
,我画了 45 个边长为 的全等正方形sqrt(2)
。我想给其中 25 个涂上阴影 - 每行 5 个。我给底行的 5 个正方形涂上阴影,然后开始给其他行涂上阴影。如何使用 来做到这一点\foreach
?
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
%A square with edge length 5*sqrt(2) is drawn. With ten line segments of slope 1 and
%ten line segments of slope -1, 45 squares with edge length sqrt(2), sixteen isosceles
%right triangles with legs of length sqrt(2), and four isosceles right triangles with
%legs of length sqrt(2)/2 are drawn. Twenty-five of the squares are shaded.
%(The figure is scaled by 1/4.)
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},0) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(2*sqrt(2)/2)}) -- (0,{(1/4)*sqrt(2)/2}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},0) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- cycle;
\path[fill=gray!50] ({(1/4)*(5*sqrt(2)/2)},0) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2))}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(7*sqrt(2)/2)},0) -- ({(1/4)*(8*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(7*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(9*sqrt(2)/2)},0) -- ({(1/4)*(10*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(9*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(8*sqrt(2)/2)},{(1/4)*(sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(4*sqrt(2)/2)}) -- (0,{(1/4)*(3*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(7*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(8*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*(7*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(6*sqrt(2)/2)}) -- (0,{(1/4)*(5*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(7*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(8*sqrt(2)/2)}) -- (0,{(1/4)*(7*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(7*sqrt(2)/2)}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(8*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(7*sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(8*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(9*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(10*sqrt(2)/2)}) -- (0,{(1/4)*(9*sqrt(2)/2)}) -- cycle;
%
%
\draw (0,0) -- ({(1/4)*(5*sqrt(2))},0) -- ({(1/4)*(5*sqrt(2))},{(1/4)*(5*sqrt(2))}) -- (0,{(1/4)*(5*sqrt(2))}) -- cycle;
%
%
\foreach \x in {1,2,3,4,5} {\draw (0,{(1/4)*(2*\x - 1)*(sqrt(2)/2)}) -- ({(1/4)*(5*sqrt(2) - (2*\x - 1)*(sqrt(2)/2))},{(1/4)*5*sqrt(2)});}
\foreach \x in {1,2,3,4,5} {\draw ({(1/4)*(2*\x - 1)*(sqrt(2)/2)},0) -- ({(1/4)*5*sqrt(2)},{(1/4)*5*sqrt(2) - (1/4)*(2*\x - 1)*(sqrt(2)/2)});}
\foreach \x in {1,2,3,4,5} {\draw (0,{(1/4)*(2*\x - 1)*(sqrt(2)/2)}) -- ({(1/4)*(2*\x - 1)*(sqrt(2)/2)},0);}
\foreach \x in {1,2,3,4,5} {\draw ({(1/4)*(2*\x - 1)*(sqrt(2)/2)},{(1/4)*5*sqrt(2)}) -- ({(1/4)*5*sqrt(2)},{(1/4)*(2*\x - 1)*(sqrt(2)/2)},0);}
\end{tikzpicture}
\end{document}
答案1
这是你想要的吗?
\begin{tikzpicture}
\foreach \y in {0,2,...,8}{%
\foreach \x in {0,2,...,8}{%
\draw[fill=black!50,xshift=\x cm, yshift=\y cm]%
(0:1cm) -- (90:1cm) -- (180:1cm) -- (270:1cm) -- cycle;
}%
}%
\draw (-1,-1) rectangle (9,9);
\end{tikzpicture}
编辑:相同的视觉结果,但范围内的计数器0,1,2,3,4
很容易看出有 5 个值。
\foreach \y in {0,1,...,4}{%
\foreach \x in {0,1,...,4}{%
\draw[fill=black!50,xshift=\x cm, yshift=\y cm]
(0:.5cm) -- (90:.5cm) -- (180:.5cm) -- (270:.5cm) -- cycle;
}%
}%
\draw (-.5,-.5) rectangle (4.5,4.5);
答案2
我如何能抗拒这场高尔夫挑战呢!
\documentclass[tikz,border=7pt]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach~in{0,...,24}
\fill[shift={({mod(~,5)+.5},{div(~,5)})},gray,rotate=45]rectangle(45:1);
\draw rectangle(5,5);
\end{tikzpicture}
\end{document}
编辑:第二个代码长 2 个字符,但使用仅有 5 次迭代的单个循环。
\documentclass[tikz,border=7pt]{standalone}
\begin{document}
\begin{tikzpicture}
\fill[gray,even odd rule,rotate=45]
foreach~in{1,3,...,9}{(~-10,-~)rectangle(10-~,~)};
\draw(45:-10)rectangle(45:10);
\end{tikzpicture}
\end{document}
答案3
与@Sigur 的答案类似,只需使用您的长度约定(以防您想在相同的约定中添加一些内容)。
\documentclass{amsart}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \Y in {0,...,4}
{\foreach \X in {0,...,4}
{\draw[fill=gray!50] ({(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*(2*sqrt(2)/2)*\Y}) --
({(1/4)*(2*sqrt(2)/2)+(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\Y}) --
({(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*(2*sqrt(2)/2)+(1/4)*(2*sqrt(2)/2)*\Y}) --
({(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\Y}) -- cycle;}}
%
\draw (0,0) -- ({(1/4)*(5*sqrt(2))},0) -- ({(1/4)*(5*sqrt(2))},{(1/4)*(5*sqrt(2))}) -- (0,{(1/4)*(5*sqrt(2))}) -- cycle;
%
\end{tikzpicture}
\end{document}