4

This code draws the following figure. Do you know a better way or a more elegant way to draw the partitionned filled ellipse than the one I used?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \coordinate (O) at (0,0);
    \coordinate (B1) at (1,0);
    \coordinate (B2) at (2,0);
    \coordinate (B3) at (3,0);
    \coordinate (B4) at (4,0);
    \draw[very thick] (O) circle (3cm and 1.5cm);

    \fill[fill=blue!60] (0,-0.5) circle (2.5cm and 0.75cm);
    \begin{scope}
        \path[clip] (B1) circle (3cm);
        \fill[fill=blue!20] (0,-0.5) circle (2.5cm and 0.75cm);
    \end{scope}
    \begin{scope}
        \path[clip] (B2) circle (3cm);
        \fill[fill=blue!60] (0,-0.5) circle (2.5cm and 0.75cm);
    \end{scope}
    \begin{scope}
        \path[clip] (B3) circle (3cm);
        \fill[fill=blue!20] (0,-0.5) circle (2.5cm and 0.75cm);
    \end{scope}
    \begin{scope}
        \path[clip] (B4) circle (3cm) ;
        \fill[fill=blue!60] (0,-0.5) circle (2.5cm and 0.75cm);
    \end{scope}
    \draw[color=blue] (0,-0.5) circle (2.5cm and 0.75cm);

    \begin{scope}
        \clip (O) circle (3cm and 1.5cm);
        \draw[thick] (B1) circle (3cm and 3cm);
        \draw[thick] (B2) circle (3cm and 3cm);
        \draw[thick] (B3) circle (3cm and 3cm);
        \draw[thick] (B4) circle (3cm and 3cm);
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

9

I don't know if it's better or more elegant but this is another option. It uses path picture and its clipping properties.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[stripe/.style n args={4}{%
    path picture={\draw[fill opacity=.5, fill=white] ([xshift=#1]path picture bounding box.north west) to[bend right] ([xshift=#1]path picture bounding box.south west) -- ([xshift=#2]path picture bounding box.south west) to[bend left] ([xshift=#2]path picture bounding box.north west) --cycle;
    \draw[fill opacity=.5, fill=white] ([xshift=#3]path picture bounding box.north west) to[bend right] ([xshift=#3]path picture bounding box.south west) -- ([xshift=#4]path picture bounding box.south west) to[bend left] ([xshift=#4]path picture bounding box.north west) --cycle;}}]

    \fill[fill=blue!60](0,-0.5) circle (2.5cm and 0.75cm);

    \draw[very thick, stripe={2cm}{3cm}{4cm}{5cm}] circle (3cm and 1.5cm);

\end{tikzpicture}
\end{document}

enter image description here

1
  • That's a much more compact way to do it. I didn't know about path picture. Thanks.
    – rahotep
    Sep 18 at 22:57
2

Explaination of main code

import animation;

settings.tex="pdflatex";
settings.outformat="pdf";

animation Anim;
unitsize(1cm);
pair O=(0,0),B1=(1,0),B2=(2,0),B3=(3,0),B4=(4,0);
Anim.add();
path g=scale(3,1.5)*unitcircle;
draw(g);
Anim.add();

path h=scale(2.5,0.75)*shift((0,-0.5))*unitcircle;
fill(h,blue);
Anim.add();

path b1=lastcut(firstcut(circle(B1,3),g).after,g).before;
path b2=lastcut(firstcut(circle(B2,3),g).after,g).before;
path b3=lastcut(firstcut(circle(B3,3),g).after,g).before;
path b4=lastcut(firstcut(circle(B4,3),g).after,g).before;

path e1=subpath(h,reltime(h,0),reltime(h,0.5));
path e2=subpath(h,reltime(h,0.5),reltime(h,1));

draw(e1,red+0.8bp,Arrow);
Anim.add();
draw(b2);
Anim.add();
dot(intersectionpoint(e1,b2,sqrtEpsilon));
Anim.add();

draw(e2,green+0.8bp,Arrow);
Anim.add();
draw(b1);
Anim.add();
dot(intersectionpoint(e2,b1,sqrtEpsilon));
Anim.add();

path s1=buildcycle(b2,e1,b1,e2);
fill(s1,gray);
Anim.add();
label("$s1$",0.5*(intersectionpoint(e1,b2,sqrtEpsilon)+intersectionpoint(e2,b1,sqrtEpsilon)));
Anim.add();

draw(b4);
Anim.add();
dot(intersectionpoint(e1,b4,sqrtEpsilon));
Anim.add();

draw(b3);
Anim.add();
dot(intersectionpoint(e2,b3,sqrtEpsilon));
Anim.add();

path s2=buildcycle(b4,e1,b3,e2);
fill(s2,gray);
Anim.add();
label("$s2$",0.5*(intersectionpoint(e1,b4,sqrtEpsilon)+intersectionpoint(e2,b3,sqrtEpsilon)));
Anim.add();

erase();
Anim.movie();

enter image description here

Compile with Asymptote or http://asymptote.ualberta.ca/.

Main code

unitsize(1cm);
pair O=(0,0),B1=(1,0),B2=(2,0),B3=(3,0),B4=(4,0);

// \draw[very thick] (O) circle (3cm and 1.5cm);
path g=scale(3,1.5)*unitcircle;
draw(g);

// \fill[fill=blue!60] (0,-0.5) circle (2.5cm and 0.75cm);
path h=scale(2.5,0.75)*shift((0,-0.5))*unitcircle;
fill(h,blue);

path b1=lastcut(firstcut(circle(B1,3),g).after,g).before;
path b2=lastcut(firstcut(circle(B2,3),g).after,g).before;
path b3=lastcut(firstcut(circle(B3,3),g).after,g).before;
path b4=lastcut(firstcut(circle(B4,3),g).after,g).before;

path e1=subpath(h,reltime(h,0),reltime(h,0.5));
path e2=subpath(h,reltime(h,0.5),reltime(h,1));

path s1=buildcycle(b2,e1,b1,e2);
fill(s1,gray);

path s2=buildcycle(b4,e1,b3,e2);
fill(s2,gray);

draw(h^^b1^^b2^^b3^^b4);

enter image description here

Explanation:

path buildcycle(... path[] p);

This returns the path surrounding a region bounded by a list of two or more consecutively intersecting paths, following the behaviour of the MetaPost buildcycle command.

See also A User's Manual for MetaPost, 9.1 Building Cycles

Two examples about buildcycle:

1.

import patterns;
size(7cm,0);
path line1=(-3,3){dir(0)}..(-2,3)..{dir(0)}(4,-2), line2=(-3.5,-3){dir(60)}..(-3,-1)..{dir(30)}(3,3),
     line3=(-4,-2){dir(30)}..(-2,-1)..{dir(45)}(3,0);
pair inter12=intersectionpoint(line1,line2),    inter13=intersectionpoint(line1,line3),
     inter23=intersectionpoint(line2,line3);
add("AinterB",crosshatch(2mm,green));
fill(buildcycle(line1,line2,line3),pattern("AinterB"));
draw(line1^^line2^^line3);
pair[] dot={(-3,3),(4,-2),(-3.5,-3),(3,3),(-4,-2),(3,0)};
for (int i=0;i<=4;i+=2){ dot("A"+string(i),dot[i],W,orange);}
for (int i=1;i<=5;i+=2){ dot("B"+string(i),dot[i],E,yellow);}
dot("inter12",inter12,1.5*dir(0),blue); 
dot("inter13",inter13,1.5*dir(0),red); 
dot("inter23",inter23,1.5*dir(-20),gray);
shipout(bbox(1mm,white));

enter image description here

2.

unitsize(1cm);
path [] c; path [] h;
c[1] = (-2,0){(1,1)}..{(1,-1)}(2,0);
h[1] = shift(0,-1.8)*c[1];
h[2] = shift(1.8,0)*rotate(90)*c[1];
h[3] = shift(0,1.8)*rotate(180)*c[1];
h[4] = shift(-1.8,0)*rotate(-90)*c[1];
draw(h[1]^^h[2]^^h[3]^^h[4],grey);
c[5] = buildcycle(h[1],h[2],h[3],h[4]);
fill(c[5], lightgrey);
draw(c[5], .8red+1bp);
for(int i=0; i<length(c[5]); ++i){
dot(string(i),point(c[5],i),2*N, blue+3bp);
}
shipout(bbox(1mm,white));

enter image description here

An example which is relative to lastcut,firstcut.

https://en.wikipedia.org/wiki/MetaPost, the third example.

// Đối với whatever, xem 8.20 MetaPost (the main documentation).

size(7cm);
pair z1=(0,0),z2=z1+2up;
pair z3=extension(z1,z1+10*dir(60),z2,z2+10*dir(-50));
pair z4=z3+(-1.5,-.5), z5=z1+dir(135);
pair z0=extension(z1,z2,z3,z4);

// fullcircle - Circle of diameter 1 centered on (0, 0)
path p0=shift(z0)*rotate(45)*yscale(0.5)*circle((0,0),0.5);
path p1=z2---z4..z0..z3---z1;
path p2=lastcut(firstcut(p1,p0).after,p0).before;
path p3=lastcut(firstcut(p0,p1).after,p1).before;

path p4=p2&p3&cycle;
fill(p4,rgb(1,1,0.2));
draw(p0,linetype(new real[] {0,3})+1.5bp);
draw(p1,linetype(new real[] {5,5})+bp);
draw(p2,blue+1.5bp);
draw(p3,red+1.5bp);
draw(z1--z2,.5white);
draw(z3--z4,.5white);
dot("$z_0$",z0,dir(-45));
dot("$z_1$",z1,dir(-135));
dot("$z_2$",z2,dir(90));
dot("$z_3$",z3);
dot("$z_4$",z4,dir(180));

enter image description here

2
\documentclass[tikz, border=1 cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\coordinate (B1) at (1,0);
\coordinate (B2) at (2,0);
\coordinate (B3) at (3,0);
\coordinate (B4) at (4,0);
\fill[blue!60] (0,-0.5) ellipse[x radius=2.5 cm, y radius=0.75 cm];
\begin{scope}[overlay]
\clip (B1) circle[radius=3 cm] (B2) circle[x radius=-3 cm, y radius=3 cm];
\fill[blue!20] (0,-0.5) ellipse[x radius=2.5 cm, y radius=0.75 cm];
\end{scope}
\begin{scope}[overlay]
\clip (B3) circle (3cm) (B4) circle[x radius=-3 cm, y radius=3 cm];
\fill[blue!20] (0,-0.5) ellipse[x radius=2.5 cm, y radius=0.75 cm];
\end{scope}
\draw[blue] (0,-0.5) ellipse[x radius=2.5 cm, y radius=0.75 cm];
\begin{scope}[overlay]
\clip (O) ellipse[x radius=3 cm, y radius=1.5 cm];
\draw[thick] (B1) circle[radius=3 cm];
\draw[thick] (B2) circle[radius=3 cm];
\draw[thick] (B3) circle[radius=3 cm];
\draw[thick] (B4) circle[radius=3 cm];
\end{scope}
\draw[very thick] (O) ellipse[x radius=3 cm, y radius=1.5 cm];    
\end{tikzpicture}
\end{document}

Ellipse with collored areas

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.