在下图中,我有一个等边三角形的三个视图,这些视图由更小的全等等边三角形分割而成。我能否提出一个建议来\foreach
减少重复编码?
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
%The region enclosed by an equilateral triangle is partitioned by nine congruent equilateral
%triangles. The centroid of each of the nine congruent triangles is labeled P_i for some
%integer 1 \leq i \leq 9. Some of them are shaded.
\coordinate (P_1) at (1/2,{1/(2*sqrt(3))});
\coordinate (P_2) at (1,{1/sqrt(3)});
\coordinate (P_3) at (3/2,{1/(2*sqrt(3))});
\coordinate (P_4) at (2,{1/sqrt(3)});
\coordinate (P_5) at (5/2,{1/(2*sqrt(3))});
%
\coordinate (P_6) at (1,{4/(2*sqrt(3))});
\coordinate (P_7) at (3/2,{5/(2*sqrt(3))});
\coordinate (P_8) at (2,{4/(2*sqrt(3))});
%
\coordinate (P_9) at (3/2,{7/(2*sqrt(3))});
%
\draw[fill=gray!50] ($(P_1) +(210:{1/sqrt(3)})$) -- ($(P_1) +(330:{1/sqrt(3)})$) -- ($(P_1) +(90:{1/sqrt(3)})$) -- cycle;
\draw ($(P_2) +(30:{1/sqrt(3)})$) -- ($(P_2) +(150:{1/sqrt(3)})$) -- ($(P_2) +(270:{1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(P_3) +(210:{1/sqrt(3)})$) -- ($(P_3) +(330:{1/sqrt(3)})$) -- ($(P_3) +(90:{1/sqrt(3)})$) -- cycle;
\draw ($(P_4) +(30:{1/sqrt(3)})$) -- ($(P_4) +(150:{1/sqrt(3)})$) -- ($(P_4) +(270:{1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(P_5) +(210:{1/sqrt(3)})$) -- ($(P_5) +(330:{1/sqrt(3)})$) -- ($(P_5) +(90:{1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(P_6) +(210:{1/sqrt(3)})$) -- ($(P_6) +(330:{1/sqrt(3)})$) -- ($(P_6) +(90:{1/sqrt(3)})$) -- cycle;
\draw ($(P_7) +(30:{1/sqrt(3)})$) -- ($(P_7) +(150:{1/sqrt(3)})$) -- ($(P_7) +(270:{1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(P_8) +(210:{1/sqrt(3)})$) -- ($(P_8) +(330:{1/sqrt(3)})$) -- ($(P_8) +(90:{1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(P_9) +(210:{1/sqrt(3)})$) -- ($(P_9) +(330:{1/sqrt(3)})$) -- ($(P_9) +(90:{1/sqrt(3)})$) -- cycle;
%The region enclosed by another equilateral triangle is partitioned by sixteen congruent
%equilateral triangles. The centroid of each of the sixteen congruent triangles is
%labeled Q_i for some integer 1 \leq i \leq 16. Some of them are shaded.
\coordinate (Q_1) at ($(5,0) +({(3/4)*1/2},{(3/4)*1/(2*sqrt(3))})$);
\coordinate (Q_2) at ($(5,0) +({(3/4)*1},{(3/4)*1/sqrt(3)})$);
\coordinate (Q_3) at ($(5,0) +({(3/4)*3/2},{(3/4)*1/(2*sqrt(3))})$);
\coordinate (Q_4) at ($(5,0) +({(3/4)*2},{(3/4)*1/sqrt(3)})$);
\coordinate (Q_5) at ($(5,0) +({(3/4)*5/2},{(3/4)*1/(2*sqrt(3))})$);
\coordinate (Q_6) at ($(5,0) +({(3/4)*3},{(3/4)*1/sqrt(3)})$);
\coordinate (Q_7) at ($(5,0) +({(3/4)*7/2},{(3/4)*1/(2*sqrt(3))})$);
%
\coordinate (Q_8) at ($(5,0) +({(3/4)*1},{(3/4)*4/(2*sqrt(3))})$);
\coordinate (Q_9) at ($(5,0) +({(3/4)*3/2},{(3/4)*5/(2*sqrt(3))})$);
\coordinate (Q_10) at ($(5,0) +({(3/4)*2},{(3/4)*4/(2*sqrt(3))})$);
\coordinate (Q_11) at ($(5,0) +({(3/4)*5/2},{(3/4)*5/(2*sqrt(3))})$);
\coordinate (Q_12) at ($(5,0) +({(3/4)*3},{(3/4)*4/(2*sqrt(3))})$);
%
\coordinate (Q_13) at ($(5,0) +({(3/4)*3/2},{(3/4)*7/(2*sqrt(3))})$);
\coordinate (Q_14) at ($(5,0) +({(3/4)*2},{(3/4)*8/(2*sqrt(3))})$);
\coordinate (Q_15) at ($(5,0) +({(3/4)*5/2},{(3/4)*7/(2*sqrt(3))})$);
%
\coordinate (Q_16) at ($(5,0) +({(3/4)*2},{(3/4)*10/(2*sqrt(3))})$);
\draw[fill=gray!50] ($(Q_1) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_1) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_1) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw ($(Q_2) +(30:{(3/4)*1/sqrt(3)})$) -- ($(Q_2) +(150:{(3/4)*1/sqrt(3)})$) -- ($(Q_2) +(270:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(Q_3) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_3) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_3) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw ($(Q_4) +(30:{(3/4)*1/sqrt(3)})$) -- ($(Q_4) +(150:{(3/4)*1/sqrt(3)})$) -- ($(Q_4) +(270:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(Q_5) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_5) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_5) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw ($(Q_6) +(30:{(3/4)*1/sqrt(3)})$) -- ($(Q_6) +(150:{(3/4)*1/sqrt(3)})$) -- ($(Q_6) +(270:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(Q_7) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_7) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_7) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(Q_8) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_8) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_8) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw ($(Q_9) +(30:{(3/4)*1/sqrt(3)})$) -- ($(Q_9) +(150:{(3/4)*1/sqrt(3)})$) -- ($(Q_9) +(270:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(Q_10) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_10) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_10) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw ($(Q_11) +(30:{(3/4)*1/sqrt(3)})$) -- ($(Q_11) +(150:{(3/4)*1/sqrt(3)})$) -- ($(Q_11) +(270:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(Q_12) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_12) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_12) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(Q_13) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_13) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_13) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw ($(Q_14) +(30:{(3/4)*1/sqrt(3)})$) -- ($(Q_14) +(150:{(3/4)*1/sqrt(3)})$) -- ($(Q_14) +(270:{(3/4)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(Q_15) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_15) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_15) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(Q_16) +(210:{(3/4)*1/sqrt(3)})$) -- ($(Q_16) +(330:{(3/4)*1/sqrt(3)})$) -- ($(Q_16) +(90:{(3/4)*1/sqrt(3)})$) -- cycle;
%The region enclosed by a third equilateral triangle is partitioned by twenty-five congruent
%equilateral triangles. The centroid of each of the twenty-five congruent triangles is
%labeled R_i for some integer 1 \leq i \leq 25. Some of them are shaded.
\coordinate (R_1) at ($(10,0) +({(3/5)*1/2},{(3/5)*1/(2*sqrt(3))})$);
\coordinate (R_2) at ($(10,0) +({(3/5)*1},{(3/5)*1/sqrt(3)})$);
\coordinate (R_3) at ($(10,0) +({(3/5)*3/2},{(3/5)*1/(2*sqrt(3))})$);
\coordinate (R_4) at ($(10,0) +({(3/5)*2},{(3/5)*1/sqrt(3)})$);
\coordinate (R_5) at ($(10,0) +({(3/5)*5/2},{(3/5)*1/(2*sqrt(3))})$);
\coordinate (R_6) at ($(10,0) +({(3/5)*3},{(3/5)*1/sqrt(3)})$);
\coordinate (R_7) at ($(10,0) +({(3/5)*7/2},{(3/5)*1/(2*sqrt(3))})$);
\coordinate (R_8) at ($(10,0) +({(3/5)*4},{(3/5)*1/sqrt(3)})$);
\coordinate (R_9) at ($(10,0) +({(3/5)*9/2},{(3/5)*1/(2*sqrt(3))})$);
%
\coordinate (R_10) at ($(10,0) +({(3/5)*1},{(3/5)*4/(2*sqrt(3))})$);
\coordinate (R_11) at ($(10,0) +({(3/5)*3/2},{(3/5)*5/(2*sqrt(3))})$);
\coordinate (R_12) at ($(10,0) +({(3/5)*2},{(3/5)*4/(2*sqrt(3))})$);
\coordinate (R_13) at ($(10,0) +({(3/5)*5/2},{(3/5)*5/(2*sqrt(3))})$);
\coordinate (R_14) at ($(10,0) +({(3/5)*3},{(3/5)*4/(2*sqrt(3))})$);
\coordinate (R_15) at ($(10,0) +({(3/5)*7/2},{(3/5)*5/(2*sqrt(3))})$);
\coordinate (R_16) at ($(10,0) +({(3/5)*4},{(3/5)*4/(2*sqrt(3))})$);
%
\coordinate (R_17) at ($(10,0) +({(3/5)*3/2},{(3/5)*7/(2*sqrt(3))})$);
\coordinate (R_18) at ($(10,0) +({(3/5)*2},{(3/5)*8/(2*sqrt(3))})$);
\coordinate (R_19) at ($(10,0) +({(3/5)*5/2},{(3/5)*7/(2*sqrt(3))})$);
\coordinate (R_20) at ($(10,0) +({(3/5)*3},{(3/5)*8/(2*sqrt(3))})$);
\coordinate (R_21) at ($(10,0) +({(3/5)*7/2},{(3/5)*7/(2*sqrt(3))})$);
%
\coordinate (R_22) at ($(10,0) +({(3/5)*2},{(3/5)*10/(2*sqrt(3))})$);
\coordinate (R_23) at ($(10,0) +({(3/5)*5/2},{(3/5)*11/(2*sqrt(3))})$);
\coordinate (R_24) at ($(10,0) +({(3/5)*3},{(3/5)*10/(2*sqrt(3))})$);
%
\coordinate (R_25) at ($(10,0) +({(3/5)*5/2},{(3/5)*13/(2*sqrt(3))})$);
\draw[fill=gray!50] ($(R_1) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_1) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_1) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_2) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_2) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_2) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_3) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_3) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_3) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_4) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_4) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_4) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_5) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_5) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_5) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_6) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_6) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_6) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_7) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_7) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_7) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_8) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_8) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_8) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_9) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_9) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_9) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(R_10) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_10) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_10) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_11) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_11) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_11) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_12) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_12) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_12) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_13) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_13) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_13) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_14) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_14) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_14) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_15) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_15) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_15) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_16) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_16) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_16) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(R_17) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_17) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_17) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_18) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_18) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_18) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_19) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_19) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_19) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_20) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_20) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_20) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_21) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_21) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_21) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(R_22) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_22) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_22) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw ($(R_23) +(30:{(3/5)*1/sqrt(3)})$) -- ($(R_23) +(150:{(3/5)*1/sqrt(3)})$) -- ($(R_23) +(270:{(3/5)*1/sqrt(3)})$) -- cycle;
\draw[fill=gray!50] ($(R_24) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_24) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_24) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
%
\draw[fill=gray!50] ($(R_25) +(210:{(3/5)*1/sqrt(3)})$) -- ($(R_25) +(330:{(3/5)*1/sqrt(3)})$) -- ($(R_25) +(90:{(3/5)*1/sqrt(3)})$) -- cycle;
\end{tikzpicture}
\end{document}
答案1
既然用单个命令直接绘制它这么短,为什么要使用循环呢?
\documentclass[border=7pt]{standalone}
\usepackage{tikz}
\begin{document}
\tikz\draw[fill=lightgray,even odd rule,xscale=2/sqrt(3),xslant=.5]
(3,0)-|(0,3)--cycle (0,2)-|(1,0)--(0,1)-|(2,0)--cycle;
\tikz\draw[fill=lightgray,even odd rule,xscale=2/sqrt(3),xslant=.5]
(4,0)-|(0,4)--cycle (0,3)-|(1,0)--(0,1)-|(3,0)--cycle (0,2)-|(2,0)--cycle;
\tikz\draw[fill=lightgray,even odd rule,xscale=2/sqrt(3),xslant=.5]
(5,0)-|(0,5)--cycle (0,4)-|(1,0)--(0,1)-|(4,0)--cycle (0,3)-|(2,0)--(0,2)-|(3,0)--cycle;
\end{document}
答案2
如果我正确阅读了代码,那么一个选项
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[mytri/.style={regular polygon, regular polygon sides=3,
minimum width={2cm/sqrt(3)}, inner sep=0, outer sep=0}]
\foreach\y in {1,...,4}{
\foreach \x in {\y,...,1}{
\node[mytri,draw,fill=gray!50,anchor=corner 3] (a) at ([shift={(60:-1*(\y-1))}]\x-1,0){};
}
}
\end{tikzpicture}
\end{document}
答案3
只是为了好玩:各用一条路径进行绘图和着色。
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\newcommand{\InsertCoordinates}[2]{\foreach \X in {1,...,#1}{
coordinate[pos={\X/(#1+1)}] (#2\X) }
}
\begin{document}
\begin{tikzpicture}
\begin{scope}[yshift=-4cm]
\begin{scope}[xshift=2cm]
\draw[fill=gray!50,even odd rule] (-30:2) -- (90:2) \InsertCoordinates{2}{x}
-- (210:2) \InsertCoordinates{2}{y}
-- (-30:2) \InsertCoordinates{2}{z}
-- (x1) -- (z2) -- (y1) -- (x2) -- (z1) -- (y2) -- (x1);
\end{scope}
\begin{scope}[xshift=7cm]
\draw[fill=gray!50,even odd rule]
(-30:2) -- (90:2) \InsertCoordinates{3}{x}
-- (210:2) \InsertCoordinates{3}{y}
-- (-30:2) \InsertCoordinates{3}{z}
-- (x1) -- (z3) -- (y1) -- (x3) -- (z1) -- (y3) -- (x1) -- (x2) -- (y2)
--(z2) -- (x2);
\end{scope}
\begin{scope}[xshift=12cm]
\draw[fill=gray!50,even odd rule]
(-30:2) -- (90:2) \InsertCoordinates{4}{x}
-- (210:2) \InsertCoordinates{4}{y}
-- (-30:2) \InsertCoordinates{4}{z}
-- (x1) -- (y4) -- (z1) -- (x4) -- (y1) -- (z4) -- (x1) -- (x2)
-- (y3) -- (z2) -- (x3) -- (y2) -- (z3) -- (x2)
;
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}