4

The following code outputs a "C" shaped polyomino translated twice as shwon in the figure below. As can be seen from the code, I have written the same description for the polyomino 3 times. What I would much rather have is a code which functions as follows:

Give a name, say "pattern" to the C-shaped polyomino once it has been described.

Now if I want to display the polyomino at a certain position, I should just be able to tell the code the x and y coordinates of the two points where the bottom-left endpoint of the polyomino sits and the code should put it there.

Basically I want a more modular way to write the same code.

\documentclass[border=5mm]{standalone}


\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{backgrounds} % For testing with "framed" option to look at the bounding box
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{ticks=none}

\begin{document}

\begin{tikzpicture}[hatched/.style = {pattern=north west lines, opacity=0.3}, scale=4]
        \begin{scope}
            \draw[line width=0.2cm] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
            \fill[red] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
        \end{scope}

        \begin{scope}[xshift=1cm, yshift=3cm]
            \draw[line width=0.2cm] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
            \fill[red] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
        \end{scope}

        \begin{scope}[xshift=2cm, yshift=6cm]
            \draw[line width=0.2cm] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
            \fill[red] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
        \end{scope}

    \end{tikzpicture}
\end{document}

Output

3

as supplement to @JpuleV answer:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}       % it load tikz too
\pgfplotsset{compat=1.10,   % it is very old, now is available 1.16 ... upgrade it!
             ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds, % For testing with "framed" option to look at the bounding box
                calc,
                patterns}

\begin{document}
\begin{tikzpicture}[scale=2, transform shape]
\tikzset{
    myc/.pic={
    \draw[line width=2mm,fill=red] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
        }
}
\path (0,0) pic {myc} (1,3) pic {myc} (2,6) pic {myc};
\end{tikzpicture}
\quad
\begin{tikzpicture}[scale=4, transform shape]
\tikzset{
    myc/.pic={
    \draw[line width=2mm,fill=red] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
        }
}
\path (0,0) pic {myc} (1,3) pic {myc} (2,6) pic {myc};
\end{tikzpicture}
\qquad
\begin{tikzpicture}%[scale=4, transform shape]
\tikzset{
    myc/.pic={
    \draw[line width=2mm,fill=red, xscale=-1] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
        }
}
\path (0,0) pic {myc} (-1,3) pic[xscale=-1] {myc} (2,6) pic {myc};
\end{tikzpicture}
\end{document}

enter image description here

edit: If you like to rotate for 180 degrees all shapes, than simply add scale=-1 to myc style definition. IN case that you like to mirroring only one of them, than add xscale-1 to pic: pic[xscale=-1] {myc}. however, reflecting picreflect its internal coordinates. this you can simple compensate with accordingly changedx` coordinate of pic position. see third example in above image.

  • 1
    Your diagram looks better then mine. In my diagram there is a problem at the overlaps. The boundaries do not gel well. What did you do to not have this problem? – caffeinemachine May 17 at 11:09
  • 1
    (i) i use \draw[line width=2mm,fill=red] ..., consequently fill is inside lines, (ii) the same you obtain if you first draw \fill and than lines with \draw. with this fill not overlaps lines, consequently overlap of lines is seen differently (less disturbing). – Zarko May 17 at 11:48
  • Can you please tell me one more thing. Suppose I define myc/.pic as you have which gives me the c-shaped polyomino. Suppose now I wanted the mirror reflection of this c-shaped polyomino (equivalently, 180 rotation). Is there a way to define myc2/.pic by using the rotation operation? In other words, I want to do something like myc2/.pic = rotate by 180{myc/.pic }. Thanks. – caffeinemachine May 17 at 15:15
  • See edited answer. – Zarko May 17 at 16:20
9

Reference: pgfmanual.pdf, section 18.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{
    myc/.pic={
        \draw[line width=0.2cm] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
        \fill[red] (0, 0) -- (2, 0) -- (2, 1) -- (1, 1) -- (1, 2) -- (2, 2) -- (2, 3) -- (0, 3) -- cycle;
    }
}
\path (0,0) pic {myc} (1,3) pic {myc} (2,6) pic {myc};
\end{tikzpicture}
\end{document}

enter image description here

  • MWE in the question use for image scale=4 which in your solution doesn't work on the same way (pic are not scaled). You should mentioned, that in case of scaling image, now is needed to add transform shape. – Zarko May 17 at 8:11
  • Nice! To compress the code even more, you could fuse the draw and fill part with \draw[line width=0.2cm,fill=red]! :) – Vinzza May 17 at 8:23
  • @Vinzza It is already in Zarko's answer, so maybe I should not edit my answer. – user156344 May 17 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.