科罗拉多卡定义为 \pic

科罗拉多卡定义为 \pic

我使用以下代码来回答这个问题邮政绘制科罗拉多游戏的进度,该游戏使用 3x3 的卡片排列在棋盘上;只显示棋盘的一部分,如下图所示。

其他卡片在文档的其他位置以不同的排列方式使用。

是否可以将每张卡片放入其中\pic,然后将每张卡片插入\pic其各自的位置;而不是重复绘制整个板!

\documentclass[12pt,a4paper]{article}
\usepackage{geometry}
\geometry{a4paper, left=10mm, right=10mm, top=8mm, bottom=15mm}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\begin{document}
%begin defining shapes ==========================
\pgfmathsetmacro{\lwdth}{.3mm}% for line width

\tikzset{%
trg/.style={%
regular polygon, regular polygon sides=3, draw=black, line width=\lwdth, fill=blue, minimum size=1.4cm, rounded corners=0.1cm},
crc/.style={%
circle, draw=black, line width=\lwdth, fill=green!70!black, minimum size=1.cm},
sqr/.style={%
regular polygon, regular polygon sides=4, draw=black, line width=\lwdth, fill=yellow, minimum size=1.2cm},
str/.style={%
star, star points=6, draw=black, line width=\lwdth, fill=red, minimum size=1.1cm},
filrd/.style={%
fill=red},
filgr/.style={%
fill=green},
filyw/.style={%
fill=yellow},
filbl/.style={%
fill=blue},
}
%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);
}}}

\renewcommand\sqw{1.3}
%end defining grid ==========================
An example of the placement of the first 4 cards of a game

starting board before play begins
\begin{tikzpicture}[scale=0.3, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 7*\sqw);
%
\foreach \i in {3,2,1}{ 
\foreach \j in {1,2,3}{ 
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {4,5,6}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {6,5,4}{ 
\foreach \j in {7,8,9}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {7,8,9}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {6,5,4}{ 
\foreach \j in {4,5,6}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
%
\foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/4.5,2.5/6.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/6.5,3.5/5.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {2.5/4.5,3.5/6.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\vskip .6cm
after player 1 places card 1 \kern 4.9mm
\begin{tikzpicture}[scale=0.3, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 7*\sqw);
%
\foreach \i in {3,2,1}{ 
\foreach \j in {1,2,3}{ 
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {4,5,6}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {6,5,4}{ 
\foreach \j in {7,8,9}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {7,8,9}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
%
\foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4,4.5/4.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/4.5,2.5/6.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/6.5,3.5/5.5,4.5/5.5,5.5/5.5,5.5/4.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {2.5/4.5,3.5/6.5,4.5/6.5,5.5/6.5,6.5/6.5,6.5/5.5,6.5/4.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\vskip .4cm
after player 2 places card 2 \kern 4.9mm
\begin{tikzpicture}[scale=0.3, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 7*\sqw);
%
\foreach \i in {3,2,1}{ 
\foreach \j in {1,2,3}{ 
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {6,5,4}{ 
\foreach \j in {7,8,9}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {7,8,9}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
%
\foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4,4.5/4.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/4.5,2.5/6.5,4.5/3.5,4.5/1.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/6.5,3.5/5.5,4.5/5.5,5.5/5.5,5.5/4.5,4.5/2.5,5.5/3.5,5.5/2.5,5.5/1.5,6.5/2.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {2.5/4.5,3.5/6.5,4.5/6.5,5.5/6.5,6.5/6.5,6.5/5.5,6.5/4.5,6.5/3.5,6.5/1.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\vskip .4cm
after player 3 places card 3 \kern 4.9mm
\begin{tikzpicture}[scale=0.3, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 7*\sqw);
%
\foreach \i in {6,5,4}{ 
\foreach \j in {7,8,9}{ 
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{ 
\foreach \j in {7,8,9}{ 
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
%
\foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4,4.5/4.4,3.5/3.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/4.5,2.5/6.5,4.5/3.5,4.5/1.5,1.5/3.5,1.5/2.5,1.5/1.5,2.5/1.5,3.5/1.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/6.5,3.5/5.5,4.5/5.5,5.5/5.5,5.5/4.5,4.5/2.5,5.5/3.5,5.5/2.5,5.5/1.5,6.5/2.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {2.5/4.5,3.5/6.5,4.5/6.5,5.5/6.5,6.5/6.5,6.5/5.5,6.5/4.5,6.5/3.5,6.5/1.5,2.5/3.5,2.5/2.5,3.5/2.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\vskip 1.cm
central square
\begin{tikzpicture}[scale=0.5, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (4*\sqw, 4*\sqw);
%
\foreach\x/\y in {1.5/2.4,2.5/2.4,3.5/1.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/3.5,3.5/2.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/1.5,2.5/3.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {2.5/1.5,3.5/3.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\hskip .6cm
card1
\begin{tikzpicture}[scale=0.5, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (4*\sqw, 4*\sqw);
%
\foreach\x/\y in {1.5/1.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/2.5,2.5/2.5,3.5/2.5,2.5/1.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/3.5,2.5/3.5,3.5/3.5,3.5/2.5,3.5/1.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\hskip .6cm
card2
\begin{tikzpicture}[scale=0.5, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (4*\sqw, 4*\sqw);
%
\foreach\x/\y in {1.5/2.5,2.5/1.5,2.5/2.5,2.5/3.5,3.5/2.5}
\node[str] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/1.5,1.5/3.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {3.5/1.5,3.5/3.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\hskip .6cm
card3
\begin{tikzpicture}[scale=0.5, transform shape]
\draw[step=\sqw] (\sqw,\sqw) grid (4*\sqw, 4*\sqw);
%
\foreach\x/\y in {3.5/3.4}
\node[trg] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {1.5/1.5,1.5/2.5,1.5/3.5,2.5/1.5,3.5/1.5}
\node[sqr] at (\x*\sqw,\y*\sqw) {};
%
\foreach\x/\y in {2.5/2.5,2.5/3.5,3.5/2.5}
\node[crc] at (\x*\sqw,\y*\sqw) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

对于自由绘图,我故意不使用库shapes.geometric,也不使用库matrix

这里每张卡片都只是一张pic

我们可以用 缩放整个图形。不应使用[scale=.5,transform shape]选项(我认为在这种情况下通常使用 ,它不适用于 ,请查看 pgfmanual.pdf)。rounded cornerscale

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\begin{document}
\tikzset{%
triangle/.pic={\draw[fill=blue,shift={(0,-.1)},scale=.45] (90:1)--(210:1)--(-30:1)--cycle;},
circle/.pic={\draw[fill=green!70,scale=.4] (0,0) circle(1);},
star/.pic={\draw[fill=magenta,scale=.4] (90:1)--(120:.6)--(150:1)--(180:.6)--
(210:1)--(240:.6)--(270:1)--(300:.6)--
(330:1)--(0:.6)--(30:1)--(60:.6)--cycle;},
square/.pic={\draw[fill=yellow,scale=.35] (-1,-1) rectangle (1,1);}
}
\tikzset{first card/.pic={  
\fill[white] (0,0) rectangle (3,3);
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{circle}   ++(0:1) pic{circle} +(0:1) pic{circle}
(.5,1.5) pic{star}     ++(0:1) pic{star}   +(0:1) pic{circle}
(.5,.5)  pic{triangle} ++(0:1) pic{star}   +(0:1) pic{circle}
;}}
\tikzset{second card/.pic={ 
\fill[white] (0,0) rectangle (3,3);
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{square} ++(0:1) pic{star} +(0:1) pic{circle}
(.5,1.5) pic{star}   ++(0:1) pic{star} +(0:1) pic{star}
(.5,.5)  pic{square} ++(0:1) pic{star} +(0:1) pic{circle}
;}}
\tikzset{third card/.pic={
\fill[white] (0,0) rectangle (3,3);         
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{square} ++(0:1) pic{circle} +(0:1) pic{triangle}
(.5,1.5) pic{square} ++(0:1) pic{circle} +(0:1) pic{circle}
(.5,.5)  pic{square} ++(0:1) pic{square} +(0:1) pic{square}
;}}
\tikzset{central card/.pic={    
\fill[white] (0,0) rectangle (3,3);
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{star}     ++(0:1) pic{square}   +(0:1) pic{circle}
(.5,1.5) pic{triangle} ++(0:1) pic{triangle} +(0:1) pic{star}
(.5,.5)  pic{square}   ++(0:1) pic{circle}   +(0:1) pic{triangle}
;}}
\begin{tikzpicture}
\path 
(0,0) pic{first card}    +(1.5,3.5) node{first card}
(4,0) pic{second card}   +(1.5,3.5) node{second card}
(8,0) pic{third card}    +(1.5,3.5) node{third card}
(12,0) pic{central card} +(1.5,3.5) node{central card}
;
\end{tikzpicture}
\vspace*{5mm}

Before putting cards

\begin{tikzpicture}
\fill[magenta] (0,0) rectangle ++(3,3) rectangle +(-3,3);   
\fill[blue] (3,0) rectangle ++(3,3) rectangle +(3,3);
\fill[yellow] (3,6) rectangle ++(3,-3) rectangle +(3,-3);
\draw[very thick] (0,0) grid[step=3cm] (9,6);
\end{tikzpicture}
\vspace*{5mm}

After putting cards

\begin{tikzpicture}
\fill[magenta] (0,0) rectangle ++(3,3) rectangle +(-3,3);   
\fill[blue] (3,0) rectangle ++(3,3) rectangle +(3,3);
\fill[yellow] (3,6) rectangle ++(3,-3) rectangle +(3,-3);
\draw[very thick] (0,0) grid[step=3cm] (9,6);
\path 
(0,0) pic{first card}
(0,3) pic{second card}
(6,0) pic{third card}
(6,3) pic{central card}
;
\end{tikzpicture}

\vspace*{5mm}

We can scale whole figure with \verb|[scale=.5,transform shape]|

\begin{tikzpicture}[scale=.5,transform shape]
    \fill[magenta] (0,0) rectangle ++(3,3) rectangle +(-3,3);   
    \fill[blue] (3,0) rectangle ++(3,3) rectangle +(3,3);
    \fill[yellow] (3,6) rectangle ++(3,-3) rectangle +(3,-3);
    \draw[very thick] (0,0) grid[step=3cm] (9,6);
    \path 
    (0,0) pic{first card}
    (0,3) pic{second card}
    (6,0) pic{third card}
    (6,3) pic{central card}
    ;
\end{tikzpicture}
\end{document}

附录我使用库更新了代码,按照OP的要求:只需更改pics 、、和shapes.geometric的定义,如下所示。trianglecirclestarsquare

\usetikzlibrary{shapes.geometric}
\tikzset{triangle/.pic={
\node[regular polygon,regular polygon sides=3,minimum size=8mm, draw,fill=blue,shift={(0,-1mm)}]{};},
circle/.pic={\node[circle,draw,fill=green!70,minimum size=8mm]{};},
star/.pic={\node[star,star points=6,draw,fill=red,minimum size=8mm]{};},
square/.pic={\node[draw,fill=yellow,minimum size=8mm]{};}
}

相关内容