summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/latex/PfCFractionNombre.tex
blob: 08e9936d529a2f8a343da56b0a9ca119a1d59546 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
%%%
% Fraction Nombre
%%%
\newtoks\tokFractionNombrePerso%
\def\UpdatetoksFN#1\nil{\addtotok\tokFractionNombrePerso{"#1",}}%

\setKVdefault[ClesFN]{Unite=5cm,Aire=false,Solution=false,Quadrillage=false,PasQuad=5mm,HautQuad=3,LargQuad=8,Vide=false,Creation=false,Couleur=blue,UniteAire=1x1,Pointilles=false,Tentatives=10}%
\defKV[ClesFN]{Perso=\setKV[ClesFN]{Creation}\xdef\PfCFooCheminFN{#1}}%

\NewDocumentCommand\FractionNombre{om}{%
  \useKVdefault[ClesFN]%
  \setKV[ClesFN]{#1}%
  \tokFractionNombrePerso{}%
  \ifboolKV[ClesFN]{Creation}{%
    \setsepchar{,}\ignoreemptyitems%
    \readlist*\PfCListeDeplacements{\PfCFooCheminFN}%
    \foreachitem\compteur\in\PfCListeDeplacements{\expandafter\UpdatetoksFN\compteur\nil}%
  }{}%
  \StrCut{#2}{/}\PfCNumA\PfCNumB%
  \ifboolKV[ClesFN]{Aire}{%
    \StrCut{\useKV[ClesFN]{UniteAire}}{x}\PfCAirex\PfCAirey%
    \ifboolKV[ClesFN]{Creation}{%
        \BuildFractionNombreAirePerso{\PfCNumA}{\PfCNumB}{\PfCAirex}{\PfCAirey}{\the\tokFractionNombrePerso}%
      }{%
      \BuildFractionNombreAire{\PfCNumA}{\PfCNumB}{\PfCAirex}{\PfCAirey}%
    }%
  }{%
    \ifboolKV[ClesFN]{Quadrillage}{%
      \BuildFractionNombreQuadrillage{\PfCNumA}{\PfCNumB}{\the\tokFractionNombrePerso}%
    }{%
      \BuildFractionNombreSegment{\PfCNumA}{\PfCNumB}%
    }%
  }%
}%

\NewDocumentCommand\BuildFractionNombreAirePerso{mmmmm}{%
  \ifluatex%
    \mplibforcehmode%
    \begin{mplibcode}
      vardef LireMotif(text t)=
      lmotif=0;
      hmotif=0;
      for p_=t:
      hmotif:=hmotif+1;
      if lmotif<(length p_):
      lmotif:=length p_;
      fi;
      endfor;
      enddef;
      %
      vardef PlacerMotif(text t)=
      ChoixLigneD=hmotif+ceiling(uniformdeviate(HautQuad-1-hmotif));
      ChoixColonneD=ceiling(uniformdeviate(LargQuad-#3-1-lmotif));
      Colonne:=ChoixColonneD;
      Ligne=ChoixLigneD;
      for p_=t:
      Colonne:=ChoixColonneD;
      for k=0 upto ((length p_)-1):
      if (substring(k,k+1) of p_)="x":
      fill ((unitsquare scaled 5mm) shifted (5mm*(Colonne-1,Ligne-1))) withcolor LightSteelBlue;
      fi;
      Colonne:=Colonne+1;
      endfor;
      Ligne:=Ligne-1;
      endfor;
      enddef;
      %
      PasQuad=\useKV[ClesFN]{PasQuad};
      HautQuad=\useKV[ClesFN]{HautQuad};
      LargQuad=\useKV[ClesFN]{LargQuad};
      pair A,B,C,D,U[];
      A=(0,0);
      B-A=PasQuad*(LargQuad,0);
      C-B=PasQuad*(0,HautQuad);
      D-C=A-B;
      % l'unité
      U0=C;
      U1-U0=#3*PasQuad*(-1,0);
      U3-U0=#4*PasQuad*(0,-1);
      U2-U3=U1-U0;
      %
      LireMotif(#5);
      PlacerMotif(#5);
      % Tracage
      fill polygone(U0,U1,U2,U3) withcolor 0.7white;
      label.top(TEX("\textbf{1 ua}"),iso(U1,U0));
      %Le quadrillage
      trace polygone(A,B,C,D);
      for k=1 upto LargQuad-1:
      trace (k/LargQuad)[A,B]--(k/LargQuad)[D,C];
      endfor;
      for k=1 upto HautQuad-1:
      trace (k/HautQuad)[A,D]--(k/HautQuad)[B,C];
      endfor;
    \end{mplibcode}
  \fi%
}%

\NewDocumentCommand\BuildFractionNombreAire{mmmm}{%
  \ifluatex%
    \mplibforcehmode%
    \begin{mplibcode}
      boolean Impossible[][];
      %
      vardef InitialisationZone=
      for k=-1 upto HautQuad+1:
      for l=-1 upto LargQuad+1:
      Impossible[k][l]=true;
      endfor;
      endfor;
      for k=1 upto HautQuad-1:
      for l=1 upto LargQuad-#3-1:
      Impossible[k][l]:=false;
      endfor;
      endfor;
      enddef;
      %
      vardef RAZZone=
      pair PileChemin[];
      indiceChemin:=0;
      enddef;
      % 
      def PushChemin(expr tt)=
      if indiceChemin<1:
      PileChemin[1]:=tt;
      indiceChemin:=1;
      else:
      PileChemin[indiceChemin+1]:=tt;
      indiceChemin:=indiceChemin+1;
      fi;
      enddef;
      %
      def PopChemin=
      tentative:=tentative+1;
      currentpicture:=nullpicture;
      RAZZone;
      for k=-1 upto HautQuad:
      for l=-1 upto LargQuad:
      Impossible[k][l]:=true;
      endfor;
      endfor;
      for k=1 upto HautQuad-1:
      for l=1 upto LargQuad-#3-1:
      Impossible[k][l]:=false;
      endfor;
      endfor;
      %Redemarrage
      nbcaseschoisies:=1;
      PushChemin((ChoixLigneD,ChoixColonneD));
      fill ((unitsquare scaled 5mm) shifted (5mm*(ChoixColonneD-1,ChoixLigneD-1))) withcolor LightSteelBlue;
      Impossible[ChoixLigneD][ChoixColonneD]:=true;
      VoisinDispo(ChoixLigneD,ChoixColonneD);
      enddef;
      %
      %Pile des cases voisines de la case parcourue
      pair PileVoisin[];
      numeric indiceVoisin;
      indiceVoisin=0;
      % 
      vardef RAZPileVoisin=
      indiceVoisin:=0;
      enddef;
      %
      def PushVoisin(expr tt)=
      if indiceVoisin<1:
      PileVoisin[1]:=tt;
      indiceVoisin:=1;
      else:
      PileVoisin[indiceVoisin+1]:=tt;
      indiceVoisin:=indiceVoisin+1;
      fi;
      enddef;
      % 
      vardef VoisinDispo(expr la,lo)=
      RAZPileVoisin;
      numeric nbvoisin;
      nbvoisin=0;
      if Impossible[la+1][lo]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la+1,lo));
      fi;
      if Impossible[la-1][lo]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la-1,lo));
      fi;
      if Impossible[la][lo+1]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la,lo+1));
      fi;
      if Impossible[la][lo-1]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la,lo-1));
      fi;
      enddef;
      %
      boolean Vide;
      Vide=\useKV[ClesFN]{Vide};
      PasQuad=\useKV[ClesFN]{PasQuad};
      HautQuad=\useKV[ClesFN]{HautQuad};
      LargQuad=\useKV[ClesFN]{LargQuad};
      Tentatives=\useKV[ClesFN]{Tentatives};
      pair A,B,C,D,U[];
      A=(0,0);
      B-A=PasQuad*(LargQuad,0);
      C-B=PasQuad*(0,HautQuad);
      D-C=A-B;
      % l'unité
      U0=C;
      U1-U0=#3*PasQuad*(-1,0);
      U3-U0=#4*PasQuad*(0,-1);
      U2-U3=U1-U0;
      % Le nombres total de cases à colorier
      NbTotalCasesAColorier=((#1*#3*#4)/#2);
      NbTotalCasesRestantes=HautQuad*(LargQuad-#3-1);
      %
      InitialisationZone;
      RAZZone;
      %
      ChoixLigneD=ceiling(uniformdeviate(HautQuad-1));
      ChoixColonneD=ceiling(uniformdeviate(LargQuad-#3-1));
      %
      tentative:=1;
      if Vide=false:
      PushChemin((ChoixLigneD,ChoixColonneD));
      nbcaseschoisies=1;
      fill ((unitsquare scaled 5mm) shifted (5mm*(ChoixColonneD-1,ChoixLigneD-1))) withcolor LightSteelBlue;
      Impossible[ChoixLigneD][ChoixColonneD]:=true;
      VoisinDispo(ChoixLigneD,ChoixColonneD);
      % Le "parcours"
      forever: exitif (nbcaseschoisies>NbTotalCasesAColorier-1) or (tentative>Tentatives);
      nb:=ceiling(uniformdeviate(nbvoisin));
      if nb>0:
      for k=1 upto nbvoisin:
      Impossible[xpart(PileVoisin[nb])][ypart(PileVoisin[nb])]:=true;
      endfor;
      PushChemin((xpart(PileVoisin[nb]),ypart(PileVoisin[nb])));
      nbcaseschoisies:=nbcaseschoisies+1;
      fill ((unitsquare scaled 5mm) shifted (5mm*(ypart(PileVoisin[nb])-1,xpart(PileVoisin[nb])-1))) withcolor LightSteelBlue;
      VoisinDispo(xpart(PileChemin[indiceChemin]),ypart(PileChemin[indiceChemin]));
      else:
      PopChemin;
      fi;
      endfor;
      fi;
      % Tracage
      if tentative>Tentatives:
      currentpicture:=nullpicture;
      picture MiseEnGarde;
      MiseEnGarde=image(
      label(TEX("\begin{minipage}{0.5\linewidth}
        La construction automatique a échouée. Relancez en augmentant le nombre de tentatives ou en augmentant les dimensions du quadrillage (Hauteur,Largeur)
      \end{minipage}"),(0,0));
      );
      draw (((llcorner MiseEnGarde)--(lrcorner MiseEnGarde)--(urcorner MiseEnGarde)--(ulcorner MiseEnGarde)--cycle) scaled 1.1) withpen pensquare scaled 2;
      draw MiseEnGarde;
      else:
      fill polygone(U0,U1,U2,U3) withcolor 0.7white;
      label.top(TEX("\textbf{1 ua}"),iso(U1,U0));
      %Le quadrillage
      trace polygone(A,B,C,D);
      for k=1 upto LargQuad-1:
      trace (k/LargQuad)[A,B]--(k/LargQuad)[D,C];
      endfor;
      for k=1 upto HautQuad-1:
      trace (k/HautQuad)[A,D]--(k/HautQuad)[B,C];
      endfor;
      fi;
    \end{mplibcode}
  \fi
}%

\NewDocumentCommand\BuildFractionNombreSegment{mm}{%
  \ifluatex%
    \mplibforcehmode%
    \begin{mplibcode}
      Unite:=\useKV[ClesFN]{Unite};
      boolean Solution,Vide;
      Solution=\useKV[ClesFN]{Solution};
      Vide=\useKV[ClesFN]{Vide};
      color CoulFN;
      CoulFN=\useKV[ClesFN]{Couleur};
      Num:=#1;
      Pas:=#2;
      pair A,B,C,D;
      A=(0,0);
      B=Unite*(1,0);
      if Num>Pas:
      C=((Num+1)/Pas)[A,B];
      Total=Num+1;
      else:
      C=B;
      Total=Pas;
      fi;
      D=(Num/Pas)[A,B];      
      trace segment(A,C);
      for k=0 upto Total:
      trace ((0,-1mm)--(0,1mm)) shifted ((k/Pas)[A,B]);
      endfor;
      trace cotationmil(A,B,5mm,15,TEX("1 ul"));
      if Solution:
      draw (A--D) withpen pencircle scaled 2 withcolor CoulFN;
      trace cotationmil(A,D,-5mm,25,TEX("$?=\dfrac{"&decimal(#1)&"}{"&decimal(#2)&"}$~ul"));
      else:
      if Vide:
      else:
      trace cotationmil(A,D,-5mm,25,TEX("$?=\dots$~ul"));
      fi;
      fi;
    \end{mplibcode}
  \fi%
}%

\NewDocumentCommand\BuildFractionNombreQuadrillage{mmm}{%
  \ifluatex%
    \mplibforcehmode%
    \begin{mplibcode}
      Unite:=#2;%\useKV[ClesFN]{Unite};%en carreaux
      HautQuad:=\useKV[ClesFN]{HautQuad};
      Largeur:=Unite;%en carreaux
      boolean Solution,Vide,Creation,Pointilles;
      Solution=\useKV[ClesFN]{Solution};
      Vide=\useKV[ClesFN]{Vide};
      Creation=\useKV[ClesFN]{Creation};
      Pointilles=\useKV[ClesFN]{Pointilles};
      color CoulFN;
      CoulFN=\useKV[ClesFN]{Couleur};
      Num:=#1;
      Pas:=#2;
      PasQuad:=\useKV[ClesFN]{PasQuad};
      pair A,B,C,D;
      A=(0,0);
      B=(((#1 div #2)+1)*Unite+1)*PasQuad*(1,0);
      C-B=(0,HautQuad*PasQuad);
      D-C=A-B;
      trace polygone(A,B,C,D);
      TotalLargeur=(((#1 div #2)+1)*Unite+1);
      for k=1 upto TotalLargeur-1:
      trace (k/TotalLargeur)[A,B]--(k/TotalLargeur)[D,C];
      endfor;
      for k=1 upto HautQuad-1:
      trace (k/HautQuad)[A,D]--(k/HautQuad)[B,C];
      endfor;
      % Macro pour le chemin perso
      vardef RemplirChemin(text t)=
      nbpoints:=0;
      % On compte le dénivelé
      denivele=0;
      denivmax:=0;
      denivmin:=0;
      for p_=t:
      if p_="+":
      denivmax:=denivmax+1;
      elseif p_="-":
      denivmin:=denivmin-1;
      fi;
      endfor;
      depart:=-(denivmin)+floor(uniformdeviate(HautQuad-2-abs(denivmax-abs(denivmin))));
      ch[0]=PasQuad*(0,depart);
      for p_=t:
      nbpoints:=nbpoints+1;
      if p_=">":
      ch[nbpoints]=ch[nbpoints-1]+PasQuad*(1,0);
      elseif p_="<":
      ch[nbpoints]=ch[nbpoints-1]+PasQuad*(-1,0);
      elseif p_="+":
      ch[nbpoints]=ch[nbpoints-1]+PasQuad*(0,1);
      elseif p_="-":
      ch[nbpoints]=ch[nbpoints-1]+PasQuad*(0,-1);
      fi;
      endfor;
      enddef;
      % On choisit le départ.
      pair ch[];
      numeric xe[],ye[];
      if Creation:
      RemplirChemin(#3);
      else:
      depart:=floor(uniformdeviate(HautQuad-1));
      ch[0]=PasQuad*(0,depart);
      xe0=0;
      ye0=depart;
      for k=1 upto #1:
      if (k mod 2)=1:
      ch[k]-ch[k-1]=PasQuad*(1,0);
      xe[k]=xe[k-1]+1;
      ye[k]=ye[k-1];
      else:
      if ye[k-1]=0:
      alea:=uniformdeviate(2);
      if alea<1:
      ch[k]-ch[k-1]=PasQuad*(0,1);
      xe[k]=xe[k-1];
      ye[k]=ye[k-1]+1;
      else:
      ch[k]-ch[k-1]=PasQuad*(1,0);
      xe[k]=xe[k-1]+1;
      ye[k]=ye[k-1];
      fi;
      elseif ye[k-1]=2:
      alea:=uniformdeviate(2);
      if alea<1:
      ch[k]-ch[k-1]=PasQuad*(0,-1);
      xe[k]=xe[k-1];
      ye[k]=ye[k-1]-1;
      else:
      ch[k]-ch[k-1]=PasQuad*(1,0);
      xe[k]=xe[k-1]+1;
      ye[k]=ye[k-1];
      fi;
      else:
      alea:=uniformdeviate(3);
      if alea<1:
      ch[k]-ch[k-1]=PasQuad*(0,1);
      xe[k]=xe[k-1];
      ye[k]=ye[k-1]+1;
      elseif alea<2:
      ch[k]-ch[k-1]=PasQuad*(1,0);
      xe[k]=xe[k-1]+1;
      ye[k]=ye[k-1];
      else:
      ch[k]-ch[k-1]=PasQuad*(0,-1);
      xe[k]=xe[k-1];
      ye[k]=ye[k-1]-1;
      fi;
      fi;      
      fi;
      endfor;
      fi;
      if Vide=false:
      trace ch[0] for k=1 upto #1:--ch[k] endfor withpen pencircle scaled 2 if Pointilles: dashed evenly else: withcolor CoulFN fi;
      fi;
      trace cotationmil(D,(D+#2*(PasQuad,0)),5mm,15,TEX("1 ul"));
      trace segment(D,D+#2*(PasQuad,0)) withpen pencircle scaled 1.5;
    \end{mplibcode}
  \fi
}%