使用装饰命令更改康托集合中单个区间的颜色

使用装饰命令更改康托集合中单个区间的颜色

我有以下康托集的 TiKZ 图片。

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations.fractals}
\begin{document}
 \begin{tikzpicture}[decoration=Cantor set,line width=2mm]
      \draw (0,0) -- (12,0);
      \draw decorate{ (0,-.5) -- (12,-.5) };
      \draw decorate{ decorate{ (0,-1) -- (12,-1) }};
      \draw decorate{ decorate{ decorate{ (0,-1.5) -- (12,-1.5) }}};
      \draw decorate{ decorate{ decorate{ decorate{ (0,-2) -- (12,-2) }}}};
      \draw decorate{ decorate{ decorate{ decorate{ decorate{ (0,-2.5) -- (12,-2.5) }}}}};
      \draw decorate{ decorate{ decorate{ decorate{ decorate{ decorate{ (0,-3) -- (12,-3) }}}}}};
    \end{tikzpicture}
\end{document}

我怎样才能改变这张图片中单个间隔的颜色,比如说第四行左边第三个间隔?

答案1

在此处输入图片描述

您必须在构建康托集时进行数学运算。我稍微修改了您的代码并插入了一些参数;主要是\N = 3为了获得修改后的片段。事实上,我修改了两个片段来说明所使用的数学公式。

代码

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations.fractals}
\usetikzlibrary{math}
\begin{document}

\begin{tikzpicture}[evaluate={%
    real \L, \dy; \L = 12; \dy = -.5;
    integer \N; \N = 3;},
  decoration=Cantor set, line width=2mm]
  \draw (0, 0) -- ++(\L, 0);
  \draw decorate{ (0, 1*\dy) -- ++(\L, 0) };
  \draw decorate{
    decorate{ (0, 2*\dy) -- ++(\L, 0) }
  };
  \draw decorate{
    decorate{
      decorate{ (0, 3*\dy) -- ++(\L, 0) }
    }
  };
  \draw decorate{
    decorate{
      decorate{
        decorate{ (0, 4*\dy) -- ++(\L, 0) }
      }
    }
  };
  \draw decorate{
    decorate{
      decorate{
        decorate{
          decorate{ (0, 5*\dy) -- ++(\L, 0) }
        }
      }
    }
  };
  \draw decorate{
    decorate{
      decorate{
        decorate{
          decorate{
            decorate{ (0, 6*\dy) -- ++(\L, 0) }
          }
        }
      }
    }
  };
  \draw[red] ({\L*(2/pow(3, \N-1))}, \N*\dy)
  -- ++({\L*(1/pow(3, \N))}, 0);
  \draw[blue] ({\L*(2/pow(3, \N-1) +2/pow(3, \N))}, \N*\dy)
  -- ++({\L*(1/pow(3, \N))}, 0);
\end{tikzpicture}
\end{document}

相关内容