我使用以下代码来绘制五格骨牌形状和板。
我怎样才能以独特的方式放置每个五格骨牌,以便每个五格骨牌都可以垂直或水平旋转或翻转来填充五格骨牌板。
\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}
答案1
您可以使用\pic
每个“五格骨牌”来实现这一点。这样,您可以为每个五格骨牌定义不同的样式。然后它们就很容易放置或旋转了。也可以翻转它们,但我发现如果我添加一个反转xscale
(或yscale
其中一个)的参数,这更容易。
以如下示例作为起点:
\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}