TikZ - 四色圆圈分割

TikZ - 四色圆圈分割

按照@Alain Matthes 的回复这个问题,wa可以得到这样的图片:

在此处输入图片描述

我不擅长修改代码以将圆分成四个部分。你能帮助我吗?

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds,calc}

% Two Colored Circle Split 
\makeatletter
\tikzset{circle split part fill/.style  args={#1,#2}{%
 alias=tmp@name, 
  postaction={%
    insert path={
     \pgfextra{% 
     \pgfpointdiff{\pgfpointanchor{\pgf@node@name}{center}}%
                  {\pgfpointanchor{\pgf@node@name}{east}}%            
     \pgfmathsetmacro\insiderad{\pgf@x}
      \fill[#1] (\pgf@[email protected]) ([xshift=-\pgflinewidth]\pgf@[email protected]) arc
                          (0:180:\insiderad-\pgflinewidth)--cycle;
      \fill[#2] (\pgf@[email protected]) ([xshift=\pgflinewidth]\pgf@[email protected])  arc
                           (180:360:\insiderad-\pgflinewidth)--cycle;            
         }}}}}  
 \makeatother  


\begin{document}

\begin{tikzpicture}
  \node[minimum size=4cm, 
    shape=circle split,
    draw=blue!70,line width=2mm,
    circle split part fill={blue!50, red!50}
    ] at (0.5, 1.5) {1\nodepart{lower} 2};
\end{tikzpicture}

\end{document}

答案1

以下代码定义了一个circle cross split由节点部分及其简写组成的形状

  • text
  • two= tr
  • three=br
  • four= bl

它需要shapes.multipart库(还提供\let相应简写的 PGF 节点部分框)并且需要:

  • 列表处理器\pgf@lib@sh@rs@process@list
  • \pgf@lib@sh@getalpha和将、、 …\pgf@lib@sh@toalpha转换为、、 …;12onetwo
  • \pgf@lib@sh@rs@nonetext它仅包含以下内容none,以便人们可以有选择地禁用其中一个季度的填充。

文本框是“居中对齐”的。这意味着文本框的一个角将填充到十字形的中心角(包括内部分隔符)。

两个上部部分和两个下部部分分别基底对齐。

进一步改进:

  • 对齐位于彼此下方/上方的节点部分的选项。
  • 第三个示例和第二个示例inner sep=+0pt表明,文本框的尺寸在计算圆的半径时并没有真正被正确使用。如果内部分隔符为空,形状边框将穿过框。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.multipart}
\makeatletter
\let\pgfnodeparttrbox\pgfnodeparttwobox
\let\pgfnodepartblbox\pgfnodepartthreebox
\let\pgfnodepartbrbox\pgfnodepartfourbox
\newif\ifpgfcirclecrosssplitcustomfill
\tikzset{%
  circle cross split part fill/.code=%
    \def\pgf@lib@sh@ccs@list@fill{#1}\pgfcirclecrosssplitcustomfilltrue,%
  circle cross split uses custom fill/.is if=pgfcirclecrosssplitcustomfill}
\pgfdeclareshape{circle cross split}{%
  \nodeparts{text,two,three,four}%
  \savedanchor\centerpoint{%
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
    \pgf@x =\wd\pgfnodeparttextbox
    \pgf@yb=\dp\pgfnodeparttextbox
    \pgf@y=\dp\pgfnodeparttwobox
    \ifdim\pgf@yb>\pgf@y
      \pgf@y=\pgf@yb
    \fi
    \advance\pgf@x\pgf@xa
    \advance\pgf@y-\pgf@ya
    \advance\[email protected]\pgflinewidth
    \advance\[email protected]\pgflinewidth
  }%
  \savedanchor\twoanchor{%
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
    \advance\[email protected]\pgflinewidth
    \advance\pgf@x\pgf@xa
    \advance\[email protected]\pgflinewidth
    \advance\pgf@y\pgf@ya
    \pgf@yb\dp\pgfnodeparttextbox
    \pgf@yc\dp\pgfnodeparttwobox
    \ifdim\pgf@yb>\pgf@yc
      \pgf@yc\pgf@yb
    \fi
    \advance\pgf@y\pgf@yc
  }%
  \savedanchor\threeanchor{%
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
    \pgf@x\wd\pgfnodeparttextbox
    \pgf@yb\dp\pgfnodeparttextbox
    \pgf@yc\dp\pgfnodeparttwobox
    \ifdim\pgf@yb>\pgf@yc
      \pgf@yc\pgf@yb
    \fi
    \advance\pgf@y-\pgf@yc
    \advance\pgf@y-2\pgf@ya
    \advance\pgf@y-\pgflinewidth
    \pgf@yb\ht\pgfnodepartthreebox
    \pgf@yc\ht\pgfnodepartfourbox
    \ifdim\pgf@yb>\pgf@yc
      \pgf@yc\pgf@yb
    \fi
    \advance\pgf@y-\pgf@yc
    \advance\pgf@x-\wd\pgfnodepartthreebox
  }%
  \savedanchor\fouranchor{%
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
    \advance\pgf@x\wd\pgfnodepartthreebox
    \advance\pgf@x2\pgf@xa
    \advance\pgf@x\pgflinewidth
  }%
  \saveddimen\radius{%
    % height:
    \pgf@y\ht\pgfnodeparttextbox
    \pgf@yb\ht\pgfnodeparttwobox
    \ifdim\pgf@yb>\pgf@y
      \pgf@y\pgf@yb
    \fi
    \pgf@yc\dp\pgfnodeparttextbox
    \pgf@yb\dp\pgfnodeparttwobox
    \ifdim\pgf@yc>\pgf@yb
      \advance\pgf@y\pgf@yc
    \else
      \advance\pgf@y\pgf@yb
    \fi
    \pgf@yb\ht\pgfnodepartthreebox
    \ifdim\pgf@yb<\ht\pgfnodepartfourbox
      \pgf@yb\ht\pgfnodepartfourbox
    \fi
    \pgf@yc\dp\pgfnodepartthreebox
    \ifdim\pgf@yc<\dp\pgfnodepartfourbox
      \advance\pgf@yb\dp\pgfnodepartfourbox
    \else
      \advance\pgf@yb\pgf@yc
    \fi
    \ifdim\pgf@yc>\pgf@y
      \pgf@y\pgf@yc
    \fi
    \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
    \advance\pgf@y2\pgf@ya
    %
    \pgf@x\wd\pgfnodeparttextbox
    \pgf@xa\wd\pgfnodepartthreebox
    \pgf@xb\wd\pgfnodeparttwobox
    \pgf@xc\wd\pgfnodepartfourbox
    \ifdim\pgf@xa>\pgf@x
      \pgf@x\pgf@xa
    \fi
    \ifdim\pgf@xb>\pgf@x
      \pgf@x\pgf@xb
    \fi
    \ifdim\pgf@xc>\pgf@x
      \pgf@x\pgf@xc
    \fi
    \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
    \advance\pgf@x2\pgf@xa
    \ifdim\pgf@y>\pgf@x
      \pgf@x\pgf@y
    \fi
    \advance\[email protected]\pgflinewidth
    %
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/minimum width}}%
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/minimum height}}%
    \ifdim\pgf@x<.5\pgf@xb
        \pgf@x=.5\pgf@xb
    \fi
    \ifdim\pgf@x<.5\pgf@yb
        \pgf@x=.5\pgf@yb
    \fi
    %
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%
    \ifdim\pgf@xb<\pgf@yb
      \advance\pgf@x\pgf@yb
    \else
      \advance\pgf@x\pgf@xb
    \fi
  }%
  \inheritanchorborder[from=circle]%
  \inheritanchor[from=circle]{north}%
  \inheritanchor[from=circle]{north west}%
  \inheritanchor[from=circle]{north east}%
  \inheritanchor[from=circle]{center}%
  \inheritanchor[from=circle]{west}%
  \inheritanchor[from=circle]{east}%
  \inheritanchor[from=circle]{mid}%
  \inheritanchor[from=circle]{mid west}%
  \inheritanchor[from=circle]{mid east}%
  \inheritanchor[from=circle]{base}%
  \inheritanchor[from=circle]{base west}%
  \inheritanchor[from=circle]{base east}%
  \inheritanchor[from=circle]{south}%
  \inheritanchor[from=circle]{south west}%
  \inheritanchor[from=circle]{south east}%
  \anchor{two}{\twoanchor}%
  \anchor{three}{\threeanchor}%
  \anchor{four}{\fouranchor}%
  \inheritbackgroundpath[from=circle]
  \beforebackgroundpath{%
    \pgfutil@tempdima=\radius
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%
    \ifdim\pgf@xb<\pgf@yb
      \advance\pgfutil@tempdima by-\pgf@yb
    \else
      \advance\pgfutil@tempdima by-\pgf@xb
    \fi
    \advance\pgfutil@tempdima by-.5\pgflinewidth%
    \pgfsetshortenstart{0pt}%
    \pgfsetshortenend{0pt}%
    \pgfsetarrows{-}%
    \pgfpathmoveto
      {\pgfpointadd{\centerpoint}{\pgfqpoint{-\pgfutil@tempdima}{0pt}}}%
    \pgfpathlineto
      {\pgfpointadd{\centerpoint}{\pgfqpoint{\pgfutil@tempdima}{0pt}}}%
    \pgfpathmoveto
      {\pgfpointadd{\centerpoint}{\pgfqpoint{0pt}{-\pgfutil@tempdima}}}%
    \pgfpathlineto
      {\pgfpointadd{\centerpoint}{\pgfqpoint{0pt}{\pgfutil@tempdima}}}%
    \pgfusepathqstroke
  }%
  \behindbackgroundpath{%
    \pgfutil@tempdima=\radius
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%  
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%  
    \ifdim\pgf@xb<\pgf@yb
      \advance\pgfutil@tempdima by-\pgf@yb
    \else
      \advance\pgfutil@tempdima by-\pgf@xb
    \fi
    \advance\pgfutil@tempdima by-.5\pgflinewidth%
    \ifpgfcirclecrosssplitcustomfill%
      \pgf@lib@sh@rs@process@list{\pgf@lib@sh@ccs@list@fill}{4}%
      {%
        \pgfmathloop
           \ifnum\pgfmathcounter>4%
           \else%
             \pgf@lib@sh@getalpha\pgf@lib@sh@rs@number{\pgfmathcounter}%
              \edef\pgf@tempa
                {\csname pgf@lib@sh@rs@\pgf@lib@sh@rs@number @item\endcsname}%
              \ifx\pgf@tempa\pgf@lib@sh@rs@nonetext\else
                \pgfsetfillcolor{\pgf@tempa}%
                \pgf@lib@sh@ccs@angles{\pgfmathcounter}%
                \pgfpathmoveto{\centerpoint}%
                \pgfpathlineto{\pgfpointadd{\centerpoint}
                  {\pgfqpointpolar{\pgf@lib@sh@ccs@angle}{\pgfutil@tempdima}}}%
                \pgfpatharc{\pgf@lib@sh@ccs@angle}{\pgf@lib@sh@ccs@angle@}
                  {\pgfutil@tempdima}%
                \pgfpathclose
                \pgfusepathqfill
              \fi
        \repeatpgfmathloop
      }%
    \fi}}
\def\pgf@lib@sh@ccs@angles#1{%
  \ifcase#1\or\def\pgf@lib@sh@ccs@angle{90}%
           \or\def\pgf@lib@sh@ccs@angle{0}%
           \or\def\pgf@lib@sh@ccs@angle{180}%
           \else\def\pgf@lib@sh@ccs@angle{270}%
  \fi
  \edef\pgf@lib@sh@ccs@angle@{\number\numexpr\pgf@lib@sh@ccs@angle+90\relax}}
\makeatother
\begin{document}
\tikz {
  \node at (3,2) [
    line width=6pt,
    circle cross split,
    draw=gray!50,
    minimum size=5cm,
    circle cross split part fill={green,blue,red,yellow!90!black}
  ] (s) {text \nodepart{two} two \nodepart{three} three \nodepart{four} four};
\foreach \anchor/\placement in {
    north west/above left, north/above, north east/above right,
    west/left, center/left, east/right,
    mid west/left, mid/above, mid east/right,
    base west/left, base/below, base east/right,
    south west/below left, south/below, south east/below right,
    text/left, two/below right, three/below, four/below right}
  \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
    node[\placement] {\scriptsize\texttt{(s.\anchor)}};
}

\fboxsep=-.4pt
\fboxrule=.4pt
\tikz
  \node [
    circle cross split,
    draw,
    circle cross split part fill={green,blue,red,yellow!90!black},
%    inner sep=+0pt
  ] (s) {\fbox{text} \nodepart{tr} \fbox{two} \nodepart{bl} \fbox{three} \nodepart{br} \fbox{four}};

\tikz\node[opacity=.5,text=blue,draw,circle cross split,inner sep=+0pt] {
                \rule{4em}{4em}
  \nodepart{tr} \rule{4em}{4em}
  \nodepart{bl} \rule{4em}{4em}
  \nodepart{br} \rule{4em}{4em}};
\end{document}

输出

示例 2 和 3

在此处输入图片描述 在此处输入图片描述

示例 1(锚点)

在此处输入图片描述

答案2

技巧

\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\psset{dimen=medusa}
\begin{pspicture}(-3,-3)(3,3)
    \psclip{\pscircle{3}}
        \psframe*[linecolor=red](-3,0)(0,3)
        \psframe*[linecolor=green](0,0)(3,-3)
    \endpsclip
    \pscircle{3}
\end{pspicture}
\end{document}

蒂克兹

\documentclass[tikz,border=12pt]{standalone}
\begin{document}
\begin{tikzpicture}
    \clip (0,0) circle (3);
    \begin{scope}
        \fill[red] (-3,0) rectangle (0,3);
        \fill[green] (0,0) rectangle (3,-3);
    \end{scope}
    \draw (0,0) circle (3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容