如何用 TikZ 绘制对角分割网格?

如何用 TikZ 绘制对角分割网格?

绘制图表

在此处输入图片描述

我写了以下代码:

\documentclass[11pt]{article}
\usepackage{amsmath,tikz}

\begin{document}
\begin{tikzpicture}
 \draw (0,0)--(0,8)--(10,8)--(10,0)--(0,0);
\draw (0,7)--(10,7);
\draw (0,6)--(10,6);
\draw (0,5)--(10,5);
\draw (0,4)--(10,4);
\draw (0,3)--(10,3);
\draw (0,2)--(10,2);
\draw (0,1)--(10,1);
\draw (1,0)--(1,8);
\draw (2,0)--(2,8);
\draw (3,0)--(3,8);
\draw (4,0)--(4,8);
\draw (5,0)--(5,8);;
\draw (6,0)--(6,8);
\draw (7,0)--(7,8);
\draw (8,0)--(8,8);
\draw (9,0)--(9,8);
\draw (0,-1)--(10,9);
\end{tikzpicture}

\end{document}

但这会产生

在此处输入图片描述

如何使用 TikZ 绘制实际图片?

答案1

你是指这样的吗?

在此处输入图片描述

代码:

\documentclass[png,border=10pt,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.shapes}

\tikzset{
  decorate with/.style args={#1 separated by #2}{
    fill,
    decorate,decoration={shape backgrounds,shape=#1,shape size=1.5mm,
    shape sep={#2, between borders}}
  }
}

\pgfkeys{/tikz/.cd,
   num quad/.initial=5,
   num quad/.get=\numquad,
   num quad/.store in=\numquad,
}

\begin{document}


\begin{tikzpicture}[x=0.5025cm,y=0.5025cm,line cap=round]
\foreach \x [count=\xi] in {1,...,\numquad}{
  \foreach \y [count=\yi] in {\x,...,\numquad}{
    \node [draw, minimum size=0.5cm,outer sep=0pt,inner sep=0pt] (u-\xi\yi) at (\xi,-\yi) {};
  }
}

\pgfmathsetmacro\shiftx{\numquad-0.5}
\pgfmathsetmacro\shifty{0.5*(\numquad+1)}

\begin{scope}[xshift=\shiftx cm,yshift=-\shifty cm]
\foreach \x [count=\xi] in {1,...,\numquad}{
  \foreach \y [count=\yi] in {\x,...,\numquad}{
    \node [draw, minimum size=0.5cm,outer sep=0pt,inner sep=0pt] (d-\xi\yi) at (-\xi,\yi) {};
  }
}
\end{scope}

\pgfmathsetmacro\halfnumquad{\numquad/2}

\begin{scope}[xshift=0.25 cm]
\path[decorate with=circle separated by 0.125cm] (\halfnumquad,0.35)--($(\halfnumquad,0.35)+(0,1.5)$) ;
\end{scope}

\begin{scope}[xshift=\shiftx cm,yshift=-0.25cm]
\path[decorate with=circle separated by 0.125cm] (0.35,-\halfnumquad)--($(0.35,-\halfnumquad)+(1.5,0)$) ;
\end{scope}

\draw[very thick] ([yshift=-0.5cm]u-1\numquad.south)--([yshift=0.5cm]d-1\numquad.north);
\end{tikzpicture}

\end{document}

此解决方案允许使用 键自动选择方格数num quad:,选择后将计算移位。但是,它们是为此设计的,num quad=5因此可能需要进行一些调整。

答案2

使用 PSTricks。

单身的:

在此处输入图片描述

\documentclass[pstricks]{standalone}
\usepackage{pst-node,multido}
\psset{unit=5mm,dimen=middle,nodesep=-1}

\def\Atom#1{%
    \multido{\ix=1+1,\iy=#1+-1}{#1}{%
        \psframe(\ix,\iy)
        \multips(!-.5 #1 2 div)(-.5,0){3}{\qdisk(0,0){2pt}}}}

\def\Molecule#1{%
    \begin{pspicture}(-1,-1)(\numexpr#1+5,\numexpr#1+2)
    \rput{-90}(!0 #1){\Atom{#1}}
    \rput(!#1 3 add 0){\psscalebox{-1 1}{\Atom{#1}}}
    \pcline(!1.5 0)(!#1 1.5 add #1)
    \end{pspicture}}


\begin{document}
\Molecule{4}
\end{document}

多种的:

在此处输入图片描述

\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{multicol}
\pagestyle{empty}

\usepackage{pst-node,multido}
\psset{unit=5mm,dimen=middle,nodesep=-1}

\def\Atom#1{%
    \multido{\ix=1+1,\iy=#1+-1}{#1}{%
        \psframe(\ix,\iy)
        \multips(!-.5 #1 2 div)(-.5,0){3}{\qdisk(0,0){2pt}}}}

\def\Molecule#1{%
    \begin{pspicture}(-1,-1)(\numexpr#1+5,\numexpr#1+2)
    \rput{-90}(!0 #1){\Atom{#1}}
    \rput(!#1 3 add 0){\psscalebox{-1 1}{\Atom{#1}}}
    \pcline(!1.5 0)(!#1 1.5 add #1)
    \end{pspicture}}

\begin{document}
\begin{multicols}{2}
\enumerate
\multido{\i=1+1}{11}{\item \raisebox{-0.5\height}{\Molecule{\i}}}
\endenumerate
\end{multicols}
\end{document}

答案3

轻量级代码(下一个代码的某些部分来自 Qrrbrbirlbel 的回答)。主要思想是使用 的转换cm={a,b,c,d,(e,f)}。这里是反射,这种方法避免了重复(扩展)代码。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand\mysubfig[1]{%
\begin{scope}[#1]
    \foreach \Row in {1,...,8}{\draw (0,\Row-1) --++(\Row,0) --++(0,8+1-\Row);}
    \draw (8,8) -| (0,0);
    \foreach \Dot in {0,1,2}{\fill (4,9+\Dot/2) circle [radius=.125cm];}
\end{scope}}

\begin{tikzpicture}
\draw (0,0) -- (10,10);
  \mysubfig{shift={(0,2)}}% or cm={1,0,0,1,(0,2)}
  \mysubfig{cm={0,1,1,0,(4,2)}]}% exchange x and y then apply a translation (4,2)
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

只是为了好玩而已,这是一个矩阵版本。

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[row sep=-0.8pt,column sep=-0.8pt,
small boxes/.style={minimum size=3mm,outer sep=0,anchor=base,thick},
execute at empty cell={
\ifnum\numexpr\pgfmatrixcurrentrow+\pgfmatrixcurrentcolumn\relax<12%
    \ifnum\numexpr\pgfmatrixcurrentrow+\pgfmatrixcurrentcolumn\relax>9%
        \node[small boxes](m-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn){};
    \else
        \node[small boxes,draw](m-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn){};
    \fi
\else
    \node[small boxes,draw](m-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn){};
\fi
}] (m) {
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
&&&&&&&&&&\\
};
\draw ([yshift=-3mm]m-8-1.south) -- ([yshift=3mm]m-1-11.north);
\foreach \x in {1,2,3}{
\fill ([yshift=2mm+3*\x mm]m-1-4.north east) circle (3pt);
\fill ([xshift=2mm+3*\x mm]m-4-11.south east) circle (3pt);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容