绘图工具

绘图工具

我正在使用 TikZ 来画图... 这里我将给出我的一幅画。我在这里使用的画太长了。那么有没有简单的方法来画这个?

\begin{document}\begin{center}
\begin{tikzpicture}
\draw[pattern color=black,pattern=dots] (0,1) rectangle (3,1.2);
\draw (1.5,1.3) node[above] {0};
\draw[fill=black] (3,1) rectangle (6,1.2);
\draw[pattern color=blue,pattern=dots] (6,1) rectangle (9,1.2);
\draw (7,1.3) node[above] {2};
\draw[pattern=dots](0,0) rectangle (1,.2);
\draw (0.5,.3) node[above]{0};
\draw[fill=black](1,0) rectangle (2,.2);
\draw[pattern=dots](2,0) rectangle (3,.2);
\draw (2.5,.2) node[above] {2};
\draw[fill=black] (3,0) rectangle (6,.2);
\draw[pattern=dots](6,0) rectangle (7,.2);
\draw (6.5,.2) node[above] {0};
\draw[fill=black](7,0) rectangle (8,.2);
\draw[pattern=dots](8,0) rectangle (9,.2);
\draw (8.5,.2) node[above] {2};
\draw[pattern=dots](0,-1) rectangle (0.3,-1.2);
\draw (0.15,-1) node[above] {0};
\draw[fill=black](0.3,-1) rectangle (0.65,-1.2);
\draw[pattern=dots](0.65,-1) rectangle (1,-1.2);
\draw (.75,-1) node[above] {2};
\draw[fill=black](1,-1) rectangle (2,-1.2);
\draw[pattern=dots](2,-1) rectangle (2.3,-1.2);
\draw (2.15,-1) node[above] {0};
\draw[fill=black](2.3,-1) rectangle (2.65,-1.2);
\draw[pattern=dots](2.65,-1) rectangle (3,-1.2);
\draw (2.75,-1) node[above] {2};
\draw[fill=black] (3,-1) rectangle (6,-1.2);
\draw[pattern=dots](6,-1) rectangle (6.3,-1.2);
\draw (6.15,-1) node[above] {0};
\draw[fill=black](6.3,-1) rectangle (6.65,-1.2);
\draw[pattern=dots](6.65,-1) rectangle (7,-1.2);
\draw (6.85,-1) node[above] {2};
\draw[fill=black](7,-1) rectangle (8,-1.2);
\draw[pattern=dots](8,-1) rectangle (8.3,-1.2);
\draw (8.15,-1) node[above] {0};
\draw[fill=black](8.3,-1) rectangle (8.65,-1.2);
\draw[pattern=dots](8.65,-1) rectangle (9,-1.2);
\draw (8.85,-1) node[above] {2};
\draw (4,-2) node { Points of the Cantor Set pass through sieves};
\end{tikzpicture}\end{center}\end{document}

答案1

由于数字和填充方块的分布中存在某种可见的模式,因此我过度使用了\foreach

我调整了坐标系,使得该图中的条形的全长为9cm,即。1 = 9cm条形分别位于y = 1,2,3。条形高度为.2。此外,所有条形的图案颜色均设置为black(默认)。

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[x=9cm]
  % Squares
  \foreach \i in {1,2,3} {
    \filldraw[pattern=dots] (0,\i) rectangle (1,\i+.2);
    \filldraw (1/3,\i) rectangle (2/3,\i+.2);
  \ifnum\i<3
    \foreach \j in {1,7} {
      \filldraw (\j/9,\i) rectangle (\j/9+1/9,\i+.2);
    }
  \fi
  \ifnum\i<2
    \foreach \j in {1,7,19,25} {
      \filldraw (\j/27,1) rectangle (\j/27+1/27,1+.2);
    }
  \fi
  }
  % Nodes
  \foreach \i in {1,2,3} {
    \node[above] at (1/3^\i-1/2/3^\i,4-\i+.2) {0};
    \node[above] at (3/3^\i-1/2/3^\i,4-\i+.2) {2};
  \ifnum\i>1
    \node[above] at (7/3^\i-1/2/3^\i,4-\i+.2) {0};
    \node[above] at (9/3^\i-1/2/3^\i,4-\i+.2) {2};
  \fi
  \ifnum\i>2
    \node[above] at (19/3^\i-1/2/3^\i,1+.2) {0};
    \node[above] at (21/3^\i-1/2/3^\i,1+.2) {2};
    \node[above] at (25/3^\i-1/2/3^\i,1+.2) {0};
    \node[above] at (27/3^\i-1/2/3^\i,1+.2) {2};
  \fi
  }
  \node at (1/2,0) {Points of the Cantor Set pass through sieves};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

递归怎么样pic?:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{patterns}
\tikzset{pics/.cd,
  cantor set/.style args={#1 by #2 order #3}{
    code={
      \pgfmathparse{int(#3)}\let\C=\pgfmathresult
      \draw [pattern=dots] (0,0) rectangle (#1,#2);
      \ifnum\C>0
        \fill (#1*1/3,0) rectangle (#1*2/3,#2);      
        \ifnum\C=1\relax
          \node [above] at (#1*1/6,#2) {0};
          \node [above] at (#1*5/6,#2) {2};
        \else
          \pic               {cantor set={#1/3 by #2 order #3-1}};
          \pic at (#1*2/3,0) {cantor set={#1/3 by #2 order #3-1}};
        \fi
      \fi
    }
  },
}
\begin{document}
\begin{tikzpicture}

\pic at (0,3) {cantor set={8cm by .25cm order 1}};
\pic at (0,2) {cantor set={8cm by .25cm order 2}};
\pic at (0,1) {cantor set={8cm by .25cm order 3}};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

以下是一个简单的例子:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
    \usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}

\draw[pattern=dots] (0,1) rectangle (3,1.2);
\draw (1.5,1.3) node[above] {0};
\draw[fill] (3,1) rectangle (6,1.2);
\draw[pattern=dots,pattern color=blue] (6,1) rectangle (9,1.2);
\draw (7,1.3) node[above] {2};

\foreach \i in {0.2,-1}{
    \draw[pattern=dots] (0,\i-0.2) rectangle (9,\i);
    \draw[fill] (3,\i-0.2) rectangle (6,\i);
    \draw[fill] (1,\i-0.2) rectangle (2,\i);
    \draw[fill] (7,\i-0.2) rectangle (8,\i);
}

% Note: I assumed the one node with height 0.3 was intended to be 0.2 like the others
\foreach \j in{0,1,3,4}{
    \draw(2*\j+0.5,0.2) node[above] {\pgfmathparse{iseven(\j) ? 0 : 2}\pgfmathresult};
}

% Note: I assumed the nodes at position *.75 were intended to be placed at *.85, like on the right side
\foreach \m in {0,2,6,8}{
    \draw[fill] (\m+0.3,-1) rectangle (\m+0.65,-1.2);
    \foreach \n in {0.15,0.85}{
        \draw(\m+\n,-1) node[above] {\pgfmathparse{\n==0.15 ? 0 : 2}\pgfmathresult};
    }
}

\draw (4,-2) node { Points of the Cantor Set pass through sieves};

\end{tikzpicture}

\end{document}

这应该比您的示例更短,而且结构更好。我基本保留了最上面的一行不变,因为您更改了图案的颜色,并且相当随意地放置了数字 2。

简化代码的方法有很多种,但它们都归结为一件事:描述模式(tikz当然不是装饰,而是图像中发生的事情)。

关于代码的几点说明:

  • 您不必绘制许多小虚线矩形,而是可以绘制一个长矩形,然后在上面绘制黑色矩形。

  • 黑色是默认颜色。除非您指定了其他颜色作为默认颜色,否则无需指定黑色作为填充颜色。

  • 您的图像中存在一定数量的重复。因此,可以使用一些循环和一些数学运算来减少代码。

代码可以进一步简化,但这个就留给你了。

有关命令的更多信息,请参阅手册tikz。该\foreach命令在第 83 章(第 909-913 页)中描述,第 90 章(第 933-944 页)中可以找到许多数学表达式。我在节点表达式中使用的函数和运算符可以在第 934、939 和 942 页中找到。

相关内容