summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/metapost/PfCLabyNombre.mp
blob: e6877ef34eedc80b0a16f24de0520087507310fd (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
%Pile des cases du chemin
pair PileChemin[];
numeric indiceChemin;
indiceChemin=0;

vardef RAZPileChemin=
  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=
  if indiceChemin<1:
    RAZPileChemin;
    for k=-1 upto LargeurLaby:
      for l=-1 upto LongueurLaby:
	if (k=-1) or (k=LargeurLaby) or (l=-1) or (l=LongueurLaby):
	  CaseExploree[k][l]:=true;
	else:
	  CaseExploree[k][l]:=false;
	fi;
      endfor;
    endfor;
    Mobile:=Depart;
    PushChemin((choixligneD,choixcolonneD));
    CaseExploree[choixligneD][choixcolonneD]:=true;
    VoisinDispo(choixligneD,choixcolonneD);
  else:
    if indiceChemin>0:
      CaseExploree[xpart(PileChemin[indiceChemin])][ypart(PileChemin[indiceChemin])]:=true;
    fi;
    indiceChemin:=indiceChemin-1;
  fi;
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;

def PopVoisin=
  if indiceVoisin<0:
    %message("La pile n'existe pas");
  else:
    indiceVoisin:=indiceVoisin-1;
  fi;
enddef;

vardef VoisinDispo(expr la,lo)=
  RAZPileVoisin;
  numeric nbvoisin;
  nbvoisin=0;
  if (la>-1) and (la<LargeurLaby) and (lo>-1) and (lo<LongueurLaby):
    if CaseExploree[la+1][lo]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la+1,lo));
    fi;
    if CaseExploree[la-1][lo]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la-1,lo));
    fi;
    if CaseExploree[la][lo+1]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la,lo+1));
    fi;
    if CaseExploree[la][lo-1]=false:
      nbvoisin:=nbvoisin+1;
      PushVoisin((la,lo-1));
    fi;
  fi;
enddef;

vardef InitialisationLabyrinthe(expr long,larg)=
  LargeurLaby:=larg;
  LongueurLaby:=long;
  pair M[][],N[][];%centre de la case, coinbg de la case
  boolean CaseExploree[][];%Case Exploree ou pas
  %on initialise les cases comme non explorées pour celles qui sont à l'intérieur du labyrinthe
  %n=0;
  for k=-1 upto larg:
    for l=-1 upto long:
      M[k][l]:=u*(0.5,-0.5)+u*(l,-k);
      N[k][l]:=u*(l,-k-1);
      if (k=-1) or (k=larg) or (l=-1) or (l=long):
	CaseExploree[k][l]:=true;
      else:
	CaseExploree[k][l]:=false;
      fi;
    endfor;
  endfor;
enddef;

vardef TraceLabyrinthe=
  save $;
  picture $;
  $=image(
      if Murs:
      for k=1 upto LargeurLaby:
	trace murgauche shifted(u*(0,-k));
      endfor;
      for k=0 upto LongueurLaby-1:
	trace murhaut shifted(u*(k,-1));
      endfor;
      for k=1 upto LargeurLaby:
	trace murdroit shifted(u*(LongueurLaby-1,-k));
      endfor;
      for k=0 upto LongueurLaby-1:
	trace murbas shifted(u*(k,-LargeurLaby));
      endfor;
      for k=1 upto LargeurLaby-1:
	for l=1 upto LongueurLaby-1:
	  trace Separation shifted(u*(l,-k));
	endfor;
      endfor;
    else:
      for k=0 upto LargeurLaby:
	trace u*(0,0-k)--u*(LongueurLaby,0-k);
      endfor;
      for k=0 upto LongueurLaby:
	trace u*(0+k,0)--u*(0+k,-LargeurLaby);
      endfor;
    fi;
    );
  $
enddef;    

vardef murgauche=
  save $;
  picture $;
  path pl[];
  $=image(
    %pl1=(0,0)--u*(0.15,0)--u*(0.15,0.2){dir180}..u*(0.08+uniformdeviate(0.05),0.2+uniformdeviate(0.1))..u*(0.09+uniformdeviate(0.015),0.3+uniformdeviate(0.2))..u*(0.08+uniformdeviate(0.04),0.5+uniformdeviate(0.2))..{dir0}u*(0.15,0.8)--u*(0.15,1)--u*(0,1)--cycle;
      pl1=(0,0)--u*(0.1,0)..u*(0.02+uniformdeviate(0.05),0.25)..u*(0.1-uniformdeviate(0.03),0.5)..u*(0.02+uniformdeviate(0.05),0.75)..u*(0.1,1)--u*(0,1)--cycle;
    remplis pl[1];
    trace pl[1];
    );
  $
enddef;%ok

vardef murhaut=
  save $;
  picture $;
  path pl[];
  $=image(
      pl1=u*(0,0.8)--u*(0,1)--u*(1,1)--u*(1,0.8)..u*(0.8+uniformdeviate(0.1),0.8+uniformdeviate(0.1))..u*(0.4+uniformdeviate(0.2),0.8-uniformdeviate(0.1))..u*(0.2+uniformdeviate(0.2),0.8+uniformdeviate(0.1))--cycle;
    remplis pl[1];
    trace pl[1];
    );
  $
enddef;

vardef murdroit=
  save $;
  picture $;
  path pl[];
  $=image(
    pl1=u*(1,0)--u*(1-0.15,0)--u*(1-0.15,0.2){dir180}..u*(1-0.08+uniformdeviate(0.05),0.2+uniformdeviate(0.1))..u*(1-0.09+uniformdeviate(0.015),0.3+uniformdeviate(0.2))..u*(1-0.08+uniformdeviate(0.04),0.5+uniformdeviate(0.2))..{dir0}u*(1-0.15,0.8)--u*(1-0.15,1)--u*(1-0,1)--cycle;
    remplis pl[1];
    trace pl[1];
    );
  $
enddef;%ok

vardef murbas=
  save $;
  picture $;
  path pl[];
  $=image(
      pl1=u*(0,0.2)--u*(0,0)--u*(1,0)--u*(1,0.2)..u*(0.8+uniformdeviate(0.1),0.2+uniformdeviate(0.1))..u*(0.4+uniformdeviate(0.2),0.2-uniformdeviate(0.1))..u*(0.2+uniformdeviate(0.2),0.2+uniformdeviate(0.1))--cycle;
    remplis pl[1];
    trace pl[1];
    );
  $
enddef;

vardef Separation=
  save $;
  picture $;
  path pl[];
  $=image(
      pl1=u*(0.2,0)..u*(0.05+uniformdeviate(0.05),0.05+uniformdeviate(0.05))..u*(0,0.2)..u*(-0.1+uniformdeviate(0.05),0.05+uniformdeviate(0.05))..u*(-0.2,0)..u*(-0.1+uniformdeviate(0.05),-0.05-uniformdeviate(0.05))..u*(0,-0.2)..u*(0.05+uniformdeviate(0.05),-0.05-uniformdeviate(0.05))..cycle;
    remplis pl[1];
    trace pl[1];
    );
  $
enddef;

endinput