1

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}

Output enter image description here

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}

Output enter image description here

NAASI
  • 2,809
  • 2
    your code is not compilable. missing are some libraries ... – Zarko Feb 22 '18 at 00:36
  • Didn't know that there were tikz libraries graphicx or graphics, but thanks for loading calc... 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
  • 1
    one problem on question please ... – Zarko Feb 22 '18 at 00:48
  • i posted 2nd example because I am looking for general solution. Do u want me to post this 2nd example as a separate Q? – NAASI Feb 22 '18 at 00:49
  • While you wait for an answer to this question, can you go back to your previous ones and look if the answers solve your problems and accept them, if they do? – samcarter_is_at_topanswers.xyz Feb 22 '18 at 09:09

3 Answers3

3

May be you can consider to use pics. You can use them to define a complex tikz figure and reuse it. It's possible to define them with arguments or even change some properties when they are finally drawn.

Following code show two examples with your figures. mytriangle defines a square triangle with origin in one vertex with three arguments: filling color and length of two sides. The second example defines the "complex" image.

\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}

enter image description here

enter image description here

Ignasi
  • 136,588
2

Maybe not the best tikz style to handle this, but works even without the missing libraries from your example.

\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}
1
\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}
  • this is going to work for triangles only. I added another example to make my point. answer by samcarter makes a template and reuses it. Thats more close to what I am looking for – NAASI Feb 22 '18 at 00:46