我有以下 Cantor Set 的 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}