% Lines starting with % are comments. All other lines are commands
% (which must end with a semicolon).
% Every metapost file has one or more beginfig(n); COMMMANDS endfig(); figure
% creating blocks, with unique n's which determine the names of the output
% files, one for each figure.
% Running the Unix command "mpost ABVenn", uses this file, named ABVenn.mp,
% and creates three files named ABVenn.1 through ABVenn.3 which can be read
% into LaTeX to put the defined figures into the postscript file.
% See http://www.stat.cmu.edu/~hseltman/LatexTips.html#Meta for details
% on the LaTeX file.
% This file creates three increasingly complex versions of a Venn diagram.
% A fourth file demonstrates what is behind the cross-hatch simulation of the
% third file.
% ABVenn.1 is the simplest version (see VennTest.pdf for the output).
beginfig(1);
% Named paths must first be "declared". Paths are drawing objects that
% can be drawn without lifting the pen.
path a, b, aa, ab;
% The "fullcircle" is centered at coordinates (0,0) with diameter equal to
% 1 postscript point (1/72 inch). Here it is scaled to have diameter 2 cm
% with the same central location. Path a is defined, but nothing is drawn.
a = fullcircle scaled 2cm;
% Path b is defined as just like a, but shift 1cm rightward and 0cm upward.
b = a shifted (1cm,0cm);
% Path aa is the 2cm upper (by definition) half circle rotated 270 degrees
% counterclockwise. This is a half circle open to the left.
aa = halfcircle scaled 2cm rotated 270;
% Path ab is the closed figure that surrounds the inner area made when
% aa and b are drawn; this is the desired intersection of circles a and b.
ab = buildcycle(aa, b);
% Now circle a is drawn. The final coordinate area of the figure will be
% whatever is needed to hold all of the components.
draw a;
% Circle b is drawn on top of whatever has been already drawn (circle a).
% It is filled with a grey shade 80% of the way from white to black.
% (Named colors such as red or blue are allowed here.) The part of
% circle b that overlaps circle a wipes out the a curve in that area.
fill b withcolor 0.8white;
fill ab withcolor 0.6white;
% Three points are defined for the purpose of constructing a curved arrow.
% It is not necessary to define these; you could put their coordinates
% directly into the following commands. The points needed for defining
% a curve or curved arrow are the starting point, a middle point, and
% an end point. Metapost makes a "nice" shape through all three points.
% By changing the position of the middle point, you change the shape of
% the curved arrow.
z0=(1.6cm,1.5cm);
z1=(0.9cm,1.2cm);
z2=(0.5cm,0.6cm);
% This is the command to define the "pen" width for drawing.
% Here we widen it from the default 0.5 points to 1 point.
pickup pencircle scaled 1pt;
% The arrow's tip is at the last mentioned point. For draw or drawarrow
% or drawdblarrow (two headed), ".." means a curved line, while "--" means
% a straight line.
drawarrow z0..z1..z2;
% The commands to draw a label have two arguments. First is the label
% itself and next is the position. Often the label is TeX commands
% (not LaTex) between "btex" and "etex" tokens. The plain label command
% centers the label on the coordinates. The others (.lft, .rt, .top, .bot,
% .ulft, .urt, .llft, .lrt) position the label relative to the coordinate.
% Scaling makes the text bigger. Note that for exact positioning
% of the A \cap B text, a coordinate pair is subtracted from z0, with
% the subtractions done separately for horizontal and vertical positions.
label.rt(btex $A \cap B$ etex, z0-(0.25cm,0.2cm)) scaled 1.2;
label(btex $A$ etex, (-0.5cm,-1.1cm)) scaled 1.2;
label(btex $B$ etex, (1.5cm,-1.1cm)) scaled 1.2;
endfig;
% ABVenn.2 is an improvement that outlines the circles and uses color.
beginfig(2);
path a, b, aa, ab;
a = fullcircle scaled 2cm;
b = a shifted (1cm,0cm);
aa = halfcircle scaled 2cm rotated 270 shifted (0cm,0cm);
ab = buildcycle(aa, b);
pickup pencircle scaled 1pt;
% Predefined colors are black, white, red, green, blue.
% Color math is allowed (e.g., (red+blue)/2.
fill a withcolor blue;
draw a;
fill b withcolor red;
fill ab withcolor (red+blue)/2;
% This outlines the b circle.
draw b;
% And this outlines the ab intersection.
draw ab;
z0=(1.6cm,1.5cm);
z1=(0.9cm,1.2cm);
z2=(0.5cm,0.5cm);
% This line uses the ":=" assignment operator to change the value
% of the predefined variable "ahlength", which controls arrow head
% sizes, from the default value of 4 to 6 postscript points.
ahlength:=6bp;
drawarrow z0..z1..z2;
% Colors can also be defined as Red, Green, Blue triples.
color aqua;
aqua:=(0.2,0.5,0.9);
label.rt(btex $A \cap B$ etex, z0-(0.25cm,0.2cm)) scaled 1.2 withcolor aqua;
label(btex $A$ etex, (-0.5cm,-1.1cm)) scaled 1.2 withcolor aqua;
label(btex $B$ etex, (1.5cm,-1.1cm)) scaled 1.2 withcolor aqua;
endfig;
% ABVenn.3 simulates cross-hatching rather than shading of the intersection.
beginfig(3);
path a, b, aa, ab;
a = fullcircle scaled 2cm;
b = a shifted (1cm,0cm);
aa = halfcircle scaled 2cm rotated 270 shifted (0cm,0cm);
ab = buildcycle(aa, b);
% A picture is a more complicated object than a path; it may consist
% of several paths. This "low level" programming declares a picture
% object, then creates an empty picture object. The picture we
% are creating is a series of diagonal lines in the form of a trapezoid
% big enough to cover the intersection of a and b. Then it is "clipped"
% to only show the part inside the intersection path defined by ab.
picture ac;
ac = nullpicture;
% The for loop uses the addto low-level command to draw lines (paths)
% between pairs of computed coordinate endpoints to create the cross-
% hatching. This creates a picture with 15 paths.
for i=1 upto 15:
addto ac doublepath (i*cm/10-1cm,-1cm)--(1cm+i*cm/10,2cm) withpen pencircle;
endfor;
% The clip command removes from picture ac the portion not inside of
% closed path ab. Comment out the clip command to see the full picture.
clip ac to ab;
pickup pencircle scaled 1pt;
draw a;
fill b withcolor 0.8white;
% The unfill command removes the shading from the intersection.
unfill ab;
% This draws just the cross-hatching of the intersection.
draw ac;
draw b;
% This outlines the intersection.
draw ab;
z0=(1.6cm,1.5cm);
z1=(0.9cm,1.2cm);
z2=(0.5cm,0.5cm);
ahlength:=6bp;
drawarrow z0..z1..z2;
label.rt(btex $A \cap B$ etex, z0-(0.25cm,0.2cm)) scaled 1.2;
label(btex $A$ etex, (-0.5cm,-1.1cm)) scaled 1.2;
label(btex $B$ etex, (1.5cm,-1.1cm)) scaled 1.2;
endfig;
% ABVenn.4 shows what is behind the cross-hatching code.
beginfig(4);
path a, b, aa, ab;
a = fullcircle scaled 2cm;
b = a shifted (1cm,0cm);
aa = halfcircle scaled 2cm rotated 270 shifted (0cm,0cm);
ab = buildcycle(aa, b);
picture ac;
ac = nullpicture;
for i=1 upto 15:
addto ac doublepath (i*cm/10-1cm,-1cm)--(1cm+i*cm/10,2cm) withpen pencircle;
endfor;
% No clipping is done, so you can see what was clipped above.
%%%% clip ac to ab;
pickup pencircle scaled 1pt;
draw a;
fill b withcolor 0.8white;
unfill ab;
draw ac;
draw b;
draw ab;
z0=(1.6cm,1.5cm);
z1=(0.9cm,1.2cm);
z2=(0.5cm,0.5cm);
ahlength:=6bp;
drawarrow z0..z1..z2;
label.rt(btex $A \cap B$ etex, z0-(0.25cm,0.2cm)) scaled 1.2;
label(btex $A$ etex, (-0.5cm,-1.1cm)) scaled 1.2;
label(btex $B$ etex, (1.5cm,-1.1cm)) scaled 1.2;
endfig;
end;