9

How do I color intersection of two ellipses in different color. For instances, the ellipses should themselves be colored as yellow and their intersection as red. My code for drawing ellipses is given below

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

UPDATE: In the comments to individual answers, I have requested to show it for three ellipses.

10

As you can see in these examples and in How to draw Venn diagrams (especially: complements) in LaTeX, it is fairly easy to draw such an ellipses as long as one of your fill colours is white.

When you want 2 different fill colours and use the methods described in the previous examples, make sure to fill the ellipse first and draw them afterwards:

\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}

enter image description here

EDIT: in response to your comment request of three ellipses

The idea stays the same:

  1. fill your ellipses
  2. fill your intersections
  3. draw your ellipses

So this gives you:

\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}

enter image description here

If you want all the intersections to be red, change the colours blue, green and grey to red.

If you want rotated ellipses use

\def\firstellipse{(-4,0) ellipse [x radius=8, y radius=3, rotate=45]}
  • Is there a way to add a third ellipse with all their intersections in red? – dineshdileep Feb 20 '15 at 7:28
  • 1
    Yes there is. Check the edit of my answer. – Maarten Dhondt Feb 20 '15 at 12:39
  • Since you have some responses that seem to answer your question, please consider marking one of them as ‘Accepted’. – Maarten Dhondt Feb 21 '15 at 11:24
  • Sure, will do so. One of the respondents has asked for more time. I wish to accept the answer that has been most useful for me. I hope you can appreciate that. THanks for the help. – dineshdileep Feb 21 '15 at 13:54
9

Another option with \tikzfillbetween command from pgfplot's fillbetween library.

\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}

enter image description here

  • 1
    Is there a way to add a third ellipse with all their intersections in red? – dineshdileep Feb 20 '15 at 7:29
7

You can first fill one ellipse with red colour and the draw (with fill) two ellipses using 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}

enter image description here

  • Hi, can show this for three ellipses? – dineshdileep Feb 20 '15 at 6:52
6

Here is an attempt with MetaPost (included in a LuaLaTeX program), for those who are interested in it.

Note that is not straightforward either to draw this kind of intersection with MetaPost. If you look at the first ellipse's definition in the program below,

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

then notice the rotated 90 part. It looks weird since it means that a rotation is applied to a circle, but if you suppress this part, nothing would work at all. The explanation lies within the intricacies of the buildcycle macro of MetaPost (the same as Asymptote's, by the way), which were discussed in detail in this topic.

UPDATE Since the second ellipse is now defined in a different manner than previously (i.e. now by rotating the first one), it makes the rotated 90 part in the definition of the first ellipse unnecessary. I've simplified the code accordingly. Still, it is good to have in mind the limitation of the buildcycle macro when tackling this sort of task.

\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}

enter image description here

Now with three ellipses, like Maarten DHondt's example above:

\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}

enter image description here

  • Am not familiar with metapost, however this is definitely going to be useful for someone. Appreciate the help!! +1 – dineshdileep Feb 21 '15 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.