I have a tikzpicture that has repeated elements. Right now I am defining them individually. But I would like to have a code that defines one template-triangle whose dimensions I control (like I do in my example via p1,q1) and use rotation to control orientation.
(Please note that this example is just for the purpose of illustration. I want to build more complicated patterns using other shapes and would like to avoid using more libraries)
My code:
\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}
2nd Example
\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}




graphicxorgraphics, but thanks for loadingcalc... These are very different examples in that the first one is really very repetative in an obvious way but the second is less so... so you may want to split this question into two. – Feb 22 '18 at 00:45