Debian Codegraph @

texmf-dist/metapost/mp3d/3dpoly.mp

raw
1 %%\input epsf
2 %%\def\newpage{\vfill\eject}
3 %%\def\vc#1{$\vcenter{#1}$}
4 %%\def\title#1{\hrule\vskip1mm#1\par\vskip1mm\hrule\vskip5mm}
5 %%\def\figure#1{\par\centerline{\epsfbox{#1}}}
6 %%\title{{\bf 3DPOLY.MP: DEFINITION OF REGULAR CONVEX POLYHEDRA IN METAPOST}}
7
8 %% version 1.34, 17 August 2003
9 %% {\bf Denis Roegel} ({\tt [email protected]})
10 %%
11 %%This package defines the five regular convex polyhedra classes and
12 %%must be used with the {\bf 3d} package.
13
14 if known three_d_poly_version:
15 expandafter endinput % avoids loading this package twice
16 fi;
17
18 message "*** 3dpoly, v1.34 (c) D. Roegel, 17 August 2003 ***";
19 numeric three_d_poly_version;
20 three_d_poly_version=1.34;
21
22 %%Each object definition is parameterized by
23 %%an instance identification.
24
25 %%All polyhedra are normalized (centered on the origin and inscriptible
26 %%in a sphere of radius 1).
27
28 %%The polyhedra are defined with a certain number of points and faces.
29 %%The points comprise both the vertices and the center of the object.
30
31 %%It might be useful to remind the Euler formula linking
32 %%the number of faces $f$,
33 %%vertices $v$ and edges $e$ in polyhedra with no hole in dimension 3:
34 %%$f+v-e=2$. This can be verified in the following table:
35
36 %%\centerline{\vbox{\halign{\quad#\hfil\quad&&\quad\hfil#\quad\cr
37 %% \omit \hfil type\hfil&\omit\hfil faces\hfil
38 %% &\omit\hfil vertices\hfil&\omit\hfil edges\hfil\cr
39 %% tetrahedron&4&4&6\cr
40 %% cube&6&8&12\cr
41 %% octahedron&8&6&12\cr
42 %% dodecahedron&12&20&30\cr
43 %% icosahedron&20&12&30\cr}}}
44
45 %%For more details, see the documentation enclosed in the distribution.
46
47 %%\newpage\title{TETRAHEDRON (4 faces)\kern1cm\epsfbox{tetra.ps}}
48
49 %% Construction of a tetrahedron:
50
51 %% One face is \vc{\epsfbox{vect-fig.1}} with $h=c\sqrt{3}/2$
52
53 %% The angle $\alpha$ between two faces is given by
54 %% $\sin(\alpha/2)={\displaystyle c/2\over \displaystyle c\sqrt{3}/2}=1/\sqrt3$
55
56 %% \figure{vect-fig.2}
57
58 def set_tetrahedron_points(expr inst)=
59 set_point(1)(0,0,0);set_point(2)(1,0,0);
60 set_point(3)(cosd(60),sind(60),0);% $60=360/6$
61 sinan=1/sqrt(3);cosan=sqrt(1-sinan**2);
62 an=180-2*angle((cosan,sinan));
63 new_face_point(4,1,2,3,an);
64 normalize_obj(inst)(1,2,3,4);
65 set_point(5)(0,0,0);% center of tetrahedron
66 enddef;
67
68 vardef def_tetrahedron(expr inst)=
69 new_obj_points(inst,5);% 4 vertices and the center
70 new_obj_faces(inst,4);% 4 faces in the definition
71 set_tetrahedron_points(inst);
72 set_obj_face(1,"1,2,4","b4fefe");
73 set_obj_face(2,"2,3,4","b49bc0");
74 set_obj_face(3,"1,4,3","b4c8fe");
75 set_obj_face(4,"1,3,2","b4fe40");
76 enddef;
77
78 %%\newpage\title{CUBE (HEXAHEDRON) (6 faces)\kern1cm\epsfbox{cube.ps}}
79
80 %%The definition of a cube is straightforward.
81
82 def set_cube_points(expr inst)=
83 set_point(1)(0,0,0);
84 set_point(2)(1,0,0);
85 set_point(3)(1,1,0);
86 set_point(4)(0,1,0);
87 set_point(5)(0,0,1);
88 set_point(6)(1,0,1);
89 set_point(7)(1,1,1);
90 set_point(8)(0,1,1);
91 normalize_obj(inst)(1,7);% 1 and 7 are opposite vertices
92 set_point(9)(0,0,0);% center of cube
93 enddef;
94
95 vardef def_cube(expr inst)=
96 new_obj_points(inst,9);% 8 vertices and the center
97 new_obj_faces(inst,6);% 6 faces in the definition
98 set_cube_points(inst);
99 set_obj_face(1,"1,2,6,5","b4fe40");
100 set_obj_face(2,"2,3,7,6","45d040");
101 set_obj_face(3,"4,8,7,3","45a114");
102 set_obj_face(4,"1,5,8,4","45a1d4");
103 set_obj_face(5,"5,6,7,8","4569d4");
104 set_obj_face(6,"4,3,2,1","112da1");
105 enddef;
106
107
108 %%\newpage\title{OCTAHEDRON (8 faces)\kern1cm\epsfbox{octa.ps}}
109
110 %% A section of a pyramid is: \vc{\epsfbox{vect-fig.3}}
111
112 %% The height of this pyramid is given
113 %% by $H^2={(c\sqrt3/2)}^2-{(c/2)}^2=c^2/2$, hence $H=c/\sqrt2$
114
115 def set_octahedron_points(expr inst)=
116 set_point(1)(0,0,0);
117 set_point(2)(1,0,0);
118 set_point(3)(1,1,0);
119 set_point(4)(0,1,0);
120 set_point(5)(.5,.5,1/sqrt(2));
121 set_point(6)(.5,.5,-1/sqrt(2));
122 normalize_obj(inst)(5,6);% 5 and 6 are opposite vertices
123 set_point(7)(0,0,0);% center of octahedron
124 enddef;
125
126 vardef def_octahedron(expr inst)=
127 new_obj_points(inst,7);% 6 vertices and the center
128 new_obj_faces(inst,8);% 8 faces in the definition
129 set_octahedron_points(inst);
130 set_obj_face(1,"1,2,5","b4fefe");
131 set_obj_face(2,"2,3,5","45d040");
132 set_obj_face(3,"3,4,5","4569d4");
133 set_obj_face(4,"4,1,5","b49bc0");
134 set_obj_face(5,"6,1,4","45a1d4");
135 set_obj_face(6,"6,2,1","b4c8fe");
136 set_obj_face(7,"6,3,2","b49b49");
137 set_obj_face(8,"6,4,3","112da1");
138 enddef;
139
140
141 %%\newpage\title{DODECAHEDRON (12 faces)\kern1cm\epsfbox{dodeca.ps}}
142
143 %% Two adjacent faces (pentagons) of the dodecahedron are as follows:
144 %%\figure{vect-fig.13}
145 %% The angle between the faces is the angle between $\overrightarrow{OA}$
146 %% and $\overrightarrow{OB}$.
147 %% $OA=OB=d_3$ as defined in \figure{vect-fig.14}
148 %% $d_1=2r\sin(\pi/5)$, $d_2=2r\sin(2\pi/5)$
149 %% and $d_3=d_1\sin(2\pi/5)=2r\sin(\pi/5)\sin(2\pi/5)$
150 %%
151 %% The angle $\alpha$ is defined by the following conditions:
152 %% \figure{vect-fig.15}
153 %% Thus $d_2^2=d_3^2+d_3^2-2d_3^2\cos\alpha$, which leads to
154 %% $\cos\alpha=1-{\displaystyle d_2^2\over\displaystyle2d_3^2}
155 %% =-{\displaystyle\cos(2\pi/5)\over\displaystyle2\sin^2(\pi/5)}$
156 %%\newpage
157
158 def set_dodecahedron_points(expr inst)=
159 new_points(fc)(10);% face centers
160 set_point_(fc1,0,0,0);set_point(1)(1,0,0);
161 set_point(2)(cosd(72),sind(72),0);% 72=360/5
162 rotate_in_plane(3,fc1,1,2);
163 rotate_in_plane(4,fc1,2,3);
164 rotate_in_plane(5,fc1,3,4);
165 cosan=-cosd(72)/(2*sind(36)*sind(36));sinan=sqrt(1-cosan**2);
166 an=180-angle((cosan,sinan));
167 new_abs_face_point(fc2,fc1,1,2,an);
168 new_abs_face_point(fc3,fc1,2,3,an);
169 new_abs_face_point(fc4,fc1,3,4,an);
170 new_abs_face_point(fc5,fc1,4,5,an);
171 new_abs_face_point(fc6,fc1,5,1,an);
172 rotate_in_plane(6,fc2,2,1);
173 rotate_in_plane(7,fc2,1,6);
174 rotate_in_plane(8,fc2,6,7);
175 rotate_in_plane(9,fc3,2,8);
176 rotate_in_plane(10,fc3,8,9);
177 rotate_in_plane(11,fc4,3,10);
178 rotate_in_plane(12,fc4,10,11);
179 rotate_in_plane(13,fc5,4,12);
180 rotate_in_plane(14,fc5,12,13);
181 rotate_in_plane(15,fc6,5,14);
182 new_abs_face_point(fc7,fc2,6,7,an);
183 new_abs_face_point(fc8,fc3,8,9,an);
184 new_abs_face_point(fc9,fc4,10,11,an);
185 new_abs_face_point(fc10,fc5,12,13,an);
186 rotate_in_plane(16,fc7,6,15);
187 rotate_in_plane(17,fc7,15,16);
188 rotate_in_plane(18,fc8,7,17);
189 rotate_in_plane(19,fc9,9,18);
190 rotate_in_plane(20,fc10,11,19);
191 normalize_obj(inst)(2,20);% opposite vertices
192 set_point(21)(0,0,0);% center of dodecahedron
193 free_points(fc)(10);
194 enddef;
195 %%\newpage
196 vardef def_dodecahedron(expr inst)=
197 save cosan,sinan,an;
198 new_obj_points(inst,21);% 21 points in the definition
199 new_obj_faces(inst,12);% 12 faces in the definition
200 set_dodecahedron_points(inst);
201 set_obj_face(1,"5,4,3,2,1","ff0fa1");
202 set_obj_face(2,"8,7,6,1,2","b40000");
203 set_obj_face(3,"10,9,8,2,3","b49b49");
204 set_obj_face(4,"12,11,10,3,4","b49bc0");
205 set_obj_face(5,"14,13,12,4,5","b4c8fe");
206 set_obj_face(6,"6,15,14,5,1","b4fefe");
207 set_obj_face(7,"6,7,17,16,15","b4fe40");
208 set_obj_face(8,"8,9,18,17,7","45d040");
209 set_obj_face(9,"10,11,19,18,9","45a114");
210 set_obj_face(10,"11,12,13,20,19","45a1d4");
211 set_obj_face(11,"14,15,16,20,13","4569d4");
212 set_obj_face(12,"16,17,18,19,20","112da1");
213 enddef;
214
215 %%\newpage\title{ICOSAHEDRON (20 faces)\kern1cm\epsfbox{icosa.ps}}
216
217 %%Two faces of an icosahedron are linked in the following way:
218
219 %% \vc{\epsfbox{vect-fig.4}} where $h=c\sqrt3/2$
220
221 %% $d$ is also a diagonal in a pentagon: \vc{\epsfbox{vect-fig.5}}
222
223 %% $c=2r\sin(\pi/5)$,
224 %% $d=2r\sin(2\pi/5)=2c\cos(\pi/5)$
225
226 %% The angle between two faces is computed as follows:
227 %% \vc{\epsfbox{vect-fig.6}}
228
229 %% We have ${(2\cos(\pi/5))}^2=2{(\sqrt3/2)}^2-2{(\sqrt3/2)}^2\cos\alpha$
230
231 %% Thus $cos\alpha=1-{8\over3}\cos^2(\pi/5)$
232 %%\newpage
233
234 def set_icosahedron_points(expr inst)=
235 set_point(1)(0,0,0);set_point(2)(1,0,0);
236 set_point(3)(cosd(60),sind(60),0);% 60=360/6
237 cosan=1-8/3*cosd(36)*cosd(36);sinan=sqrt(1-cosan**2);
238 an=180-angle((cosan,sinan));
239 new_face_point(4,1,2,3,an);
240 new_face_point(5,2,3,1,an);
241 new_face_point(6,3,1,2,an);
242 new_face_point(7,2,4,3,an);
243 new_face_point(8,3,5,1,an);
244 new_face_point(9,1,6,2,an);
245 new_face_point(10,3,4,7,an);
246 new_face_point(11,3,7,5,an);
247 new_face_point(12,1,8,6,an);
248 normalize_obj(inst)(1,10);% opposite vertices
249 set_point(13)(0,0,0);% center of icosahedron
250 enddef;
251
252 vardef def_icosahedron(expr inst)=
253 save cosan,sinan,an;
254 new_obj_points(inst,13);% 12 vertices and the center
255 new_obj_faces(inst,20);% 20 faces in the definition
256 set_icosahedron_points(inst);
257 set_obj_face(1,"3,2,1","b40000");
258 set_obj_face(2,"2,3,4","ff0fa1");
259 set_obj_face(3,"3,7,4","b49b49");
260 set_obj_face(4,"3,5,7","b49bc0");
261 set_obj_face(5,"3,1,5","b4c8fe");
262 set_obj_face(6,"1,8,5","b4fefe");
263 set_obj_face(7,"1,6,8","b4fe40");
264 set_obj_face(8,"1,2,6","45d040");
265 set_obj_face(9,"2,9,6","45a114");
266 set_obj_face(10,"2,4,9","45a1d4");
267 set_obj_face(11,"9,4,10","4569d4");
268 set_obj_face(12,"4,7,10","112da1");
269 set_obj_face(13,"7,5,11","b4fefe");
270 set_obj_face(14,"5,8,11","b49bc0");
271 set_obj_face(15,"8,6,12","45a114");
272 set_obj_face(16,"6,9,12","b49b49");
273 set_obj_face(17,"8,12,11","b40000");
274 set_obj_face(18,"7,11,10","45a1d4");
275 set_obj_face(19,"12,10,11","b4c8fe");
276 set_obj_face(20,"9,10,12","ff0fa1");
277 enddef;
278
279 %%\newpage\title{General draw functions}
280
281 def draw_polyhedron(expr name)=
282 draw_faces(name);
283 enddef;
284
285 let draw_tetrahedron=draw_polyhedron;
286 let draw_cube=draw_polyhedron;
287 let draw_octahedron=draw_polyhedron;
288 let draw_dodecahedron=draw_polyhedron;
289 let draw_icosahedron=draw_polyhedron;
290
291 let new_poly=assign_obj;
292
293 endinput
294