-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackside.js
237 lines (181 loc) · 6.35 KB
/
backside.js
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
function Chenille(canvas, nbAnneaux, rayon){
//propriete:
// sa tete,
//la liste de ses anneaux (sous forme d'un tableau),
// le canvas dans lequel il s'affiche
var largeur = canvas.width;
var hauteur = canvas.height;
this.canvas=canvas;
//créé tete et anneaux
this.tete = new Tete(largeur/2, hauteur/2 ,rayon,0);
this.anneaux = new Array(nbAnneaux);
var dx = rayon;
for (var i=1; i < nbAnneaux + 1 ; i++) {
dx = rayon*i;
this.anneaux[i-1] = new Anneau(largeur/2 - dx, hauteur/2 ,rayon);
}
//methodes
//dessiner()
//deplacer()
};
Chenille.prototype.dessiner = function(){
var ctxt = this.canvas.getContext("2d");
this.tete.dessiner(ctxt);
for (var i=0; i< this.anneaux.length-1; i++){
this.anneaux[i].dessiner(ctxt);
}
};
Chenille.prototype.deplacer= function(){
//sauvegarde coord de la tete avant depl.
var ancien_x_tete = this.tete.x;
var ancien_y_tete = this.tete.y;
//this.anneaux[0].dessiner(this.canvas.getContext("2d"));
for (var i=this.anneaux.length - 1; i>0; i--)
{
this.anneaux[i].x = this.anneaux[i-1].x;
this.anneaux[i].y = this.anneaux[i-1].y;
//this.anneaux[i].dessiner(this.canvas.getContext("2d"));
}
this.anneaux[0].x = ancien_x_tete;
this.anneaux[0].y = ancien_y_tete;
this.tete.deplacer(this.canvas);
};
function Tete(xInit, yInit, rInit, cap){
//propriete :
//coord x de son centre,
//coord y de son centre,
//son rayon ,
//cap,
//la couleur de la tete est differete des anneaux,
//
this.x = xInit;
this.y = yInit;
this.r = rInit;
this.cap = cap;
//methode: deplacer,
//afficher,
//devierCap(deltaC),
//deplacerSelonCap(),
//capOK(canvas)
};
Tete.prototype.dessiner = function(ctxt){
//affiche l'anneau
ctxt.beginPath();
ctxt.arc(this.x,this.y,this.r,0,2*Math.PI);
ctxt.strokeStyle="black";
ctxt.fillStyle="#027c02";
ctxt.stroke();
ctxt.fill();
};
Tete.prototype.deplacer = function(canvas){
var deltaC = 10; //10deg fixé arbitrairement
// calcul un cap aléatoirement entre -30 et 30deg.
var cap_aleatoire = Math.random() * (30 - (-30)) + (-30);
console.log(this.cap);
this.cap= this.cap + cap_aleatoire;
//deplacerSelonCap();
while (this.capOK(canvas) === 0 )
{ // le cap est mauvais, il faut le devier de 10deg
this.devierCap(deltaC);
}
this.deplacerSelonCap();
};
Tete.prototype.capOK = function(canvas){
//prédit les coord suivantes de la tete et retourne si ce cap est possible ou non.
var angle_rad = this.cap * Math.PI / 180;
var Xsuiv = this.x + this.r * Math.cos(angle_rad);
var Ysuiv = this.y + this.r * Math.sin(angle_rad);
var bool = 1;
if (Xsuiv > canvas.width - this.r || Xsuiv < this.r || Ysuiv > canvas.height - this.r || Ysuiv < this.r)
{
bool=0;
}
return bool;
};
Tete.prototype.devierCap = function(deltaC){
this.cap = this.cap + deltaC;
console.log(this.cap + " apres deviationcap");
console.log("X vaut: " +this.x );
console.log("Y vaut: " +this.y );
};
Tete.prototype.deplacerSelonCap = function(){
var cap_rad = this.cap * Math.PI / 180;
this.x = this.x + this.r * Math.cos(cap_rad);
this.y = this.y + this.r * Math.sin(cap_rad);
};
function Anneau(xInit,yInit,rInit ){
//constructeur
//job : fixe position initiale de l'anneau et son rayon d'apres les parametres fournis
//this.canvas = canvas;
this.x = xInit;
this.y = yInit;
this.r = rInit;
//propriete:
//coord x de son centre,
//coord y de son centre,
//rayon de son cercle,
//methode: deplacer, afficher
};
Anneau.prototype.placerA = function(px,py){
};
Anneau.prototype.dessiner = function(ctxt){
//affiche l'anneau
ctxt.beginPath();
ctxt.arc(this.x,this.y,this.r,0,2*Math.PI);
ctxt.strokeStyle="#057005";
ctxt.fillStyle="#0cad0c";
ctxt.stroke();
ctxt.fill();
};
function creation_tab_chenilles(canvas,nbchenilles, nbanneaux, rayon){
var tab_chenilles = Array(nbchenilles);
for (var i=0; i< tab_chenilles.length; i++){
tab_chenilles[i] = new Chenille(canvas, nbanneaux, rayon);
}
return tab_chenilles;
}
function init(){
// dessiner une chenille
// creation de l'objet
var canvas = document.getElementById("myCanvas");
// var hauteur = canvas.height;
// var largeur = canvas.width;
var ctxt = canvas.getContext("2d");
//Creation d'un tableau de chenilles
// une chenille de 10 anneaux + 1 tete , rayon=10
var tab_chenilles =Array();
tab_chenilles = creation_tab_chenilles(canvas,1,3,10);
for (i=0; i<tab_chenilles.length; i++){
tab_chenilles[i].dessiner();
}
document.getElementById("startBtn").onclick = function() {
document.getElementById("settings").disabled=true;
document.getElementById("stopBtn").disabled=false;
document.getElementById("startBtn").disabled = true;
// dessine une chenille
timerId = setInterval( function()
{
// la fonction invoquée périodiquement (toutes les 20 ms) par le timer
ctxt.clearRect(0, 0, canvas.width, canvas.height);
for (i=0; i<tab_chenilles.length; i++)
{
tab_chenilles[i].deplacer();
tab_chenilles[i].dessiner();
}
}
, 100);
};
document.getElementById("stopBtn").onclick = function() {
document.getElementById("startBtn").disabled = false;
document.getElementById("settings").disabled = false;
// interruption du timer
clearInterval(timerId);
};
document.getElementById("settings").onclick = function() {
var nbanneaux = parseInt(document.getElementById("anneaux").value);
var rayon = parseInt(document.getElementById("rayon").value);
var nbchenilles = parseInt(document.getElementById("nbchenilles").value);
ctxt.clearRect(0, 0, canvas.width, canvas.height);
tab_chenilles = creation_tab_chenilles(canvas,nbchenilles,nbanneaux,rayon);
};
}