summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/latex/PfCMulPiecesPuzzle.tex
blob: 56a7ad6943b63fca13c4a8a419d9f94e8ba50450 (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
%%%
% Pièces de puzzle pour multiplication
%%%
%https://mirror.gutenberg-asso.fr/tex.loria.fr/prod-graph/metafun.pdf
\newtoks\toklistepuzzlea%

\def\UpdatetoksPuzzleA#1\nil{\addtotok\toklistepuzzlea{"#1",}}%

\setKVdefault[ClesPuzzleMul]{Solution=false,Unite=1cm,Couleur=LightSteelBlue}

\NewDocumentCommand\PuzzleMul{om}{%
  \useKVdefault[ClesPuzzleMul]%
  \setKV[ClesPuzzleMul]{#1}%
  \setsepchar{,}%
  \readlist*\PfCListePuzzle{#2}%
  \StrLen{\PfCListePuzzle[1]}[\TaillePuzzle]%
  \xdef\HauteurPuzzle{\PfCListePuzzlelen}%
  \toklistepuzzlea{}%
  \foreachitem\compteur\in\PfCListePuzzle{\expandafter\UpdatetoksPuzzleA\compteur\nil}%
  \BuildPuzzle{\the\toklistepuzzlea}%
}

\def\BuildPuzzleCode{%
  def PreDrawPuzzle ( expr ww, hh, uu ) =
  %randomseed := random_seed;
  def rand (expr x) =
    (((x)+(uniformdeviate 10))/100)
  enddef;
  w := ww;
  h := hh;
  un := uu/100;
  boolean swapone; swapone := false;
  boolean swaptwo; swaptwo := false;
  path p[], pw[], ph[];
  def buildpath (expr len, num) =
    for j=1 upto num:
      clearxy;
      swaptwo := not swaptwo;
      if odd num:
	swapone := not swaptwo;
      else:
	swapone := swaptwo;
      fi;
      for i=0 upto len+1:
	x[i]c = ((i*100)*un);
	y[i]c = ((j*100)*un);
	x[i]m = (x[i]c+50*un);
	if swapone:
	  y[i]m = (y[i]c-25*un)
	else:
	  y[i]m = (y[i]c+25*un)
	fi;
	x[i]l = (rand(85)[x[i]c,x[i]m]);
	x[i]r = (rand(15)[x[i]m,x[i+1]c]);
	y[i]l = y[i]r=y[i]c;
	x[i]ll = (rand(75)[x[i]c,x[i]m]);
	x[i]rr = (rand(25)[x[i]m,x[i+1]c]);
	y[i]ll = y[i]rr=.50[y[i]c,y[i]m];
      endfor;
      p[j] :=
	for i=0 upto len:
	z[i]c..z[i]l..z[i]ll..z[i]m..z[i]rr..z[i]r..
      endfor
      z[len+1]c ;
    endfor;
  enddef;
  pw[1] := (100*un,100*un)--(100*un,(h+1)*100*un);
  pw[w+1] := ((w+1)*100*un,100*un)--((w+1)*100*un,(h+1)*100*un);
  ph[1] := (100*un,100*un)--((w+1)*100*un,100*un);
  ph[h+1] := (100*un,(h+1)*100*un)--((w+1)*100*un,(h+1)*100*un);
  buildpath (w+1,h);
  for k=2 upto h:
    ph[k] := p[k] cutbefore pw[1];
    ph[k] := ph[k] cutafter pw[w+1];
  endfor;
  buildpath (h+1,w);
  for k=2 upto w:
    pw[k] := p[w-k+2] rotated 90 shifted ((w+2)*100*un,0);
    pw[k] := pw[k] cutbefore ph[1];
    pw[k] := pw[k] cutafter ph[h+1];
  endfor;
  path phw[][];
  for kh=1 upto h:
    for kw=1 upto w:
      phw[kh][kw] := buildcycle (ph[kh], pw[kw], ph[kh+1], pw[kw+1]);
    endfor;
  endfor;
enddef;
def DrawWholePuzzle =
  for k=1 upto h+1:
    draw ph[k] withpen pencircle scaled (2.5*un) shifted (-100*un,-100*un);
  endfor;
  for k=1 upto w+1:
    draw pw[k] withpen pencircle scaled (2.5*un) shifted (-100*un,-100*un);
  endfor;
enddef;
def DrawPuzzlePiece (expr ww, hh) =
draw phw[ww][hh] withpen pencircle scaled (2.5*un) shifted (-100*un,-100*un);
enddef;
def DrawPuzzleBorder =
draw (pw[1]--ph[1]--pw[w+1]--ph[h+1]--cycle)
withpen pencircle scaled (2.5*un) shifted (-100*un,-100*un);
enddef;
%
vardef AffichagePiece(text t)=
ligne:=Hauteur+1;
for p_=t:
ligne:=ligne-1;
colonne:=0;
for k=1 upto Taille:
colonne:=colonne+1;
if substring(k-1,k) of p_="X":
DrawPuzzlePiece(ligne,colonne);
else:
if Solution:
path titi;
titi=phw[ligne][colonne];
fill (titi shifted (-100*un,-100*un)) withcolor CoulSolution;
draw titi withpen pencircle scaled (2.5*un) shifted (-100*un,-100*un);
fi;
fi;
endfor;
endfor;
enddef;
}%

\NewDocumentCommand\BuildPuzzle{m}{%
  \ifluatex
    \mplibforcehmode
    \begin{mplibcode}
      \BuildPuzzleCode
      Taille:=\TaillePuzzle;
      Hauteur:=\HauteurPuzzle;
      Unite:=\useKV[ClesPuzzleMul]{Unite};
      boolean Solution;
      Solution=\useKV[ClesPuzzleMul]{Solution};
      color CoulSolution;
      CoulSolution=\useKV[ClesPuzzleMul]{Couleur};
      
      PreDrawPuzzle(Taille, Hauteur, Unite);
      
      AffichagePiece(#1);
    \end{mplibcode}
  \else%
    \begin{mpost}[mpsettings={\BuildPuzzleCode;Taille:=\TaillePuzzle;Hauteur:=\HauteurPuzzle;Unite:=\useKV[ClesPuzzleMul]{Unite};boolean Solution;Solution=\useKV[ClesPuzzleMul]{Solution};color CoulSolution;CoulSolution=\useKV[ClesPuzzleMul]{Couleur};}]
      PreDrawPuzzle(Taille, Hauteur, Unite);
      
      AffichagePiece(#1);
    \end{mpost}
  \fi%
}%