这种形如方程x+y=const的映射必定是线性的,所以找到一个用两重循环的变量\x
以及\y
正好可以覆盖所需位置的线性坐标映射即可:
\documentclass[border=2pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,2}
\foreach \y in {1,...,6}
\node at (\x-.5,\y-.5) [scale=.75] {$(\x,\y)$};
\draw (0,0) grid ([xshift=-.01pt]2,6);
\clip [xshift={2cm-.01pt}] (0,0) -- (45:{3*sqrt(2)}) -- (90:6) -- cycle;
\draw [xshift=2cm,rotate=45,step={sqrt(2)/2}] (0,0) grid (6,6);
\foreach \x in {5,...,9}
\foreach \y in {1,...,\fpeval{(10-\x)}}
\node at (\x/2,\fpeval{\y+(\x-5)/2}) [scale=.5] {$(\fpeval{\x/2},\fpeval{\y+(\x-5)/2})$};
\end{tikzpicture}
\end{document}
Edited:
其实也就是小学生找规律问题的\pgffor
版本罢了...
General Case:
\documentclass[border=2pt,tikz]{standalone}
\newcommand*{\drawmygrid}[2]{%
\begin{tikzpicture}
\foreach \x in {1,...,#1}
\foreach \y in {1,...,#2}
\node at (\x-.5,\y-.5) [scale=.75] {$(\x,\y)$};
\draw (0,0) grid ([xshift=-.01pt]#1,#2);
\clip [xshift={#1cm-.01pt}] (0,0) -- (45:{(#2/2)*sqrt(2)}) -- (90:#2) -- cycle;
\draw [xshift={#1cm},rotate=45,step={sqrt(2)/2}] (0,0) grid (#2,#2);
\def\Na{\fpeval{2*#1+1}}
\def\Nb{\fpeval{\Na+#2-1}}
\foreach \x in {\Na,...,\fpeval{\Nb-1}}
\foreach \y in {1,...,\fpeval{\Nb-\x}}
\node at (\x/2,\fpeval{\y+(\x-\Na)/2}) [scale=.5] {$(\fpeval{\x/2},\fpeval{\y+(\x-\Na)/2})$};
\end{tikzpicture}
}
\begin{document}
\drawmygrid{2}{6}
\drawmygrid{5}{10}
\drawmygrid{8}{15}
\end{document}
问 如何控制这个foreach产生的node内容限定在框内?