带有一个或两个环的彩色扇区

带有一个或两个环的彩色扇区

我有这个代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        \draw[fill=red] (0,0)--(0:6) arc (0:60:6)--cycle;
        \draw[fill=white] (0,0)--(0:5) arc (0:60:5)--cycle;
        \draw[fill=green] (0,0)--(0:3.5) arc (0:60:3.55)--cycle;
        
        \draw[fill=red] (0,0)--(60:6) arc (60:120:6)--cycle;
        \draw[fill=green] (0,0)--(60:5) arc (60:120:5)--cycle;
        \draw[fill=white] (0,0)--(60:3.5) arc (60:120:3.55)--cycle;
        
        \draw[fill=white] (0,0)--(120:6) arc (120:180:6)--cycle;
        \draw[fill=green] (0,0)--(120:5) arc (120:180:5)--cycle;
        \draw[fill=red] (0,0)--(120:3.5) arc (120:180:3.55)--cycle;
        
        \draw[fill=white] (0,0)--(180:6) arc (180:240:6)--cycle;
        \draw[fill=red] (0,0)--(180:5) arc (180:240:5)--cycle;
        \draw[fill=green] (0,0)--(180:3.5) arc (180:240:3.55)--cycle;
        
        
        \draw[fill=green] (0,0)--(240:6) arc (240:300:6)--cycle;
        \draw[fill=red] (0,0)--(240:5) arc (240:300:5)--cycle;
        \draw[fill=white] (0,0)--(240:3.5) arc (240:300:3.55)--cycle;
        
        \draw[fill=green] (0,0)--(300:6) arc (300:360:6)--cycle;
        \draw[fill=white] (0,0)--(300:5) arc (300:360:5)--cycle;
        \draw[fill=red] (0,0)--(300:3.5) arc (300:360:3.55)--cycle;
    \end{tikzpicture}
\end{document}

输出如下:

在此处输入图片描述

问题:使用一个或两个循环可以获得相同的输出吗?

编辑:如果您希望每个彩色扇区具有相同的面积(2pi),则需要这 3 个半径:2sqrt(3)、sqrt(24)和 6。

答案1

我喜欢使用简短的规范,而不是把所有内容都拼写出来,这就是为什么我使用了一些智能键设置,以便它们自动定义我们可以在路径上使用的另一种样式。这允许使用gnr, ngr, rgn, grn, nrg, rng代表g绿色、r红色和n无的短循环列表。

ring or pie使用一次(因为即使是三个环也可以放入 foreach 循环中)但我相信无论如何这个键都很方便……第二篇文档展示了我们如何使用它来实现更灵活的绘图而不需要更改太多代码。

代码

\documentclass[tikz]{standalone}
\tikzset{
  set colors/.style n args={3}{
    /tikz/@set color/#1={1}, /tikz/@set color/#2={2}, /tikz/@set color/#3={3}},
  /utils/temp/.style 2 args={@set color/#1/.style={/tikz/color ##1/.style={fill=#1#2}}},
  /utils/temp/.list={red, green, none}}
\begin{document}
\tikz[
  ring or pie/.style n args={3}{
    insert path={   (60*#1   :#3) arc[forward =#1, radius=#3]
                 -- (60*#1+60:#2) arc[backward=#1, radius=#2] -- cycle}},
  forward/.style={start angle=60*#1, delta angle=60},
  backward/.style={end angle=60*#1, delta angle=-60}]
\foreach[count=\i from 0] \c in {gnr, ngr, rgn, grn, nrg, rng}
  \foreach[
    /tikz/set colors/.expand once=\c, count=\j,
    remember=\r as \prevR (initially 0)]\r in {3.5, 5, 6}
    \draw[color \j, ring or pie={\i}{\prevR}{\r}];
\end{document}

输出

在此处输入图片描述


我们可以将这种方法扩展为三种以上的颜色和一个包含六个饼图的轮子。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns, shadings}
\tikzset{
  new color shortcut/.style n args={3}{
    #1 color/#2/.style={/tikz/use #1 color/##1/.style={#3}}},
  set color shortcuts/.style 2 args={% draw and text, too
    new color shortcut={draw}{#1}{draw={#1#2}},
    new color shortcut={text}{#1}{text={#1#2}},
    new color shortcut={fill}{#1}{fill={#1#2}}},
  /utils/temp/.style={
    use #1 color/.is choice,
    #1 colors/.code=
      \def\pgfmathcounter{0}\pgfkeysalso{/tikz/@#1 colors={##1\relax}},
    @#1 colors/.code 2 args={%
      \edef\pgfmathcounter{\pgfinteval{\pgfmathcounter+1}}%
      \pgfkeysalso{/tikz/#1 color/##1=\pgfmathcounter}%
      \ifx##2\relax\else\pgfkeysalso{/tikz/@#1 colors={##2}}\fi}},
  /utils/temp/.list={draw, fill, color},
  wheel of rings and pies/.style={
    forward/.style={start angle=360/#1*##1, delta angle= 360/#1},
    backward/.style={end  angle=360/#1*##1, delta angle=-360/#1},
    ring or pie/.style n args={3}{insert path={(360/#1*##1:##3)
      arc[forward=##1, radius=##3] --(360/#1*##1+360/#1:##2)
      arc[backward=##1, radius=##2] -- cycle}}}}
\begin{document}
\tikz[wheel of rings and pies=6, set color shortcuts/.list={red, green, none}]
\foreach[count=\i from 0] \c in {gnr, ngr, rgn, grn, nrg, rng}
  \foreach[
    /tikz/fill colors/.expand once=\c, count=\j,
    remember=\r as \prevR (initially 0)]\r in {3.5, 5, 6}
    \draw[use fill color/.expanded=\j, ring or pie={\i}{\prevR}{\r}];
\tikz[
  wheel of rings and pies=24,
  set color shortcuts/.list={yellow, cyan, magenta},
  new color shortcut=
    {fill}{X}{preaction={ball color=gray}, pattern=bricks, pattern color=black}]
\foreach[count=\i from 0]\c in {ycmX, cymX, mycX, ymcX, cmyX, mcyX,
  mcXy, cmXy, Xmcy, mXcy, cXmy, Xcmy, Xymc, yXmc, mXyc, Xmyc, ymXc, myXc,
  cyXm, ycXm, Xcym, cXym, yXcm, Xycm}
  \foreach[
    /tikz/fill colors/.expand once=\c, count=\j,
    remember=\r as \prevR (initially 0)]\r in {1, 2, 3, 4}
    \draw[use fill color/.expanded=\j, ring or pie={\i}{\prevR}{\r}];
\end{document}

输出(仅第二张图)

在此处输入图片描述

答案2

在对 Qrrbrbirlbel 的回答进行一番思考之后,我使用一个循环(4 个变量)对我的问题进行了简短的解决方案:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        \foreach \ang/\c/\d/\f in {0/green/white/red,60/white/green/red,120/red/green/white,180/green/red/white,240/white/red/green,300/red/white/green}{%
            \pgfmathsetmacro{\Ang}{\ang+60}
            \draw[fill=\f] (0,0)--(\ang:6) arc (\ang:\Ang:6)--cycle;
            \draw[fill=\d] (0,0)--(\ang:4.9) arc (\ang:\Ang:4.9)--cycle;
            \draw[fill=\c] (0,0)--(\ang:3.464) arc (\ang:\Ang:3.464)--cycle;
            }
    \end{tikzpicture}
\end{document}

输出还显示所有扇区的相同区域(未请求):

在此处输入图片描述

相关内容