在tikz中為它們的交點繪製不同顏色的橢圓?

在tikz中為它們的交點繪製不同顏色的橢圓?

如何用不同的顏色為兩個橢圓的交點著色。例如,橢圓本身應著色為黃色,其交點應著色為紅色。下面給出了我繪製橢圓的程式碼

\draw (-4,0) ellipse (8 and 3);
\draw (8,0) ellipse (8 and 3);

更新:在對個人答案的評論中,我要求顯示三個省略號。

答案1

正如你所看到的這些範例和中如何在 LaTeX 中繪製維恩圖(尤其是補集),只要填滿顏色之一是白色,繪製這樣的橢圓就相當容易。

當您想要兩種不同的填充顏色並使用前面範例中描述的方法時,請確保先填充橢圓,然後再繪製它們:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \def\firstellipse{(-4,0) ellipse (8 and 3)}
    \def\secondellipse{(8,0) ellipse (8 and 3)}

    % colour ellipses
    \fill[yellow] \firstellipse \secondellipse;

    % colour intersection
    \begin{scope}
        \clip \firstellipse;
        \fill[red] \secondellipse;
    \end{scope}

    % outiline of ellipses
    \draw \firstellipse \secondellipse;
\end{tikzpicture}
\end{document}

在此輸入影像描述

編輯:回應您對三個省略號的評論請求

這個想法保持不變:

  1. 填寫你的省略號
  2. 填滿你的路口
  3. 畫出你的橢圓

所以這給了你:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \def\firstellipse{(-4,0) ellipse [x radius=8, y radius=3, rotate=150]}
    \def\secondellipse{(8,0) ellipse [x radius=8, y radius=3, rotate=30]}
    \def\thirdellipse{(2,-10.5) ellipse [x radius=8, y radius=3, rotate=90]}
    \def\boundingbox{(-12,-16) rectangle (16,3)}

    % fill ellipses
    \fill[yellow] \firstellipse \secondellipse \thirdellipse;

    % fill intersections
    % intersection of second and third
    \begin{scope}
        \clip \boundingbox \firstellipse;
        \clip \secondellipse;
        \fill[blue] \thirdellipse;
    \end{scope}
    % intersection of first and third
    \begin{scope}
        \clip \boundingbox \secondellipse;
        \clip \firstellipse;
        \fill[green] \thirdellipse;
    \end{scope}
    % intersection of first and second
    \begin{scope}
        \clip \boundingbox \thirdellipse;
        \clip \firstellipse;
        \fill[gray] \secondellipse;
    \end{scope}
    % intersection of first, second and third
    \begin{scope}
        \clip \firstellipse;
        \clip \secondellipse;
        \clip \thirdellipse;
        \fill[red] \boundingbox;
    \end{scope}

    % outline of ellipses
    \draw \firstellipse \secondellipse \thirdellipse;
\end{tikzpicture}
\end{document}

在此輸入影像描述

如果您希望所有交叉點都是紅色,請將顏色藍色、綠色和灰色變更為紅色。

如果你想要旋轉橢圓使用

\def\firstellipse{(-4,0) ellipse [x radius=8, y radius=3, rotate=45]}

答案2

\tikzfillbetween來自庫的命令的另一個選項pgfplot's fillbetween

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
  \begin{tikzpicture}
    \draw[name path=A, fill=red] (-4,0) ellipse (8 and 3);
    \draw[name path=B] (8,0) ellipse (8 and 3);
    \tikzfillbetween[of=A and B]{yellow};
  \end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

您可以先用紅色填滿一個橢圓,然後使用以下命令繪製(填滿)兩個橢圓even odd rule

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
  \begin{tikzpicture}
    \fill[red] (-4,0) ellipse (8 and 3);
    \draw[fill=yellow,even odd rule,line width=1pt] (-4,0) ellipse (8 and 3)
                                                    (8,0) ellipse (8 and 3);
  \end{tikzpicture}
\end{document}

在此輸入影像描述

答案4

這是一個嘗試元郵報(包含在 LuaLaTeX 程式中),適合對此感興趣的人。

請注意,要繪製與 MetaPost 的這種交集也不簡單。如果您查看下面程式中第一個橢圓的定義,

fullcircle rotated 90 xscaled 9cm yscaled 3cm shifted (3.2cm, 0);

然後注意該rotated 90部分。它看起來很奇怪,因為這意味著旋轉應用於圓,但如果你抑制這部分,沒有什麼會起作用的。解釋在於buildcycleMetaPost 巨集的複雜性(與漸近線的,順便說一下),對此進行了詳細討論在這個主題中

更新由於現在第二個橢圓的定義方式與之前不同(即現在透過旋轉第一個橢圓),因此rotated 90第一個橢圓定義中的部分變得不必要。我相應地簡化了程式碼。不過,buildcycle在處理此類任務時,最好記住巨集的局限性。

\documentclass{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}

color yellow; yellow = red+green;

def erase_and_fill expr pat = unfill pat; fill pat enddef;

path fig[];
fig1 = fullcircle xscaled 9cm yscaled 3cm shifted (3.2cm, 0); 
fig2 = fig1 rotated 180;

beginfig(1); 
    for i = 1, 2: erase_and_fill fig[i] withcolor yellow; endfor
    erase_and_fill buildcycle(fig1, fig2) withcolor red; 
    for i = 1, 2: draw fig[i]; endfor 
endfig;

\end{mplibcode}
\end{document}

在此輸入影像描述

現在有三個省略號,就像上面 Maarten DHondt 的例子一樣:

\documentclass{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}

color yellow; yellow = red+green;

def erase_and_fill expr pat = unfill pat; fill pat enddef;

path fig[];
fig1 = fullcircle xscaled 9cm yscaled 3cm shifted (4cm, 0) rotated 30; 
fig2 = fig1 rotated 120; 
fig3 = fig2 rotated 120; 

beginfig(1);
    for i = 1 upto 3: erase_and_fill fig[i] withcolor yellow; endfor
    erase_and_fill buildcycle(fig1, fig2) withcolor 0.7white;
    erase_and_fill buildcycle(fig2, fig3) withcolor green;
    erase_and_fill buildcycle(fig1, fig3) withcolor blue;
    erase_and_fill buildcycle(fig1, fig2, fig3) withcolor red; 
    for i = 1 upto 3: draw fig[i]; endfor
endfig;

\end{mplibcode}
\end{document}

在此輸入影像描述

相關內容