| 1 |
% venn.mp |
| 2 |
% MetaPost input file for venn diagrams. |
| 3 |
% |
| 4 |
% LICENSE |
| 5 |
% This is released under the LaTeX Project Public License. |
| 6 |
% |
| 7 |
% HISTORY |
| 8 |
% 2001-Apr-15 Jim Hefferon [email protected] Written |
| 9 |
% |
| 10 |
|
| 11 |
%defaultscale := 9pt/fontsize defaultfont; |
| 12 |
|
| 13 |
newinternal venn_width, venn_height; |
| 14 |
venn_width := 100pt; venn_height := .618venn_width; |
| 15 |
newinternal venn_circle_diameter; |
| 16 |
venn_circle_diameter := .5venn_height; |
| 17 |
newinternal venn_pen; |
| 18 |
venn_pen := 0.4pt; |
| 19 |
vardef venn_shade = .9white enddef; |
| 20 |
vardef venn_unshade = white enddef; |
| 21 |
|
| 22 |
path venn_box; % box that borders a Venn diagram; `U' |
| 23 |
venn_box = (0,0)--(venn_width,0)--(venn_width,venn_height) |
| 24 |
--(0,venn_height)--cycle; |
| 25 |
newinternal venn_circle_left_shift, |
| 26 |
venn_circle_right_shift, |
| 27 |
venn_circle_top_shift, |
| 28 |
venn_circle_bottom_shift; |
| 29 |
venn_circle_left_shift := .5venn_width; |
| 30 |
venn_circle_right_shift := venn_width-venn_circle_left_shift; |
| 31 |
venn_circle_top_shift := .5venn_height; |
| 32 |
venn_circle_bottom_shift := venn_height-venn_circle_bottom_shift; |
| 33 |
|
| 34 |
|
| 35 |
% ..... venn_color ....... |
| 36 |
% Return either venn_shade or venn_unshade, as chosen. |
| 37 |
% ARGS |
| 38 |
% b A boolean |
| 39 |
% RETURNS |
| 40 |
% a color |
| 41 |
% USAGE |
| 42 |
% fill circle withcolor venn_color(true); |
| 43 |
% |
| 44 |
def venn_color(expr b) = |
| 45 |
if b: venn_shade else: venn_unshade fi |
| 46 |
enddef; |
| 47 |
|
| 48 |
% ...... draw_venn_none ........... |
| 49 |
% Draw a Venn diagram with no enclosed circles (just the universal set) |
| 50 |
% ARGS |
| 51 |
% univ A boolean; color the set or not? |
| 52 |
% RETURNS |
| 53 |
% -- |
| 54 |
% USAGE |
| 55 |
% draw_venn_none(true); |
| 56 |
% draws a shaded set. |
| 57 |
% |
| 58 |
def draw_venn_none(expr univ)text t = |
| 59 |
begingroup |
| 60 |
fill venn_box t withcolor venn_color(univ); |
| 61 |
pickup pensquare scaled venn_pen; draw venn_box t; |
| 62 |
endgroup |
| 63 |
enddef; |
| 64 |
|
| 65 |
% ...... draw_venn_one ........... |
| 66 |
% Draw a Venn diagram with one enclosed circle |
| 67 |
% ARGS |
| 68 |
% univ A boolean; color the universal set or not? |
| 69 |
% cl A boolean; color the circle? |
| 70 |
% RETURNS |
| 71 |
% -- |
| 72 |
% USAGE |
| 73 |
% draw_venn_one(true,false); |
| 74 |
% draws a shaded set with the unshaded circle. |
| 75 |
% |
| 76 |
def draw_venn_one(expr univ, cl)text t = |
| 77 |
begingroup |
| 78 |
venn_circle_left_shift := .5venn_width; |
| 79 |
venn_circle_right_shift := venn_width-venn_circle_left_shift; |
| 80 |
venn_circle_top_shift := .5venn_height; |
| 81 |
venn_circle_bottom_shift := venn_height-venn_circle_top_shift; |
| 82 |
|
| 83 |
fill venn_box t withcolor venn_color(univ); |
| 84 |
pickup pensquare scaled venn_pen; draw venn_box t; |
| 85 |
|
| 86 |
save circle; path circle; |
| 87 |
circle = fullcircle scaled venn_circle_diameter |
| 88 |
shifted (venn_circle_left_shift,venn_circle_top_shift) t; |
| 89 |
fill circle withcolor venn_color(cl); |
| 90 |
pickup pencircle scaled venn_pen; draw circle; |
| 91 |
endgroup |
| 92 |
enddef; |
| 93 |
|
| 94 |
% ...... draw_venn_two ........... |
| 95 |
% Draw a Venn diagram with two enclosed circles |
| 96 |
% ARGS |
| 97 |
% univ A boolean; color the universal set or not? |
| 98 |
% cl A boolean; color the left circle? |
| 99 |
% lr A boolean; color the intersection wedge? |
| 100 |
% cr A boolean; color the right circle? |
| 101 |
% RETURNS |
| 102 |
% -- |
| 103 |
% USAGE |
| 104 |
% draw_venn_two(true,false,true,false) shifted (2in,1in); |
| 105 |
% draws a diagram, shifted. |
| 106 |
% NOTE |
| 107 |
% The trick here is to use mpost's buildcycle to define the |
| 108 |
% path `lr' outlining the intersection of the left and right |
| 109 |
% circles. Below the left circle is defined to be rotated -180. |
| 110 |
% That's because otherwise the intersection with the right |
| 111 |
% circle occurs on top early in the path, and on bottom late in |
| 112 |
% the path, and so the left of the lr region crosses through the |
| 113 |
% `point 0 of circle_left' boundary, which confuses buildcycle. |
| 114 |
% So, it's a hack. |
| 115 |
% |
| 116 |
def draw_venn_two(expr univ, cl, lr, cr)text t = |
| 117 |
begingroup |
| 118 |
venn_circle_left_shift := .4venn_width; |
| 119 |
venn_circle_right_shift := venn_width-venn_circle_left_shift; |
| 120 |
venn_circle_top_shift := .5venn_height; |
| 121 |
venn_circle_bottom_shift := venn_height-venn_circle_top_shift; |
| 122 |
|
| 123 |
fill venn_box t withcolor venn_color(univ); |
| 124 |
pickup pensquare scaled venn_pen; draw venn_box t; |
| 125 |
|
| 126 |
save circle_left, circle_right; path circle_left, circle_right; |
| 127 |
circle_left = fullcircle rotated -180 scaled venn_circle_diameter |
| 128 |
shifted (venn_circle_left_shift,venn_circle_top_shift) t; |
| 129 |
circle_right = fullcircle scaled venn_circle_diameter |
| 130 |
shifted (venn_circle_right_shift,venn_circle_top_shift) t; |
| 131 |
save area_lr; path area_lr; |
| 132 |
area_lr = buildcycle(circle_left,circle_right); |
| 133 |
|
| 134 |
fill circle_left withcolor venn_color(cl); |
| 135 |
fill circle_right withcolor venn_color(cr); |
| 136 |
fill area_lr withcolor venn_color(lr); |
| 137 |
pickup pencircle scaled venn_pen; |
| 138 |
draw circle_left; draw circle_right; |
| 139 |
endgroup |
| 140 |
enddef; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
% ...... draw_venn_three ........... |
| 145 |
% Draw a Venn diagram with three enclosed circles |
| 146 |
% ARGS |
| 147 |
% univ A boolean; color the universal set or not? |
| 148 |
% cl A boolean; color the left circle? |
| 149 |
% lr A boolean; color the intersection wedge? |
| 150 |
% cr A boolean; color the right circle? |
| 151 |
% lb A boolean; color the intersection of left and bot right circles? |
| 152 |
% rb A boolean; color the intersection of right and bot circles? |
| 153 |
% lrb A boolean; color the intersection of all three circles? |
| 154 |
% cb A boolean; color the bottom circle? |
| 155 |
% RETURNS |
| 156 |
% -- |
| 157 |
% USAGE |
| 158 |
% draw_venn_three(true,false, ...) shifted (2in,1in); |
| 159 |
% draws a diagram, shifted. |
| 160 |
% NOTE |
| 161 |
% The trick here is to use mpost's buildcycle to define the |
| 162 |
% paths `lr' (outlining the intersection of the left and right |
| 163 |
% circles), `rb', `lb', and `lrb'. |
| 164 |
% To do that I had to fiddle with the times along the circles. |
| 165 |
% So, it's a hack. |
| 166 |
% |
| 167 |
def draw_venn_three(expr univ, cl, lr, cr, lb, rb, lrb, cb)text t = |
| 168 |
begingroup |
| 169 |
venn_circle_left_shift := .4venn_width; |
| 170 |
venn_circle_right_shift := venn_width-venn_circle_left_shift; |
| 171 |
venn_circle_top_shift := .63venn_height; |
| 172 |
venn_circle_bottom_shift := venn_height-venn_circle_top_shift; |
| 173 |
|
| 174 |
fill venn_box t withcolor venn_color(univ); |
| 175 |
pickup pensquare scaled venn_pen; draw venn_box t; |
| 176 |
|
| 177 |
save circle_left, circle_right, circle_bottom; |
| 178 |
path circle_left, circle_right, circle_bottom; |
| 179 |
circle_left = (reverse fullcircle) rotated -180 scaled venn_circle_diameter |
| 180 |
shifted (venn_circle_left_shift,venn_circle_top_shift) t; |
| 181 |
circle_right = (reverse fullcircle) scaled venn_circle_diameter |
| 182 |
shifted (venn_circle_right_shift,venn_circle_top_shift) t; |
| 183 |
circle_bottom = (reverse fullcircle) rotated -90 scaled venn_circle_diameter |
| 184 |
shifted (.5venn_width,venn_circle_bottom_shift) t; |
| 185 |
save area_lr, area_lb, area_rb, area_lrb; |
| 186 |
path area_lr, area_lb, area_rb, area_lrb; |
| 187 |
area_lr = buildcycle(circle_left,circle_right); |
| 188 |
area_lb = buildcycle(circle_left,circle_bottom); |
| 189 |
area_rb = buildcycle(circle_right,circle_bottom); |
| 190 |
area_lrb = buildcycle(circle_left,circle_right,circle_bottom); |
| 191 |
|
| 192 |
fill circle_left withcolor venn_color(cl); |
| 193 |
fill circle_right withcolor venn_color(cr); |
| 194 |
fill circle_bottom withcolor venn_color(cb); |
| 195 |
fill area_lr withcolor venn_color(lr); |
| 196 |
fill area_lb withcolor venn_color(lb); |
| 197 |
fill area_rb withcolor venn_color(rb); |
| 198 |
fill area_lrb withcolor venn_color(lrb); |
| 199 |
pickup pencircle scaled venn_pen; |
| 200 |
draw circle_left; draw circle_right; draw circle_bottom; |
| 201 |
endgroup |
| 202 |
enddef; |
| 203 |
|
| 204 |
|
| 205 |
% test: This works for me under Linux (2001-Apr-15) |
| 206 |
% > mpost venn |
| 207 |
% > tex mproof venn.0 |
| 208 |
% > dvips -Pwww -omproof.ps mproof |
| 209 |
% results in a document mproof.ps in PostScript that I can see with |
| 210 |
% > gv mproof.ps (type that from an XWindow xterm). |
| 211 |
|
| 212 |
% To run the test, uncomment the next five lines (including the `end'). |
| 213 |
%beginfig(0); |
| 214 |
% draw_venn_three(false,true,false,true,false,true,true,false); |
| 215 |
% draw_venn_three(false,true,false,true,false,true,true,false) shifted (2in,3in); |
| 216 |
%endfig; |
| 217 |
%end |
| 218 |
|