4

I use the following code to draw pentomino shapes and boards.

How can I put each pentomino in a unique style, so that each one can be rotated or flipped vertically or horizontally to fill pentomino boards.

\documentclass[12pt,a4paper]{article}
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{geometry}
\geometry{a4paper, left=10mm, right=10mm, top=8mm, bottom=15mm}
\begin{document}
%begin defining shapes ==========================
\pgfmathsetmacro{\lwdth}{.3mm}% for line width
\tikzset{%
filrd/.style={%
fill=red},
filrdl/.style={%
fill=red!40!white},
filgr/.style={%
fill=green},
filbl/.style={%
fill=blue},
filbll/.style={%
fill=blue!50!white},
filcn/.style={%
fill=cyan},
filcnl/.style={%
fill=cyan!70!white},
filtl/.style={%
fill=teal},
filtll/.style={%
fill=teal!70!white},
filov/.style={%
fill=olive},
filovl/.style={%
fill=olive!80!white},
filvtl/.style={%
fill=violet!50!white},
filyw/.style={%
fill=yellow!50!white},
filbk/.style={%
fill=black},
}
%end defining shapes ==========================
%begin defining grid ==========================
\newcommand\sqw{1}
\tikzset{
pics/square/.default={\sqw},
pics/square/.style = {
code = {
\draw[pic actions, draw=none] (0,0) rectangle (#1,#1);
}}}%draw=none is to avoid displaying the little squares outline

\renewcommand\sqw{1.4} %end defining grid ========================== %begin defining pentoshapes ========================== \def\pentshap#1 {% \begin{tikzpicture}[scale=#1, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (15\sqw, 16\sqw); % \node at (2.5\sqw,12.5\sqw) {\huge\bf T}; \foreach \i in {15}{ \foreach \j in {1,2,3}{ \pic[filcn] at (\j\sqw,\i\sqw) {square};};}; \pic[filcn] at (2\sqw,14\sqw) {square}; \pic[filcn] at (2\sqw,13\sqw) {square}; % \node at (6.5\sqw,12.5\sqw) {\huge\bf U}; \foreach \i in {14}{ \foreach \j in {5,6,7}{ \pic[filcnl] at (\j\sqw,\i\sqw) {square};};}; \pic[filcnl] at (5\sqw,15\sqw) {square}; \pic[filcnl] at (7\sqw,15\sqw) {square}; % \node at (10.5\sqw,12.5\sqw) {\huge\bf V}; \foreach \i in {13}{ \foreach \j in {9,10,11}{ \pic[filtll] at (\j\sqw,\i\sqw) {square};};}; \pic[filtll] at (9\sqw,15\sqw) {square}; \pic[filtll] at (9\sqw,14\sqw) {square}; % \node at (14.5\sqw,12.5\sqw) {\huge\bf Y}; \foreach \i in {15,...,12}{ \foreach \j in {13}{ \pic[filrd] at (\j\sqw,\i\sqw) {square};};}; \pic[filrd] at (14\sqw,14\sqw) {square}; % \node at (2.5\sqw,11.5\sqw) {\huge\bf W}; \foreach \i in {9}{ \foreach \j in {1,2}{ \pic[filbll] at (\j\sqw,\i\sqw) {square};};}; \foreach \i in {8}{ \foreach \j in {2,3}{ \pic[filbll] at (\j\sqw,\i\sqw) {square};};}; \pic[filbll] at (1\sqw,10\sqw) {square}; % \node at (6.5\sqw,11.5\sqw) {\huge\bf X}; \foreach \i in {9}{ \foreach \j in {5,6,7}{ \pic[filvtl] at (\j\sqw,\i\sqw) {square};};}; \pic[filvtl] at (6\sqw,10\sqw) {square}; \pic[filvtl] at (6\sqw,8\sqw) {square}; % \node at (10.5\sqw,11.5\sqw) {\huge\bf Z}; \foreach \i in {10,...,8}{ \foreach \j in {10}{ \pic[filrdl] at (\j\sqw,\i\sqw) {square};};}; \pic[filrdl] at (9\sqw,10\sqw) {square}; \pic[filrdl] at (11\sqw,8\sqw) {square}; % \node at (13.5\sqw,11.5\sqw) {\huge\bf L}; \foreach \i in {10,...,7}{ \foreach \j in {13}{ \pic[filgr] at (\j\sqw,\i\sqw) {square};};}; \pic[filgr] at (14\sqw,7\sqw) {square}; % \node at (2.5\sqw,6.5\sqw) {\huge\bf P}; \foreach \i in {5,4}{ \foreach \j in {2,3}{ \pic[filov] at (\j\sqw,\i\sqw) {square};};}; \pic[filov] at (2\sqw,3\sqw) {square}; % \node at (6.5\sqw,6.5\sqw) {\huge\bf F}; \foreach \i in {5}{ \foreach \j in {6,7}{ \pic[filovl] at (\j\sqw,\i\sqw) {square};};}; \foreach \i in {4}{ \foreach \j in {5,6}{ \pic[filovl] at (\j\sqw,\i\sqw) {square};};}; \pic[filovl] at (6\sqw,3\sqw) {square}; % \node at (10.5\sqw,6.5\sqw) {\huge\bf N}; \foreach \i in {5,4}{ \foreach \j in {11}{ \pic[filtll] at (\j\sqw,\i\sqw) {square};};}; \foreach \i in {4,...,2}{ \foreach \j in {10}{ \pic[filtll] at (\j\sqw,\i\sqw) {square};};}; % \node at (13.5\sqw,6.5\sqw) {\huge\bf I}; \foreach \i in {5,...,1}{ \foreach \j in {13}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; \end{tikzpicture} }% %end defining pentoshapes ========================== \begin{center} The 12 Pentominoes shapes \pentshap 1 \end{center} \newpage \begin{center} \begin{tikzpicture}[scale=1, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (9\sqw, 9\sqw); \foreach \i in {5,4}{ \foreach \j in {4,5}{ \pic[filbk] at (\j\sqw,\i\sqw) {square};};}; \end{tikzpicture} \end{center} \end{document}

enter image description here

enter image description here enter image description here

Hany
  • 4,709

1 Answers1

7

You can achieve this with a \pic for each of the 'pentominoes'. This way you can define a different style for each one. And then they are easy to place or rotate. It's possible to flip then too, but I find this easier if I add a parameter that inverts the xscale (or the yscale, one of them).

An example as a starting point:

\documentclass[tikz,border=2mm]{standalone}

\tikzset {% some pentominoes: pics/pentominoT/.style={code={\draw[fill=green,fill opacity=0.2] (1,0) |- (0,2) |- (3,3) |- (2,2) |- cycle;}}, pics/pentominoU/.style={code={\draw[fill=red ,fill opacity=0.2] (0,0) |- (1,2) |- (2,1) |- (3,2) |- cycle;}}, pics/pentominoN/.style={code={\draw[xscale=#1,% <-- -1/1 to flip/not flip fill=blue ,fill opacity=0.2] (0,0) |- (1,3) |- (2,4) |- (1,2) |- cycle; \fill[red] (0,0) circle [radius=1pt]; % <-- remove this when you are done }}, pics/pentominoN/.default={1},% <-- default: not flip }

\begin{document} \begin{tikzpicture}[line join=round,thick] \draw[help lines] (0,0) grid (6,7); \pic [rotate=180] at (4,4) {pentominoT}; \pic at (2,2) {pentominoT}; \pic [rotate= 90] at (6,3) {pentominoU}; \pic [rotate=-90] at (0,3) {pentominoU}; \pic at (1,3) {pentominoN}; % <-- default: not flip \pic [rotate=-90] at (2,0) {pentominoN={-1}};% <-- flip \end{tikzpicture} \end{document}

enter image description here Edit: I added a reference point at the origin to the pentominoN as an example. This way is easy to place the piece on the board.

Juan Castaño
  • 28,426
  • @ Juan Thank you very much for your answer. I will redefine my code and apply yours. Just 2 comments. The first, in your code: "pentominoN/.style [xscale=#1,% <-- 1/-1 to flip/not flip" did you mean "1/-1 not to flip/flip"? The second, when rotating the pentomino, applying the x,y coordinate is tricky; you have to do a lot of trials to decide where to place the rotated pentomino in the desired location. I know this depends on the starting point of the shape. – Hany Jan 31 '22 at 04:53
  • @Hany. For the first question, you are right and I edited my answer. For the second, you can add a reference point at the origin to help you. I added it as an example to the pentominoN, but you can add it to all the pieces until you have your draw complete and then comment it or remove it. – Juan Castaño Jan 31 '22 at 07:09
  • @ – Juan Thank you very much for your update. I will apply your reference point and see how it can help; and get back to you. – Hany Jan 31 '22 at 11:18
  • @ – Juan If you have time, would you please check my post: https://tex.stackexchange.com/questions/632041/coloraddo-cards-defined-as-pic – Hany Jan 31 '22 at 16:55