为了绘制填充网格,我使用了第一个代码(来自这个帖子)。
我设法通过使用第二个代码来稍微减少代码,利用\foreach
。
是否可以通过填充网格中的一组 9 个方块来进一步简化它,例如从点 0,0 到点 3,3 的坐标,填充绿色,等等。
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\tikzset{%
filrd/.style={%
fill=red},
filgr/.style={%
fill=green},
filyw/.style={%
fill=yellow},
filbl/.style={%
fill=blue},
}
%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.3}
%end defining grid ==========================
\begin{center}
\begin{tikzpicture}
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 16*\sqw);
%col1
\pic[filgr] at (1*\sqw,15*\sqw) {square};
\pic[filgr] at (1*\sqw,14*\sqw) {square};
\pic[filgr] at (1*\sqw,13*\sqw) {square};
\pic[filyw] at (1*\sqw,12*\sqw) {square};
\pic[filyw] at (1*\sqw,11*\sqw) {square};
\pic[filyw] at (1*\sqw,10*\sqw) {square};
\pic[filbl] at (1*\sqw,9*\sqw) {square};
\pic[filbl] at (1*\sqw,8*\sqw) {square};
\pic[filbl] at (1*\sqw,7*\sqw) {square};
\pic[filgr] at (1*\sqw,6*\sqw) {square};
\pic[filgr] at (1*\sqw,5*\sqw) {square};
\pic[filgr] at (1*\sqw,4*\sqw) {square};
\pic[filrd] at (1*\sqw,3*\sqw) {square};
\pic[filrd] at (1*\sqw,2*\sqw) {square};
\pic[filrd] at (1*\sqw,1*\sqw) {square};
%col2
\pic[filgr] at (2*\sqw,15*\sqw) {square};
\pic[filgr] at (2*\sqw,14*\sqw) {square};
\pic[filgr] at (2*\sqw,13*\sqw) {square};
\pic[filyw] at (2*\sqw,12*\sqw) {square};
\pic[filyw] at (2*\sqw,11*\sqw) {square};
\pic[filyw] at (2*\sqw,10*\sqw) {square};
\pic[filbl] at (2*\sqw,9*\sqw) {square};
\pic[filbl] at (2*\sqw,8*\sqw) {square};
\pic[filbl] at (2*\sqw,7*\sqw) {square};
\pic[filgr] at (2*\sqw,6*\sqw) {square};
\pic[filgr] at (2*\sqw,5*\sqw) {square};
\pic[filgr] at (2*\sqw,4*\sqw) {square};
\pic[filrd] at (2*\sqw,3*\sqw) {square};
\pic[filrd] at (2*\sqw,2*\sqw) {square};
\pic[filrd] at (2*\sqw,1*\sqw) {square};
%col3
\pic[filgr] at (3*\sqw,15*\sqw) {square};
\pic[filgr] at (3*\sqw,14*\sqw) {square};
\pic[filgr] at (3*\sqw,13*\sqw) {square};
\pic[filyw] at (3*\sqw,12*\sqw) {square};
\pic[filyw] at (3*\sqw,11*\sqw) {square};
\pic[filyw] at (3*\sqw,10*\sqw) {square};
\pic[filbl] at (3*\sqw,9*\sqw) {square};
\pic[filbl] at (3*\sqw,8*\sqw) {square};
\pic[filbl] at (3*\sqw,7*\sqw) {square};
\pic[filgr] at (3*\sqw,6*\sqw) {square};
\pic[filgr] at (3*\sqw,5*\sqw) {square};
\pic[filgr] at (3*\sqw,4*\sqw) {square};
\pic[filrd] at (3*\sqw,3*\sqw) {square};
\pic[filrd] at (3*\sqw,2*\sqw) {square};
\pic[filrd] at (3*\sqw,1*\sqw) {square};
%col4
\pic[filrd] at (4*\sqw,15*\sqw) {square};
\pic[filrd] at (4*\sqw,14*\sqw) {square};
\pic[filrd] at (4*\sqw,13*\sqw) {square};
\pic[filbl] at (4*\sqw,12*\sqw) {square};
\pic[filbl] at (4*\sqw,11*\sqw) {square};
\pic[filbl] at (4*\sqw,10*\sqw) {square};
\pic[filyw] at (4*\sqw,9*\sqw) {square};
\pic[filyw] at (4*\sqw,8*\sqw) {square};
\pic[filyw] at (4*\sqw,7*\sqw) {square};
\pic[filbl] at (4*\sqw,6*\sqw) {square};
\pic[filbl] at (4*\sqw,5*\sqw) {square};
\pic[filbl] at (4*\sqw,4*\sqw) {square};
\pic[filgr] at (4*\sqw,3*\sqw) {square};
\pic[filgr] at (4*\sqw,2*\sqw) {square};
\pic[filgr] at (4*\sqw,1*\sqw) {square};
%col5
\pic[filrd] at (5*\sqw,15*\sqw) {square};
\pic[filrd] at (5*\sqw,14*\sqw) {square};
\pic[filrd] at (5*\sqw,13*\sqw) {square};
\pic[filbl] at (5*\sqw,12*\sqw) {square};
\pic[filbl] at (5*\sqw,11*\sqw) {square};
\pic[filbl] at (5*\sqw,10*\sqw) {square};
\pic[filyw] at (5*\sqw,9*\sqw) {square};
\pic[filyw] at (5*\sqw,8*\sqw) {square};
\pic[filyw] at (5*\sqw,7*\sqw) {square};
\pic[filbl] at (5*\sqw,6*\sqw) {square};
\pic[filbl] at (5*\sqw,5*\sqw) {square};
\pic[filbl] at (5*\sqw,4*\sqw) {square};
\pic[filgr] at (5*\sqw,3*\sqw) {square};
\pic[filgr] at (5*\sqw,2*\sqw) {square};
\pic[filgr] at (5*\sqw,1*\sqw) {square};
%col6
\pic[filrd] at (6*\sqw,15*\sqw) {square};
\pic[filrd] at (6*\sqw,14*\sqw) {square};
\pic[filrd] at (6*\sqw,13*\sqw) {square};
\pic[filbl] at (6*\sqw,12*\sqw) {square};
\pic[filbl] at (6*\sqw,11*\sqw) {square};
\pic[filbl] at (6*\sqw,10*\sqw) {square};
\pic[filyw] at (6*\sqw,9*\sqw) {square};
\pic[filyw] at (6*\sqw,8*\sqw) {square};
\pic[filyw] at (6*\sqw,7*\sqw) {square};
\pic[filbl] at (6*\sqw,6*\sqw) {square};
\pic[filbl] at (6*\sqw,5*\sqw) {square};
\pic[filbl] at (6*\sqw,4*\sqw) {square};
\pic[filgr] at (6*\sqw,3*\sqw) {square};
\pic[filgr] at (6*\sqw,2*\sqw) {square};
\pic[filgr] at (6*\sqw,1*\sqw) {square};
%col7
\pic[filyw] at (7*\sqw,15*\sqw) {square};
\pic[filyw] at (7*\sqw,14*\sqw) {square};
\pic[filyw] at (7*\sqw,13*\sqw) {square};
\pic[filgr] at (7*\sqw,12*\sqw) {square};
\pic[filgr] at (7*\sqw,11*\sqw) {square};
\pic[filgr] at (7*\sqw,10*\sqw) {square};
\pic[filrd] at (7*\sqw,9*\sqw) {square};
\pic[filrd] at (7*\sqw,8*\sqw) {square};
\pic[filrd] at (7*\sqw,7*\sqw) {square};
\pic[filyw] at (7*\sqw,6*\sqw) {square};
\pic[filyw] at (7*\sqw,5*\sqw) {square};
\pic[filyw] at (7*\sqw,4*\sqw) {square};
\pic[filbl] at (7*\sqw,3*\sqw) {square};
\pic[filbl] at (7*\sqw,2*\sqw) {square};
\pic[filbl] at (7*\sqw,1*\sqw) {square};
%col8
\pic[filyw] at (8*\sqw,15*\sqw) {square};
\pic[filyw] at (8*\sqw,14*\sqw) {square};
\pic[filyw] at (8*\sqw,13*\sqw) {square};
\pic[filgr] at (8*\sqw,12*\sqw) {square};
\pic[filgr] at (8*\sqw,11*\sqw) {square};
\pic[filgr] at (8*\sqw,10*\sqw) {square};
\pic[filrd] at (8*\sqw,9*\sqw) {square};
\pic[filrd] at (8*\sqw,8*\sqw) {square};
\pic[filrd] at (8*\sqw,7*\sqw) {square};
\pic[filyw] at (8*\sqw,6*\sqw) {square};
\pic[filyw] at (8*\sqw,5*\sqw) {square};
\pic[filyw] at (8*\sqw,4*\sqw) {square};
\pic[filbl] at (8*\sqw,3*\sqw) {square};
\pic[filbl] at (8*\sqw,2*\sqw) {square};
\pic[filbl] at (8*\sqw,1*\sqw) {square};
%col9
\pic[filyw] at (9*\sqw,15*\sqw) {square};
\pic[filyw] at (9*\sqw,14*\sqw) {square};
\pic[filyw] at (9*\sqw,13*\sqw) {square};
\pic[filgr] at (9*\sqw,12*\sqw) {square};
\pic[filgr] at (9*\sqw,11*\sqw) {square};
\pic[filgr] at (9*\sqw,10*\sqw) {square};
\pic[filrd] at (9*\sqw,9*\sqw) {square};
\pic[filrd] at (9*\sqw,8*\sqw) {square};
\pic[filrd] at (9*\sqw,7*\sqw) {square};
\pic[filyw] at (9*\sqw,6*\sqw) {square};
\pic[filyw] at (9*\sqw,5*\sqw) {square};
\pic[filyw] at (9*\sqw,4*\sqw) {square};
\pic[filbl] at (9*\sqw,3*\sqw) {square};
\pic[filbl] at (9*\sqw,2*\sqw) {square};
\pic[filbl] at (9*\sqw,1*\sqw) {square};
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\draw[step=\sqw] (\sqw,\sqw) grid (10*\sqw, 16*\sqw);
%gr
\foreach \i in {15,14,13,6,5,4}{
\foreach \j in {1,2,3}{
\pic[filgr] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {18,17,16,3,2,1}{
\foreach \j in {4,5,6}{
\pic[filgr] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {21,20,19,12,11,10}{
\foreach \j in {7,8,9}{
\pic[filgr] at (\j*\sqw,\i*\sqw) {square};};};
%yw
\foreach \i in {21,20,19,12,11,10}{
\foreach \j in {1,2,3}{
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {9,8,7}{
\foreach \j in {4,5,6}{
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {15,14,13,6,5,4}{
\foreach \j in {7,8,9}{
\pic[filyw] at (\j*\sqw,\i*\sqw) {square};};};
%bl
\foreach \i in {18,17,16,9,8,7}{
\foreach \j in {1,2,3}{
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {21,20,19,12,11,10,6,5,4}{
\foreach \j in {4,5,6}{
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {3,2,1}{
\foreach \j in {7,8,9}{
\pic[filbl] at (\j*\sqw,\i*\sqw) {square};};};
%rd
\foreach \i in {3,2,1}{
\foreach \j in {1,2,3}{
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {15,14,13}{
\foreach \j in {4,5,6}{
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\foreach \i in {18,17,16,9,8,7}{
\foreach \j in {7,8,9}{
\pic[filrd] at (\j*\sqw,\i*\sqw) {square};};};
%
\end{tikzpicture}
\end{center}
\end{document}
答案1
我不知道你的目标,所以我也不知道这是否是正确的方法。不过,这很简单。它只需要一个\foreach
循环遍历颜色的 。然后我将尺寸\sqw
作为比例因子应用,而不是在每个元素中应用它。我想,看到你提供的链接,你需要在这个网格上绘制更多图形。如果比例不适合他们,只需将我的所有代码放在 中scope
并将比例应用于此scope
。
例如:
\documentclass[tikz,border=2mm]{standalone}
\definecolor{colorg}{HTML}{00FF00} % green
\definecolor{colorr}{HTML}{FF0000} % red
\definecolor{colory}{HTML}{FFFF00} % yellow
\definecolor{colorb}{HTML}{0000FF} % blue
\begin{document}
\newcommand\sqw{1}
\begin{tikzpicture}[scale=\sqw,y=-1cm]
\foreach[count=\ii]\i in {g,r,y,
y,b,g,
b,y,r,
g,b,y,
r,g,b}
{
\pgfmathsetmacro\myrow{3*mod(\ii-1,3)}
\pgfmathsetmacro\mycol{3*div(\ii-1,3)}
\fill[color\i] (\myrow,\mycol) rectangle ++ (3,3);
}
\draw (0,0) grid (9,15);
\end{tikzpicture}
\end{document}
编辑:这就是我的想法,但正如我所说,我不知道这是否是更好的方法:
\documentclass[tikz,border=2mm]{standalone}
\definecolor{colorg}{HTML}{00FF00} % green
\definecolor{colorr}{HTML}{FF0000} % red
\definecolor{colory}{HTML}{FFFF00} % yellow
\definecolor{colorb}{HTML}{0000FF} % blue
\definecolor{colorw}{HTML}{FFFFFF} % white
\begin{document}
\newcommand\sqw{1}
\begin{tikzpicture}[scale=\sqw,y=-1cm]
\foreach[count=\ii]\i in {g,r,y,
y,b,g,
b,y,r,
g,b,y,
r,g,b,
w,w,y}
{
\pgfmathsetmacro\myrow{3*mod(\ii-1,3)}
\pgfmathsetmacro\mycol{3*div(\ii-1,3)}
\fill[color\i] (\myrow,\mycol) rectangle ++ (3,3);
}
\draw (0,0) grid (9,18);
\foreach\x/\y in {0/15,1/17,2/17}
\draw[very thick,fill=magenta] (\x+0.5,\y+0.5) circle [radius=0.3cm]; % <-- here you can define a \pic
\foreach\x/\y in {3/15,4/16,4/17,3/17}
\draw[very thick,fill=brown] (\x+0.2,\y+0.2) rectangle ++ (0.6,0.6); % <-- here you can define another \pic
\end{tikzpicture}
\end{document}