Debian Codegraph @

texmf-dist/metapost/threeddice/threeddice.mp

raw
1 % File threeddice.mp
2 % Version 1.0
3 %%%%%%%%%%%%%%%%%%%%
4 numeric dot_diam, corner_rad, face_margin, die_size;
5
6 % diameter of a dot
7 dot_diam := 1/6;
8
9 % distance from edge of face to edge of dot.
10 face_margin := dot_diam/2;
11
12 % rounded corner radius
13 corner_rad := dot_diam/2;
14
15 % size of a single die face:
16 die_size := 1cm;
17
18 % Outline of a face.
19 path die_face;
20 die_face := (0,corner_rad){down}
21 ..{right}(corner_rad,0)..(1-corner_rad,0){right}
22 ..{up}(1,corner_rad)..(1,1-corner_rad){up}
23 ..{left}(1-corner_rad,1)..(corner_rad,1){left}
24 ..{down}(0,1-corner_rad)..cycle;
25
26 % dot shape
27 path dot;
28 dot := fullcircle scaled dot_diam;
29
30 % distance from nearest edge to center of dot:
31 numeric dot_shift;
32 dot_shift := dot_diam/2 + face_margin;
33
34 % Five of 9 locations where a dot could be located.
35 pair die_face[];
36
37 die_face 1 := ( 0, 1) + dot_shift*( 1,-1); % TL
38 die_face 2 := ( 0,.5) + dot_shift*( 1, 0); % TC
39 die_face 3 := (.5,.5) ; % CC
40 die_face 4 := ( 1,.5) + dot_shift*(-1, 0); % BC
41 die_face 5 := ( 1, 0) + dot_shift*(-1, 1); % BR
42
43
44 picture pips[];
45 pips 1 := pips 2 := nullpicture;
46
47 % dot in center
48 addto pips 1 contour dot shifted die_face 3;
49
50 % dots in TL and BR corners
51 addto pips 2 contour dot shifted die_face 1;
52 addto pips 2 contour dot shifted die_face 5;
53
54 % dots in TR and BL corners
55 pips 7 := pips 2 rotatedabout ((.5,.5), 90);
56
57 % 3 dots on TL to BR diagonal
58 pips 3 := pips 2;
59 addto pips 3 also pips 1;
60
61 % 3 dots on BL to TR diagonal
62 pips 8 := pips3 rotatedabout ((.5,.5),90);
63
64 % Dots in all 4 corners
65 pips 4 := pips 2;
66 addto pips 4 also pips 7;
67
68 % dots in all four corners and center
69 pips 5 := pips 4;
70 addto pips 5 also pips 1;
71
72 % 6 dots, 3 dots on top and three on bottom
73 pips 6 := pips 4;
74 addto pips 6 contour dot shifted die_face 2;
75 addto pips 6 contour dot shifted die_face 4;
76
77 % 6 dots, 3 at left and 3 at right
78 pips 11 := pips6 rotatedabout ((.5,.5),90);
79
80 % When three faces are showing, alternates 7, 8 and 11 for 2, 3 and 6 are
81 % used when the total of the pips is odd (). This is an ad hoc rule that
82 % matches what I see on my reference die.
83 vardef pips_sel_iii (expr X)=
84 X if odd pips_total: if (X=2) or (X=3) or (X=6): + 5 fi fi
85 enddef;
86 %
87 % Choices are less easy when only two faces show:
88 vardef pips_sel_ii (expr X,Y)=
89 X if ((X=2) and ((Y=3) or (Y=4))) or
90 ((X=3) and ((Y=1) or (Y=6))) or
91 ((X=6) and ((Y=2) or (Y=5))): + 5
92 fi
93 enddef;
94
95 % for die with 3 faces showing:
96 path topface[], llftface, lrtface;
97 topface 1 = die_face rotated 45 yscaled (1/sqrt(3));
98 llftface = topface 1 rotated 120;
99 lrtface = topface 1 rotated -120;
100
101 % for die with 2 faces showing:
102 path frontface;
103 topface 2 := (die_face shifted (-.5,0)) yscaled (1/sqrt(2));
104 frontface := topface 2 rotated 180;
105
106 pair D[];
107 D1 := dir 30;
108 D2 := dir 150;
109
110 path corner_connect[];
111 corner_connect1 := (point 6 of lrtface){up}..{D2}(point 3 of topface 1);
112 corner_connect2 := (point 6 of topface 1){-D1}..{down}(point 3 of llftface);
113 corner_connect3 := (point 6 of llftface){-D2}..{D1}(point 3 of lrtface);
114 corner_connect4 := (point 0 of frontface){up}..(point 3 of topface 2){up};
115 corner_connect5 := (point 0 of topface 2){down}..(point 3 of frontface){down};
116
117 % Text parameter is for drawing options (transformation, withcolor, etc.);
118 % expression parameters are the # of pips on top, left and right.
119 def die_three(expr T, L, R) text _do_ =
120 numeric pips_total;
121 pips_total := T + L + R;
122 draw topface 1 _do_;
123 draw llftface _do_;
124 draw lrtface _do_;
125 for n = 1 upto 3:
126 draw corner_connect[n] _do_;
127 endfor
128 draw pips[pips_sel_iii(T)] rotated 45 yscaled (1/sqrt(3)) _do_;
129 draw pips[pips_sel_iii(L)] rotated 45 yscaled (1/sqrt(3)) rotated 120 _do_;
130 draw pips[pips_sel_iii(R)] rotated 45 yscaled (1/sqrt(3)) rotated -120 _do_;
131 enddef;
132
133 def die_two (expr T, F) text _do_ =
134 draw topface 2 _do_;
135 draw frontface _do_;
136 draw (pips[pips_sel_ii(T,F)] shifted .5left) yscaled (1/sqrt(2)) _do_;
137 draw (pips[pips_sel_ii(F,T)] shifted .5left) yscaled (1/sqrt(2)) rotated 180 _do_;
138 draw corner_connect4 _do_;
139 draw corner_connect5 _do_;
140 enddef;
141
142 def die_one (expr F) text _do_ =
143 draw die_face _do_;
144 draw pips[F] _do_;
145 enddef;
146
147 if known outputtemplate:
148 outputtemplate := "die3d-%c.mps";
149 elseif known mpversion:
150 filenametemplate "die3d-%c.mps";
151 fi
152 % The 8 permissible views (up to rotation) with three faces equally
153 % visible of a right handed die:
154 beginfig(123);
155 die_three(1,2,3) scaled die_size;
156 endfig;
157
158 beginfig(135);
159 die_three(1,3,5) scaled die_size;
160 endfig;
161
162 beginfig(154);
163 die_three(1,5,4) scaled die_size;
164 endfig;
165
166 beginfig(142);
167 die_three(1,4,2) scaled die_size;
168 endfig;
169
170 beginfig(246);
171 die_three(2,4,6) scaled die_size;
172 endfig;
173
174 beginfig(263);
175 die_three(2,6,3) scaled die_size;
176 endfig;
177
178 beginfig(365);
179 die_three(3,6,5) scaled die_size;
180 endfig;
181
182 beginfig(456);
183 die_three(4,5,6) scaled die_size;
184 endfig;
185
186 % The 12 permissible views (up to rotation) with only two faces equally
187 % visible of a right handed die:
188 beginfig(12);
189 die_two(1,2) scaled die_size;
190 endfig;
191
192 beginfig(13);
193 die_two(1,3) scaled die_size;
194 endfig;
195
196 beginfig(14);
197 die_two(1,4) scaled die_size;
198 endfig;
199
200 beginfig(15);
201 die_two(1,5) scaled die_size;
202 endfig;
203
204 beginfig(23);
205 die_two(2,3) scaled die_size;
206 endfig;
207
208 beginfig(24);
209 die_two(2,4) scaled die_size;
210 endfig;
211
212 beginfig(26);
213 die_two(2,6) scaled die_size;
214 endfig;
215
216 beginfig(35);
217 die_two(3,5) scaled die_size;
218 endfig;
219
220 beginfig(36);
221 die_two(3,6) scaled die_size;
222 endfig;
223
224 beginfig(45);
225 die_two(4,5) scaled die_size;
226 endfig;
227
228 beginfig(46);
229 die_two(4,6) scaled die_size;
230 endfig;
231
232 beginfig(56);
233 die_two(5,6) scaled die_size;
234 endfig;
235
236 % The 6 possible single faces
237 beginfig(1);
238 die_one(1) scaled die_size;
239 endfig;
240
241 beginfig(2);
242 die_one(2) scaled die_size;
243 endfig;
244
245 beginfig(3);
246 die_one(3) scaled die_size;
247 endfig;
248
249 beginfig(4);
250 die_one(4) scaled die_size;
251 endfig;
252
253 beginfig(5);
254 die_one(5) scaled die_size;
255 endfig;
256
257 beginfig(6);
258 die_one(6) scaled die_size;
259 endfig;
260
261 end