编辑

编辑

我正在尝试在 Ti 中重新创建以下图像

在此处输入图片描述

使用一些旧代码我能够产生以下结果

在此处输入图片描述

虽然我能够得到正确的结果,但我觉得我的解决方案有点奇怪,因为它需要两次传递。任何关于替代方法或代码改进的建议都非常欢迎。

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm]
\edef\size{4}
    \foreach \x in {0,...,\size} \foreach \y in {0,...,\size}
    {
        \pgfmathparse{mod(\x+\y,\size) ? "none" : "black"}
        \edef\colour{\pgfmathresult}
        \path[draw=black, fill=\colour] (\x,\y) rectangle ++ (1,1);

        \pgfmathparse{\x==\y ? "black" : "none"}
        \edef\colour{\pgfmathresult}
        \path[fill=\colour] (\x,\y) rectangle ++ (1,1);
    }
    \draw (0,0)--(0,\size+1)--(\size+1,\size+1)--(\size+1,0)--cycle;
\end{tikzpicture}

\end{document}

答案1

tikz

\documentclass[tikz]{standalone}

\begin{document}
    \begin{tikzpicture}[
node distance = 0mm,
   box/.style = {draw, minimum size=10mm, fill=black,
                 outer sep=0pt},
                      ]
\edef\size{4}
\foreach \y in {0,...,\size}
\foreach \x in {0,...,\size}
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
 \else
\node[box,fill=none] at (\x,\y) {};
 \fi
}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

笔记:的值\size必须为零或任何偶数自然数(0、2、4、...)

附录:

  • 上图的默认基线(current bounding box.south)˙ For series of those images for different value of大小为:
\documentclass{article}
\usepackage{tikz}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
    \begin{figure}
\begin{tabularx}{\linewidth}{>{\hsize=0.5\hsize}C C >{\hsize=1.5\hsize}C}
    \begin{tikzpicture}[baseline=(current bounding box.south),
node distance = 0mm,
   box/.style = {draw, minimum size=10mm, fill=black,
                 outer sep=0pt},
                      ]
\edef\size{0} % in this MWE the meaning of `\size` is changed
\foreach \y in {0,...,\size}
\foreach \x in {0,...,\size}
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
 \else
\node[box,fill=none] at (\x,\y) {};
 \fi
}
    \end{tikzpicture}
    \caption{}
&
    \begin{tikzpicture}[%baseline=(current bounding box.south),
node distance = 0mm,
   box/.style = {draw, minimum size=10mm, fill=black,
                 outer sep=0pt},
                      ]
\edef\size{1}
\foreach \y in {0,...,2*\size} % changed, now number of boxes is odd 
\foreach \x in {0,...,2*\size} % changed,
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
 \else
\node[box,fill=none] at (\x,\y) {};
 \fi
}
    \end{tikzpicture}
    \caption{}
&
    \begin{tikzpicture}[baseline=(current bounding box.south),
node distance = 0mm,
   box/.style = {draw, minimum size=10mm, fill=black,
                 outer sep=0pt},
                      ]
\edef\size{2}
\foreach \y in {0,...,\size}
\foreach \x in {0,...,\size}
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
 \else
\node[box,fill=none] at (\x,\y) {};
 \fi
}
    \end{tikzpicture}
    \caption{}
\end{tabularx}
    \end{figure}
\end{document}

在此处输入图片描述

答案2

PSTricks 解决方案仅用于娱乐目的!

在此处输入图片描述

\documentclass[border=1pt]{standalone}
\usepackage{pstricks}
\def\obj#1{%
    \pspicture[dimen=m](#1,#1)
        \multips(0,0)(0,1){#1}{\multips(0,0)(1,0){#1}{\psframe(1,1)}}
        \multips(0,0)(1,1){#1}{\psframe*(1,1)}
        \multips(0,#1)(1,-1){#1}{\psframe*(1,-1)}
    \endpspicture}

\begin{document}
\foreach \i in {3,5,7}{\obj{\i}\quad}
\end{document}

编辑

我发明了如下算法(尚未申请专利)。不需要嵌套循环。

在此处输入图片描述

\documentclass[border=12pt]{standalone}
\usepackage[nomessages]{fp}
\usepackage{xintexpr}
\usepackage{pstricks}
\psset{unit=5mm}
\def\obj#1{%
    \pspicture[dimen=m](#1,#1)
    \FPeval\N{#1*#1-1}
    \foreach \j in {0,...,\N}
    {
        \FPeval\y{trunc(\j/#1:0)}
        \FPeval\x{\j-#1*y}
        \xintifboolexpr{\x=\y||(\x+\y)=(#1-1)}
        {\psframe[fillstyle=solid,fillcolor=black](\x,\y)(+\x+1,\y+1)}
        {\psframe(\x,\y)(+\x+1,\y+1)}
    }
    \endpspicture}
\begin{document}
\foreach \i in {1,3,5,7,9}{\obj{\i}\quad}
\end{document}

答案3

编辑: 以下适用于所有值size

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
\edef\size{4}
    \foreach \x in {0,...,\size} \foreach \y in {0,...,\size} {
        \pgfmathsetmacro{\colour}{(\x==\y || \x+\y==\size) ? "black" : "none"}
        \draw[fill=\colour] (\x,\y) rectangle ++ (1,1);
    }
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案4

我看不出双循环的理由,也看不出复杂的条件。

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[xboard/.style={insert path={
(0,0) grid (#1,#1)
foreach \X in {1,...,#1}
{(\X-0.5,\X-0.5) pic{bx} (\X-0.5,#1-\X+0.5) pic{bx}}}},
pics/bx/.style={code={\fill (-0.5,-0.5) rectangle (0.5,0.5);}}]
\draw[xboard=1] [xshift=2cm,xboard=3]
[xshift=4cm,xboard=5] [xshift=6cm,xboard=7];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容