summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/metapost/PfCLego.mp
blob: 1b5c8fc7ae637f397c10366cd635f1d840b2caf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Lego boxes by Anthony Phan.
% file: lego.mp
% last modification: January 29, 2001.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Licence? Feel-free-to-send-me-a-postcard
%
% Anthony Phan,
%
% D\'epartement de Math\'ematiques,
% SP2MI, T\'el\'eport 2,
% Boulevard Marie et Pierre Curie,
% BP 30179,
% F-86962 Futuroscope-Chasseneuil cedex.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

magnification=1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Les figures d\'efinies par ce programme
% repr\'esente une tentative de programmation
% tridimensionnelle avec MetaPost. Les trucs
% essentiels sont l'usage d'un rep\`ere par
% lequel s'effectue la projection sur le plan,
% et l'affichage des faces dans le plan dont
% le contour est orient\'e positivement qui
% corespondent alors \`a des faces visibles.
%
% Le niveau math\'ematique correspondant
% \`a ce programme devrait \^etre le niveau
% premier cycle en sciences.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Param\`etres de projection
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Rep\`ere initial

pair e_x,e_y,e_z,e_xi,e_yi,e_zi;
e_xi=sqrt 0.5*dir 210;
e_yi=sqrt 0.5*dir -30;
e_zi=sqrt 0.5*dir 90;

% trois coordonn\'ees du vecteur de profondeur,
% il v\'erifie depth_xi*e_xi+depth_yi*e_yi+depth_zi*e_zi=(0,0);

depth_xi:=-1; depth_yi:=-1; depth_zi:=-1;

% Param\`etres d'\'eclairement

% vecteur unitaire de l'origine \`a la source lumineuse

light_xi=0; light_yi=0; light_zi=1;

% r\'eglage du contraste

light_min=0.25; light_max=1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Param\`etres, unit\'es et mesures essentielles
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

color currentcolor; currentcolor=red;

lego_unit        :=magnification*0.8cm; % official
lego_height      :=1.2lego_unit;        % official
lego_plate_height:=0.4lego_unit;        % official
lego_thickness   :=1/6lego_unit;
lego_eps         :=1/20lego_unit;
lego_overshoot   :=lego_plate_height-lego_thickness;%0.2133lego_unit;
lego_diam        :=lego_unit-2lego_thickness;
lego_Diam        :=(sqrt 2)*lego_unit-lego_diam;
cyl_steps        :=60;

path upper_path, lower_path, temp_path;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Rotation du rep\`ere initial
% selon des pr\'etendus angles d'Euler.
% avec d\'etermination des facteurs d'ensoleillement.
% et des facteurs de profondeur...
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef euler(expr theta,phi,psi)=
  save ctheta,stheta,cphi,sphi,cpsi,spsi;
  ctheta=cosd theta; stheta=sind theta;
  cphi=cosd phi; sphi=sind phi;
  cpsi=cosd psi; spsi=sind psi;
  %
  e_x:= cphi*ctheta*e_xi + cphi*stheta*e_yi + sphi*e_zi;
  e_y:=-(cpsi*stheta+spsi*sphi*ctheta)*e_xi
  +     (cpsi*ctheta-spsi*sphi*stheta)*e_yi
  +     spsi*cphi*e_zi;
  e_z:=(spsi*stheta-cpsi*sphi*ctheta)*e_xi
  -    (spsi*ctheta+cpsi*sphi*stheta)*e_yi
  +    cpsi*cphi*e_zi;
  %
  light_x:= cphi*ctheta*light_xi + cphi*stheta*light_yi + sphi*light_zi;
  light_y:=-(cpsi*stheta+spsi*sphi*ctheta)*light_xi
  +     (cpsi*ctheta-spsi*sphi*stheta)*light_yi
  +     spsi*cphi*light_zi;
  light_z:=(spsi*stheta-cpsi*sphi*ctheta)*light_xi
  -    (spsi*ctheta+cpsi*sphi*stheta)*light_yi
  +    cpsi*cphi*light_zi;
  %
  depth_x:= cphi*ctheta*depth_xi + cphi*stheta*depth_yi + sphi*depth_zi;
  depth_y:=-(cpsi*stheta+spsi*sphi*ctheta)*depth_xi
  +     (cpsi*ctheta-spsi*sphi*stheta)*depth_yi
  +     spsi*cphi*depth_zi;
  depth_z:=(spsi*stheta-cpsi*sphi*ctheta)*depth_xi
  -    (spsi*ctheta+cpsi*sphi*stheta)*depth_yi
  +    cpsi*cphi*depth_zi;
enddef;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Nous ne remplissons une face que
% si elle est orient\'ee positivement.
% L'argument ``factor'' correspond \`a 
% l'\'eclairement.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

def fill_special(expr p,factor)=
  if turningnumber p>=0:
    fill p withcolor mycolor factor
  fi
enddef;

vardef mycolor expr factor=
  ((0.5*(1+factor))[light_min,light_max])*currentcolor
enddef;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% D\'etermination de l'ordre d'affichage (cylindres)
% sur une ligne, sur un rectangle...
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

def ordered_row(expr e_n,e_m,depth_n,n)=
  if depth_n<0:
    for $=1 step 1 until n: row_command(e_n,e_m,$); endfor
  else:
    for $=n step -1 until 1: row_command(e_n,e_m,$); endfor
  fi
enddef;

def ordered_rows(expr depth_n,depth_m,n,m)=
  if depth_n<0:
    if depth_m<0:
      if depth_n<depth_m:
	for $$=1 step 1 until m: for $=1 step 1 until n:
	    row_command($,$$); endfor endfor
      else:
	for $=1 step 1 until n: for $$=1 step 1 until m:
	    row_command($,$$); endfor endfor
      fi
    else:
      if depth_n<depth_m:
	for $$=m step -1 until 1: for $=1 step 1 until n:
	    row_command($,$$); endfor endfor
      else:
	for $=1 step 1 until n:  for $$=m step -1 until 1:
	    row_command($,$$); endfor endfor
      fi
    fi
  else:
    if depth_m<0:
      if depth_n<depth_m:
	for $$=1 step 1 until m: for $=n step -1 until 1:
	    row_command($,$$); endfor endfor
      else:
	for $=n step -1 until 1: for $$=1 step 1 until m:
	    row_command($,$$); endfor endfor
      fi
    else:
      if depth_n<depth_m:
	for $$=m step -1 until 1: for $=n step -1 until 1:
	    row_command($,$$); endfor endfor
      else:
	for $=n step -1 until 1: for $$=m step -1 until 1:
	    row_command($,$$); endfor endfor
      fi
    fi
  fi
enddef;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Cylindres divers
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

def z_cylinder(expr basepoint,radius,height,external)=
  lower_path:=(for i=0 upto cyl_steps:
      basepoint+radius*cosd(i*360/cyl_steps)*e_x
      +radius*sind(i*360/cyl_steps)*e_y --
    endfor basepoint+radius*e_x);
  upper_path:=lower_path shifted (height*e_z);
  for i=0 upto cyl_steps:
    fill_special(if external:
	point i of lower_path--point (i+1) of lower_path
	--point (i+1) of upper_path--point i of upper_path--cycle
      else:
	point i of upper_path--point (i+1) of upper_path
	--point (i+1) of lower_path--point i of lower_path--cycle
      fi,
      if external: else: - fi
      (cosd((i+0.5)*360/cyl_steps)*light_x
	+sind((i+0.5)*360/cyl_steps)*light_y));
  endfor
enddef;

def teton_command(expr $,$$)=
  z_cylinder(M1+($-0.5)*lego_unit*e_x+($$-0.5)*lego_unit*e_y,
    0.5lego_diam-lego_eps,lego_overshoot,true);
  fill_special(upper_path--cycle,light_z);
enddef;

def stick_command(expr e_n,e_m,$)=
  z_cylinder(M1'+$*lego_unit*e_n+0.5lego_unit*e_m,
    lego_thickness,lego_height-lego_thickness,true);
  fill reverse lower_path--cycle withcolor mycolor -light_z;
enddef;

def cyl_command(expr $,$$)=
  z_cylinder(M1'+$*lego_unit*e_x+$$*lego_unit*e_y,
    0.5lego_diam,lego_height-lego_thickness,false);
  temp_path:=lower_path;
  z_cylinder(M1'+$*lego_unit*e_x+$$*lego_unit*e_y,
    0.5lego_Diam,lego_height-lego_thickness,true);
  fill (reverse lower_path)--temp_path--cycle
  withcolor mycolor -light_z;
enddef;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Le plat principal : Adaptation à plusieurs legos sur une même figure par CP
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pair M[],M[]',M[]'',M[]''';

numeric currentnumber;
currentnumber=0;

color TR;
TR=(0,0,0);

vardef Lego_box(expr n,m,lego_color)=
  currentcolor:=lego_color;
  currentnumber:=currentnumber+1;
  %
  % Points clefs de la brique
  %
  if currentnumber=1:
    M1=0.5(-n*lego_unit*e_x-m*lego_unit*e_y+lego_height*e_z)+(lego_unit*redpart(TR)*e_x+lego_unit*greenpart(TR)*e_y+lego_height*bluepart(TR)*e_z);
  M1'-M1=-lego_height*e_z;
  M2-M1=M2'-M1'=n*lego_unit*e_x;
  M3-M1=M3'-M1'=n*lego_unit*e_x+m*lego_unit*e_y;
  M4-M1=M4'-M1'=m*lego_unit*e_y;
  M1'''-M1+lego_thickness*e_z=M1''-M1'=lego_thickness*(e_x+e_y);
  M2'''-M2+lego_thickness*e_z=M2''-M2'=lego_thickness*(-e_x+e_y);
  M3'''-M3+lego_thickness*e_z=M3''-M3'=lego_thickness*(-e_x-e_y);
  M4'''-M4+lego_thickness*e_z=M4''-M4'=lego_thickness*(e_x-e_y);
  else:
    M1:=0.5(-n*lego_unit*e_x-m*lego_unit*e_y+lego_height*e_z)+(lego_unit*redpart(TR)*e_x+lego_unit*greenpart(TR)*e_y+lego_height*bluepart(TR)*e_z);
    M1':=M1-lego_height*e_z;
    M2:=M1+n*lego_unit*e_x;
    M2':=M1'+n*lego_unit*e_x;
    M3:=M1+n*lego_unit*e_x+m*lego_unit*e_y;
    M3':=M1'+n*lego_unit*e_x+m*lego_unit*e_y;
    M4:=M1+m*lego_unit*e_y;
    M4':=M1'+m*lego_unit*e_y;
    M1''':=M1-lego_thickness*e_z+lego_thickness*(e_x+e_y);
    M1'':=M1'+lego_thickness*(e_x+e_y);
    M2''':=M2-lego_thickness*e_z+lego_thickness*(-e_x+e_y);
    M2'':=M2'+lego_thickness*(-e_x+e_y);
    M3''':=M3-lego_thickness*e_z+lego_thickness*(-e_x-e_y);
    M3'':=M3'+lego_thickness*(-e_x-e_y);
    M4''':=M4-lego_thickness*e_z+lego_thickness*(e_x-e_y);
    M4'':=M4'+lego_thickness*(e_x-e_y);
  fi;
  %%
  %% (face du dessus et t\'etons)
  %%             ou
  %% (t\'etons, faces lat\'erales internes
  %% et faces du dessous) 
  %%
  if turningnumber(M1--M2--M3--M4--cycle)>0:
    fill_special(M1--M2--M3--M4--cycle,light_z);
    let row_command=teton_command;
    ordered_rows(depth_x,depth_y,n,m);
  else:
    let row_command=teton_command;
    ordered_rows(depth_x,depth_y,n,m);
    fill_special(M4'''--M3'''--M2'''--M1'''--cycle,-light_z);
    fill_special(M1''--M1'''--M2'''--M2''--cycle,light_y);
    fill_special(M3''--M3'''--M4'''--M4''--cycle,-light_y);
    fill_special(M1''--M4''--M4'''--M1'''--cycle,light_x);
    fill_special(M2''--M2'''--M3'''--M3''--cycle,-light_x);
    if (n>1) and (m>1):
      let row_command=cyl_command;
      ordered_rows(depth_x,depth_y,n-1,m-1);
    else: if (n>1) and (m=1):
	let row_command=stick_command;
	ordered_row(e_x,e_y,depth_x,n-1); fi
      if (n=1) and (m>1):
	let row_command=stick_command;
	ordered_row(e_y,e_x,depth_y,m-1); fi
    fi
    fill_special(M4'--M3'--M2'--M1'--M4'--M4''--M1''--M2''
      --M3''--M4''--cycle,-light_z);
  fi;
 % %
 % % faces lat\'erales externes
 % %
  fill_special(M1--M1'--M2'--M2--cycle,-light_y);
  fill_special(M3--M3'--M4'--M4--cycle,light_y);
  fill_special(M1--M4--M4'--M1'--cycle,-light_x);
  fill_special(M2--M2'--M3'--M3--cycle,light_x);
enddef;

endinput