我有一个包含重复元素的 tikzpicture。现在我正在单独定义它们。但我希望有一个代码来定义一个模板三角形,我控制该三角形的尺寸(就像我在示例中通过 p1,q1 所做的那样),并使用旋转来控制方向。
(请注意,此示例仅用于说明目的。我想使用其他形状构建更复杂的图案,并希望避免使用更多库)
我的代码:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,graphicx,calc}
\begin{document}
\begin{tikzpicture}
% Gridline
\coordinate (x1) at (-4,+0);
\coordinate (x2) at (+0,-4);
\coordinate (x3) at (+4,+0);
\coordinate (x4) at (+0,+4);
\coordinate (k) at (+0,+0);
\coordinate (p1) at (+4,+0); %Triangle variable
\coordinate (q1) at (+0,+4); %Triangle variable
%---------------------------------
\coordinate (G1) at ($(x1)+(x2)$);
\coordinate (G2) at ($(x3)+(x4)$);
\draw [step=0.5cm,draw=gray] (G1) grid (G2);
%\draw [fill=yellow,opacity=0.5] ($(x1)+(x4)$)--(G2)--($(x3)+(x2)$)--(G1);
%---------------------------------
\coordinate (a1) at (k);
\coordinate (b1) at (p1);
\coordinate (c1) at (q1);
\coordinate (C1) at ($(k)$);
\coordinate (B1) at ($(C1)+(k)+(c1)$);
\coordinate (A1) at ($(B1)-(b1)$);
\draw [fill=black] (A1)--(B1)--(C1)--cycle;
\coordinate (A2) at ($(C1)$);
\coordinate (B2) at ($(A2)+(k)-(c1)$);
\coordinate (C2) at ($(B2)+(k)+(b1)$);
\draw [fill=red] (A2)--(B2)--(C2)--cycle;
\coordinate (A3) at ($(C1)$);
\coordinate (B3) at ($(A3)+(k)-(b1)$);
\coordinate (C3) at ($(B3)+(k)-(c1)$);
\draw [fill=green] (A3)--(B3)--(C3)--cycle;
\coordinate (A4) at ($(C1)$);
\coordinate (B4) at ($(A4)+(k)+(b1)$);
\coordinate (C4) at ($(B4)+(k)+(c1)$);
\draw [fill=blue] (A4)--(B4)--(C4)--cycle;
%---------------------------------
\draw[white,opacity=1] (current bounding box.south west) rectangle
(current bounding box.north east);
\end{tikzpicture}
\end{document}
第二个例子
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc,graphics}
\begin{document}
\begin{tikzpicture}
% Gridline
\coordinate (x1) at (-4,+0);
\coordinate (x2) at (+0,-4);
\coordinate (x3) at (+4,+0);
\coordinate (x4) at (+0,+4);
\coordinate (k) at (+0,+0);
\coordinate (p1) at (+1.75,+0);
\coordinate (q1) at (+0,+1.75);
\coordinate (r1) at (+4.5,+0);
\coordinate (s1) at (+0,+4.5);
%---------------------------------
\coordinate (G1) at ($(x1)+(x2)$);
\coordinate (G2) at ($(x3)+(x4)$);
\draw [step=0.5cm,draw=none] (G1) grid (G2);
\draw [fill=yellow] ($(x1)+(x4)$)--(G2)--($(x3)+(x2)$)--(G1);
%---------------------------------
\coordinate (a1) at (k);
\coordinate (b1) at (p1);
\coordinate (c1) at (q1);
\coordinate (A1) at ($(x1)+(x4)$);
\coordinate (B1) at ($(A1)+(k)+(b1)$);
\coordinate (C1) at ($(B1)+(k)-(c1)$);
\draw [fill=black] (A1)--(B1)--(C1)--cycle;
\coordinate (b2) at (r1);
\coordinate (c2) at (c1);
\coordinate (A2) at (B1);
\coordinate (B2) at ($(A2)+(k)+(b2)$);
\coordinate (C2) at ($(B2)+(a1)-(c2)$);
\coordinate (D2) at (C1);
\draw [fill=black] (A2)--(B2)--(C2)--(D2)--cycle;
\coordinate (b3) at (b1);
\coordinate (c3) at (c1);
\coordinate (A3) at (B2);
\coordinate (B3) at ($(A3)+(k)+(b3)$);
\coordinate (C3) at ($(B3)+(a1)-(c2)$);
\coordinate (D3) at (C2);
\draw [fill=black] (A3)--(B3)--(C3)--(D3)--cycle;
\coordinate (b4) at (b1);
\coordinate (c4) at (s1);
\coordinate (A4) at (C2);
\coordinate (B4) at ($(A4)+(k)+(b4)$);
\coordinate (C4) at ($(B4)+(k)-(c4)$);
\coordinate (D4) at ($(C4)+(k)-(b4)$);
\draw [fill=black] (A4)--(B4)--(C4)--(D4)--cycle;
\coordinate (A5) at (D4);
\coordinate (B5) at ($(A5)+(k)+(b1)$);
\coordinate (C5) at ($(B5)+(k)-(c1)$);
\draw [fill=black] (A5)--(B5)--(C5)--cycle;
\coordinate (A6) at (C1);
\coordinate (B6) at ($(C1)+(k)-(s1)$);
\coordinate (C6) at (A5);
\draw [fill=black] (A6)--(B6)--(C6)--cycle;
%---------------------------------
\draw[white,opacity=1] (current bounding box.south west) rectangle
(current bounding box.north east);
\end{tikzpicture}
\end{document}
答案1
这可能不是处理此问题的最佳tikz
方式,但即使没有示例中缺少的库也可以工作。
\documentclass[tikz]{standalone}
\newcommand{\tri}[1]{%
\draw[#1] (0,0) -- (4,0) -- (4,4) -- cycle;
}
\begin{document}
\begin{tikzpicture}
\tri{fill=black}
\tri{fill=red,rotate=90}
\tri{fill=green,rotate=180}
\tri{fill=blue,rotate=270}
\end{tikzpicture}
\end{document}
答案2
也许您可以考虑使用pics
。您可以使用它们来定义复杂的 tikz 图形并重复使用它。可以使用参数定义它们,甚至可以在最终绘制它们时更改某些属性。
以下代码显示了两个带有图形的示例。mytriangle
定义一个正方形三角形,其原点位于一个顶点,并带有三个参数:填充颜色和两条边的长度。第二个示例定义了“复杂”图像。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{positioning}
\tikzset{
pics/mytriangle/.style n args={3}{
code={
\fill[#1] (0,0)--++(0,#2)--++(#3,0)--cycle;
}
},
myfigure/.pic={
\fill[black] (0,0) rectangle ++(-1,-1);
\fill[blue] (-1,0) rectangle ++(-3,-1);
\fill[green] (-4,0)-- ++(-1,0)--++(1,-1)--cycle;
\fill[blue] (0,-1) rectangle ++(-1,-3);
\fill[green] (0,-4)-- ++(-1,0)--++(1,-1)--cycle;
\fill[red] (-4,-1)-- ++(0,-3)--++(3,0)--cycle;
}
}
\begin{document}
\begin{tikzpicture}
\foreach \a/\c in {0/red,90/green,180/blue,270/black}
\pic[rotate=\a] at (0,0) {mytriangle={\c}{2}{2}};
\begin{scope}[xshift=4cm]
\foreach \a/\c in {0/red,90/green,180/blue,270/black}
\pic[rotate=\a] at (0,0) {mytriangle={\c}{1}{1.8}};
\end{scope}
\begin{scope}[xshift=8cm]
\foreach \a/\c in {0/red,90/green,180/blue,270/black}
\pic[rotate=\a] at (0,0) {mytriangle={\c}{1.5}{1}};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}
\foreach \a in {30,120,210,300}
\pic[rotate=\a] at (\a:-1cm) {myfigure};
\end{tikzpicture}
\end{document}
答案3
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\foreach \X/\Col in{1/red,2/green,3/black,4/blue}
{
\coordinate (x\X) at ({-90*\X}:4);
\draw[fill=\Col] (x\X) -- +({-90*(\X-1)}:4) -- (O) -- cycle;
}
%Gridline
\coordinate (G1) at ($(x1)+(x2)$);
\coordinate (G2) at ($(x3)+(x4)$);
\draw [step=0.5cm,draw=gray] (G1) grid (G2);
\draw[white,opacity=1] (current bounding box.south west) rectangle
(current bounding box.north east);
\end{tikzpicture}
\end{document}