summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/latex/PfCBillard.tex
blob: 1dcde0e4c49bacb3d783320a87e68c818b6e5b33 (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
%%%
% Billard
%%%
\setKVdefault[ClesBillard]{Longueur=8cm,Largeur=5cm,Solution=false,Angle=70,Depart=0.5,Vrai=false}

\NewDocumentCommand\Billard{o m}{%
  \useKVdefault[ClesBillard]%
  \setKV[ClesBillard]{#1}%
  \ifboolKV[ClesBillard]{Solution}{%
    \MPBillardSolution{#2}{\useKV[ClesBillard]{Depart}}{\useKV[ClesBillard]{Angle}}%
    }{%
      \MPBillard{#2}{\useKV[ClesBillard]{Depart}}{\useKV[ClesBillard]{Angle}}%
    }%
}%

\def\MPBillard#1#2#3{%
  \mplibcodeinherit{disable}%
  \begin{Geometrie}[CoinHD={(\useKV[ClesBillard]{Longueur}+2cm,\useKV[ClesBillard]{Largeur}+3cm)}]
    boolean Vrai;
    Vrai:=\useKV[ClesBillard]{Vrai};
    % On définit la liste des 25 lettres alphabétiques dans laquelle on choisit autant de lettres que la longueur du mot
    % On a retiré la lettre Q pour des questions d'alignements esthétiques.
    vardef ChoixLettre=
    save Lettre,choixalea;
    string Lettre;
    choixalea=floor(uniformdeviate(26)+1);
    if choixalea=1:
    Lettre="A";
    elseif choixalea=2:
    Lettre="B";
    elseif choixalea=3:
    Lettre="C";
    elseif choixalea=4:
    Lettre="D";
    elseif choixalea=5:
    Lettre="E";
    elseif choixalea=6:
    Lettre="F";
    elseif choixalea=7:
    Lettre="G";
    elseif choixalea=8:
    Lettre="H";
    elseif choixalea=9:
    Lettre="I";
    elseif choixalea=10:
    Lettre="J";
    elseif choixalea=11:
    Lettre="K";
    elseif choixalea=12:
    Lettre="L";
    elseif choixalea=13:
    Lettre="M";
    elseif choixalea=14:
    Lettre="N";
    elseif choixalea=15:
    Lettre="O";
    elseif choixalea=16:
    Lettre="P";
    elseif choixalea=17:
    Lettre="O";
    elseif choixalea=18:
    Lettre="R";
    elseif choixalea=19:
    Lettre="S";
    elseif choixalea=20:
    Lettre="T";
    elseif choixalea=21:
    Lettre="U";
    elseif choixalea=22:
    Lettre="V";
    elseif choixalea=23:
    Lettre="W";
    elseif choixalea=24:
    Lettre="X";
    elseif choixalea=25:
    Lettre="Y";
    elseif choixalea=26:
    Lettre="Z";
    fi;
    Lettre
    enddef;
    % On crée un Quick Sort
    def QS(expr ndeb,nfin)=
    begingroup
    save v,m,x;
    if ndeb<nfin:
      v:=l[cpt[ndeb]];
      m:=ndeb;
      for i=(ndeb+1) upto nfin:
	if l[cpt[i]]<v:
	  m:=m+1;
	  x:=cpt[m];cpt[m]:=cpt[i];cpt[i]:=x;
	fi
      endfor;
      x:=cpt[m];cpt[m]:=cpt[ndeb];cpt[ndeb]:=x;
      QS(ndeb,m-1);
      QS(m+1,nfin);
    fi
    endgroup
    enddef;
    % On définit le rectangle
    pair M[],Co,Intermed;
    M1=u*(1,2);
    M2-M1=(\useKV[ClesBillard]{Longueur},0);
    M3-M2=(0,\useKV[ClesBillard]{Largeur});
    M4-M3=M1-M2;
    Co=iso(M1,M3);
    path rec,cote[];
    rec=polygone(M1,M2,M3,M4);
    cote1=segment(M1,M2);
    cote2=segment(M2,M3);
    cote3=segment(M3,M4);
    cote4=segment(M4,M1);
    trace rec;
    pair Pt[],FauxPt[];
    nbfaux=0;
    l1=#2;
    Pt[1]=point(l1) of rec;
    angleref=#3;
    if l1>3:
    angledepart=90+angleref;
    Pt[2]=demidroite(Pt[1],rotation(M1,Pt1,angledepart)) intersectionpoint (subpath(0,3) of rec);
    dotlabel.lft(TEX(substring(0,1) of #1),Pt[1]);
    elseif l1>2:
    angledepart=180+angleref;
    Pt[2]=demidroite(Pt[1],rotation(M4,Pt1,angledepart)) intersectionpoint ((subpath(3,4) of rec)--(subpath(0,2) of rec));
    dotlabel.top(TEX(substring(0,1) of #1),Pt[1]);
    elseif l1>1:
    angledepart=angleref-90;
    Pt[2]=demidroite(Pt[1],rotation(M3,Pt1,angledepart)) intersectionpoint ((subpath(2,4) of rec)--(subpath(0,1) of rec));
    dotlabel.rt(TEX(substring(0,1) of #1),Pt[1]);
    else:
    angledepart=angleref;
    Pt[2]=demidroite(Pt[1],rotation(M2,Pt1,angledepart)) intersectionpoint (subpath(1,4) of rec);
    dotlabel.bot(TEX(substring(0,1) of #1),Pt[1]);
    fi;
    tourne=90;
    numeric Blong;
    BLong:=length #1;
    % Détermination des points.
    if Vrai:
    for k=3 upto BLong:
    Intermed:=symetrie(Pt[k-2],Pt[k-1],Pt[k-1]+M2-M3);
    if (demidroite(1/1000[Pt[k-1],Intermed],Intermed) intersectiontimes rec)<>(-1,-1):
    Pt[k]:=demidroite(1/1000[Pt[k-1],Intermed],Intermed) intersectionpoint rec;
    else:
    Intermed:=symetrie(Pt[k-2],Pt[k-1],Pt[k-1]+M1-M2);
    Pt[k]:=demidroite(1/1000[Pt[k-1],Intermed],Intermed) intersectionpoint rec;
    fi;
    endfor;
    else:
    for k=3 upto BLong:
    Intermed:=rotation(Pt[k-2],Pt[k-1],tourne);
    if (demidroite(1/1000[Pt[k-1],Intermed],Intermed) intersectiontimes rec)<>(-1,-1):
    Pt[k]:=demidroite(1/1000[Pt[k-1],Intermed],Intermed) intersectionpoint rec;
    else:
    Intermed:=rotation(Pt[k-2],Pt[k-1],-tourne);
    Pt[k]:=demidroite(1/1000[Pt[k-1],Intermed],Intermed) intersectionpoint rec;
    fi;
    endfor;
    fi;
    % Ajout des faux points
    path SPath[];
    cpt[1]:=1;
    cpt[BLong+1]:=BLong+1;
    l[BLong+1]:=4;
    for k=2 upto BLong:
    SPath[k]=rec cutafter demidroite(Co,Pt[k]);
    l[k]=arclength SPath[k];
    l[k]:=arctime l[k] of rec;
    cpt[k]:=k;
    endfor;
    QS(1,BLong+1);
    for k=2 upto BLong+1:
    if l[cpt[k]]-l[cpt[k-1]]>0.3:
    nbfaux:=nbfaux+1;
    FauxPt[nbfaux]=point(l[cpt[k-1]]+0.1) of rec;
    nbfaux:=nbfaux+1;
    FauxPt[nbfaux]=point(l[cpt[k]]-0.1) of rec;
    fi;
    endfor;
    drawoptions();
    % Tracés
    drawarrow Pt[1]--(Pt[1]+1.5cm*unitvector(Pt[2]-Pt[1])) withpen pencircle scaled 1.25;
    % Labelisation
    for k=2 upto BLong:
    if (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[1])<>(-1,-1):
    dotlabel.bot(TEX(substring(k-1,k) of #1),Pt[k]);
    elseif (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[2])<>(-1,-1):
    dotlabel.rt(TEX(substring(k-1,k) of #1),Pt[k]);
    elseif (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[3])<>(-1,-1):
    dotlabel.top(TEX(substring(k-1,k) of #1),Pt[k]);
    elseif (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[4])<>(-1,-1):
    dotlabel.lft(TEX(substring(k-1,k) of #1),Pt[k]);
    fi;
    endfor;
    for k=1 upto nbfaux:
    if (demidroite(Co,FauxPt[k]) intersectiontimes cote[1])<>(-1,-1):
    dotlabel.bot(TEX(ChoixLettre),FauxPt[k]);
    elseif (demidroite(Co,FauxPt[k]) intersectiontimes cote[2])<>(-1,-1):
    dotlabel.rt(TEX(ChoixLettre),FauxPt[k]);
    elseif (demidroite(Co,FauxPt[k]) intersectiontimes cote[3])<>(-1,-1):
    dotlabel.top(TEX(ChoixLettre),FauxPt[k]);
    elseif (demidroite(Co,FauxPt[k]) intersectiontimes cote[4])<>(-1,-1):
    dotlabel.lft(TEX(ChoixLettre),FauxPt[k]);
    fi;
    endfor;
    picture Reponse;
    Reponse=image(
    trace segment((0,0),(7*BLong*mm-2mm,0)) dashed dashpattern(on5mm off2mm);
    );
    trace Reponse shifted((xpart(Co),5mm)-center Reponse);    
  \end{Geometrie}%
}%

\def\MPBillardSolution#1#2#3{%
  \mplibcodeinherit{enable}%
  \begin{mplibcode}%
    Figure(0,0,\useKV[ClesBillard]{Longueur}+2cm,\useKV[ClesBillard]{Largeur}+3cm);
    trace rec;
    % Tracés
    drawarrow Pt[1]--(Pt[1]+1.5cm*unitvector(Pt[2]-Pt[1])) withpen pencircle scaled 1.25;
    for k=1 upto BLong-1:
    trace segment(Pt[k],Pt[k+1]);
    endfor;
    if Vrai=false:
    for k=2 upto BLong-1:
    trace codeperp(Pt[k-1],Pt[k],Pt[k+1],5);
    endfor;
    fi;
    % Labelisation
    if l1>3:
    dotlabel.lft(TEX(substring(0,1) of #1),Pt[1]);
    elseif l1>2:
    dotlabel.top(TEX(substring(0,1) of #1),Pt[1]);
    elseif l1>1:
    dotlabel.rt(TEX(substring(0,1) of #1),Pt[1]);
    else:
    dotlabel.bot(TEX(substring(0,1) of #1),Pt[1]);
    fi;
    for k=2 upto BLong:
    if (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[1])<>(-1,-1):
    dotlabel.bot(TEX(substring(k-1,k) of #1),Pt[k]);
    elseif (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[2])<>(-1,-1):
    dotlabel.rt(TEX(substring(k-1,k) of #1),Pt[k]);
    elseif (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[3])<>(-1,-1):
    dotlabel.top(TEX(substring(k-1,k) of #1),Pt[k]);
    elseif (demidroite(1/1000[Pt[k-1],Pt[k]],Pt[k]) intersectiontimes cote[4])<>(-1,-1):
    dotlabel.lft(TEX(substring(k-1,k) of #1),Pt[k]);
    fi;
    endfor;
    trace Reponse shifted((xpart(Co),5mm)-center Reponse);
    % On affiche les lettres de la réponse.
    for k=1 upto BLong:
    label.top(TEX(substring(k-1,k) of #1),((xpart(Co),5mm)-center Reponse)+(k-1)*(7mm,0)+(2.5mm,0));
    endfor;
  \end{mplibcode}%
  \mplibcodeinherit{disable}%
}%