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
// ------------------ SVG et canvas ------------------
// Cette fonction calcule tous les points de la fonction mathématique.
// Elle place chaque coordonnée "x" et "y" dans les tableaux "pointX" et "pointY"
// A la fin, elle choisi la méthode d'affichage entre :
// 1) une seule image SVG
// 2) plusieurs images SVG
function evaluerSVG(eq) {
borneXGauche = parseFloat(document.getElementById("borneXGauche").value)
borneXDroite = parseFloat(document.getElementById("borneXDroite").value)
borneYGauche = parseFloat(document.getElementById("borneYGauche").value)
borneYDroite = parseFloat(document.getElementById("borneYDroite").value)
multiplicateurX = largeur/Math.abs(borneXDroite - borneXGauche)
multiplicateurY = hauteur/Math.abs(borneYDroite - borneYGauche)
lineWidth = document.getElementById("inputTaille").value
var i = 0
var y1, p1
for(x=borneXGauche; x<=(borneXDroite+5*precision); x=x+precision){
y = eval(eq)
if(!isNaN(y)){
i++
pointX[i] = (x - borneXGauche) * multiplicateurX
pointY[i] = hauteur - ((y - borneYGauche) * multiplicateurY)
pente[i] = hauteur - (((y-y1)/precision - borneYGauche)* multiplicateurY)
pente2[i] = hauteur - ((((y-y1)/precision-p1)/precision - borneYGauche)* multiplicateurY)
p1 = (y-y1)/precision
y1 = y
}
}
pente[1]=pente[2]
pente2[2]=pente2[3]
pente2[1]=pente2[2]
//alert(pointX+'\n'+pointY)
if(document.getElementById("selectMethodeAffichage").value == "svg2"){
calculerGraphSVG2(i)
}
else{
calculerGraphSVG(i)
}
}
// Même fonction mais pour dessiner à l'aide de canvas
function evaluerCanvas(eq) {
borneXGauche = parseFloat(document.getElementById("borneXGauche").value)
borneXDroite = parseFloat(document.getElementById("borneXDroite").value)
borneYGauche = parseFloat(document.getElementById("borneYGauche").value)
borneYDroite = parseFloat(document.getElementById("borneYDroite").value)
multiplicateurX = largeur/Math.abs(borneXDroite - borneXGauche)
multiplicateurY = hauteur/Math.abs(borneYDroite - borneYGauche)
lineWidth = document.getElementById("inputTaille").value
var i = 0
var y1, p1
for(x=borneXGauche; x<=(borneXDroite+5*precision); x=x+precision){
y = eval(eq)
i++
if(!isNaN(y)){
pointX[i] = (x - borneXGauche) * multiplicateurX
pointY[i] = hauteur - ((y - borneYGauche) * multiplicateurY)
pente[i] = hauteur - (((y-y1)/precision - borneYGauche)* multiplicateurY)
pente2[i] = hauteur - ((((y-y1)/precision-p1)/precision - borneYGauche)* multiplicateurY)
p1 = (y-y1)/precision
y1 = y
}
else{
pointX[i] = "undefined"
pointY[i] = "undefined"
pente[i] = "undefined"
pente2[i] = "undefined"
}
}
pente[1]=pente[2]
pente2[2]=pente2[3]
pente2[1]=pente2[2]
calculerGraphCanevas(i)
}
// ---- SVG (une image) ----
// Génère le code HTML qui permet d'afficher le graphique et le place dans la div "affichage"
function calculerGraphSVG2(fin){
image = ""
for (i=1; i<fin; i++){
image = image + '<line x1="'+pointX[i]+'" y1="'+pointY[i]+'" x2="'+pointX[i+1]+'" y2="'+pointY[i+1]+'" style="stroke:'+couleurFonction+';stroke-width:2;"/>'
}
graphique = '<line x1="'+(-borneXGauche*multiplicateurX)+'" y1="'+0+'" x2="'+(-borneXGauche*multiplicateurX)+'" y2="'+hauteur+'" style="stroke:rgb(0,0,0);stroke-width:2;opacity:0.3;"/>'
graphique = graphique + '<line x1="'+0+'" y1="'+(hauteur-(-borneYGauche*multiplicateurY))+'" x2="'+largeur+'" y2="'+(hauteur-(-borneYGauche*multiplicateurY))+'" style="stroke:rgb(0,0,0);stroke-width:2;opacity:0.3;"/>'
image = '<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">'+image+graphique+'</svg> '
document.getElementById("affichage").innerHTML = image
//alert(image)
}
// ---- SVG (images multiples) ----
// Créé les différents éléments pour dessiner la fonction mathématique
// et les place dans la div "affichage"
function calculerGraphSVG(fin){
document.getElementById("affichage").innerHTML = ""
svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
svg.setAttribute("width", "100%")
svg.setAttribute("height", "100%")
for (i=1; i<fin; i++){
if ((pointY[i]<0) && (pointY[i+1]>hauteur)){
i++
}
if ((pointY[i]>hauteur) && (pointY[i+1]<0)){
i++
}
var ligne = document.createElementNS("http://www.w3.org/2000/svg", "line")
ligne.setAttribute("x1", pointX[i]+decalageX)
ligne.setAttribute("x2", pointX[i+1]+decalageX)
ligne.setAttribute("y1", pointY[i]+decalageY)
ligne.setAttribute("y2", pointY[i+1]+decalageY)
ligne.setAttribute("stroke", couleurFonction)
ligne.setAttribute("stroke-width", lineWidth)
svg.appendChild(ligne)
}
// dérivée
if(document.getElementById("checkDerivee").checked){
for (i=1; i<fin; i++){
var ligne = document.createElementNS("http://www.w3.org/2000/svg", "line")
ligne.setAttribute("x1", pointX[i]+decalageX)
ligne.setAttribute("x2", pointX[i+1]+decalageX)
ligne.setAttribute("y1", pente[i]+decalageY)
ligne.setAttribute("y2", pente[i+1]+decalageY)
ligne.setAttribute("stroke", "white")
ligne.setAttribute("stroke-width", lineWidth)
ligne.setAttribute("opacity", "0.8")
svg.appendChild(ligne)
}
}
if(document.getElementById("checkAxes").checked){
calculerAxes()
}
calculerGrilleEchelle()
document.getElementById("affichage").appendChild(svg)
}
// axes
function calculerAxes(){
var ligne = document.createElementNS("http://www.w3.org/2000/svg", "line")
ligne.setAttribute("x1", (-borneXGauche*multiplicateurX)+decalageX)
ligne.setAttribute("y1", 0+decalageY)
ligne.setAttribute("x2", (-borneXGauche*multiplicateurX)+decalageX)
ligne.setAttribute("y2", hauteur+decalageY)
ligne.setAttribute("stroke", "rgb(0,0,0)")
ligne.setAttribute("stroke-width", "2")
ligne.setAttribute("opacity", "0.3")
svg.appendChild(ligne)
var ligne = document.createElementNS("http://www.w3.org/2000/svg", "line")
ligne.setAttribute("x1", 0+decalageX)
ligne.setAttribute("y1", (hauteur-(-borneYGauche*multiplicateurY))+decalageY)
ligne.setAttribute("x2", largeur+decalageX)
ligne.setAttribute("y2", (hauteur-(-borneYGauche*multiplicateurY))+decalageY)
ligne.setAttribute("stroke", "rgb(0,0,0)")
ligne.setAttribute("stroke-width", "2")
ligne.setAttribute("opacity", "0.3")
svg.appendChild(ligne)
}
// grille et échelle
function calculerGrilleEchelle(){
var intervalX = Math.round(Math.abs(borneXGauche-borneXDroite)/10)
var intervalY = Math.round(Math.abs(borneYGauche-borneYDroite)/10)
var initialX = Math.round(-borneXGauche) % intervalX
var initialY = Math.round(-borneYGauche) % intervalY
for(var i=initialX;i<=Math.round(borneXDroite-borneXGauche);i=i+intervalX){
var position = Math.round((Math.round(borneXGauche) - borneXGauche +i) * multiplicateurX)
if(document.getElementById("checkGrille").checked){
var grille = document.createElementNS("http://www.w3.org/2000/svg", "line")
grille.setAttribute("x1", position)
grille.setAttribute("y1", 0)
grille.setAttribute("x2", position)
grille.setAttribute("y2", hauteur)
grille.setAttribute("stroke", "rgb(0,0,0)")
grille.setAttribute("stroke-width", "2")
grille.setAttribute("opacity", "0.05")
svg.appendChild(grille)
}
if(document.getElementById("checkEchelle").checked){
var txt = document.createElementNS("http://www.w3.org/2000/svg", "text")
txt.appendChild(document.createTextNode(i+borneXGauche))
txt.setAttribute("x", position-6)
txt.setAttribute("y", (hauteur-(-borneYGauche*multiplicateurY))+decalageY-2)
txt.setAttribute("fill-opacity", 0.6)
txt.setAttribute("fill", couleurEchelle)
svg.appendChild(txt)
}
}
for(var i=initialY;i<=Math.round(borneYDroite-borneYGauche);i=i+intervalY){
var position = hauteur - Math.round((Math.round(borneYGauche) - borneYGauche +i) * multiplicateurY)
if(document.getElementById("checkGrille").checked){
var grille = document.createElementNS("http://www.w3.org/2000/svg", "line")
grille.setAttribute("x1", 0)
grille.setAttribute("y1", position)
grille.setAttribute("x2", largeur)
grille.setAttribute("y2", position)
grille.setAttribute("stroke", "rgb(0,0,0)")
grille.setAttribute("stroke-width", "2")
grille.setAttribute("opacity", "0.05")
svg.appendChild(grille)
}
if(document.getElementById("checkEchelle").checked){
var txt = document.createElementNS("http://www.w3.org/2000/svg", "text")
txt.appendChild(document.createTextNode(i+borneYGauche))
txt.setAttribute("x", (-borneXGauche*multiplicateurX)+decalageX+2)
txt.setAttribute("y", position+6)
txt.setAttribute("fill-opacity", 0.6)
txt.setAttribute("fill", couleurEchelle)
svg.appendChild(txt)
}
}
}
// ---- Canevas ----
// Permet de générer le code HTML nécessaire à canvas et dessine la fonction
// à l'aide de lignes dans la zone de canvas.
function calculerGraphCanevas(fin){
document.getElementById("affichage").innerHTML = '<canvas id="canvas" width="'+largeur+'" height="'+hauteur+'"></canvas>'
ctx = document.getElementById('canvas').getContext('2d')
var undefined = true
// Autres fonctions
for(var i=0; i<listeFonctions.length; i++){
ctx.beginPath()
x = borneXGauche-3*precision
y = eval(listeFonctions[i])
if(isNaN(y)){
ctx.moveTo(x, 0)
}
else{
ctx.moveTo(x, y)
}
ctx.strokeStyle = listeCouleurs[i]
ctx.fillStyle = listeCouleurs[i]
ctx.lineWidth = lineWidth
for(x=borneXGauche-2*precision; x<=(borneXDroite+2*precision); x+=precision){
y = eval(listeFonctions[i])
if(!isNaN(y)){
ctx.lineTo((x - borneXGauche) * multiplicateurX+decalageX, hauteur - ((y - borneYGauche) * multiplicateurY)+decalageY)
/*pente[i] = hauteur - ((10*(y-y1) - borneYGauche) * multiplicateurY)*/
}
}
ctx.stroke()
}
// aire sous la fonctions
if(document.getElementById("checkAire").checked){
ctx.strokeStyle = "rgba(255,255,255,0)";
ctx.fillStyle = "rgba(0,180,255,0.3)";
var a = parseInt(document.getElementById("aireG").value);
var b = parseInt(document.getElementById("aireD").value);
var fct = document.getElementById("inputEq").value;
var f = function(x){
return eval(fct);
};
var convertX = function(nbr){
return (nbr - borneXGauche) * multiplicateurX;
};
var convertY = function(nbr){
return hauteur - ((nbr - borneYGauche) * multiplicateurY)
};
var n = 5000/(borneXDroite-borneXGauche);
var aire, largeurRect, gaucheRect, droiteRect, millieuRect, hauteurRect, aireRect;
aire = 0;
largeurRect = (b-a)/n;
for(var i=0; i<n; i++){
gaucheRect = a + i*largeurRect;
droiteRect = a + (i+1)*largeurRect;
millieuRect = (gaucheRect+droiteRect) / 2;
hauteurRect = f(millieuRect);
//aireRect = largeurRect * hauteurRect;
//aire = aire + aireRect;
try{
ctx.fillRect(convertX(gaucheRect), convertY(hauteurRect), 2, hauteurRect*multiplicateurY);
}
catch(err){
}
}
}
// fonction
ctx.beginPath()
//ctx.moveTo(pointX[1]+decalageX, pointY[1]+decalageY)
ctx.strokeStyle = couleurFonction
ctx.fillStyle = couleurFonction
ctx.lineWidth = lineWidth
if(document.getElementById("selectMethodeAffichage").value == "canvas"){
for (i=1; i<fin; i++){
if(isNaN(pointX[i]) || isNaN(pointY[i])){
undefined = true
}
else{
if(undefined){
ctx.moveTo(pointX[i]+decalageX, pointY[i]+decalageY)
}
undefined = false
ctx.lineTo(pointX[i]+decalageX, pointY[i]+decalageY)
}
}
ctx.stroke()
}
else{
for (i=1; i<fin; i++){
if(!isNaN(pointX[i]) && !isNaN(pointY[i])){
ctx.moveTo(pointX[i]+decalageX, pointY[i]+decalageY)
ctx.arc(pointX[i]+decalageX, pointY[i]+decalageY, lineWidth/2, 0, 2*Math.PI, false)
}
}
ctx.fill()
//ctx.stroke()
}
// dérivée première
if(document.getElementById("checkDerivee").checked){
undefined = true
ctx.beginPath()
ctx.strokeStyle = "rgba(255, 255, 255, 0.8)"
ctx.fillStyle = "rgba(255, 255, 255, 0.8)"
ctx.lineWidth = lineWidth
//ctx.moveTo(pointX[1]+decalageX, pente[1]+decalageY)
for (i=1; i<fin; i++){
if(isNaN(pointX[i]) || isNaN(pente[i])){
undefined = true
}
else{
if(undefined){
try{
ctx.moveTo(pointX[i]+decalageX, pente[i]+decalageY)
}
catch(err){
//alert(";"+pente[i]+" ; "+pointX[i])
}
}
undefined = false
try{
ctx.lineTo(pointX[i]+decalageX, pente[i]+decalageY)
}
catch(err){
//alert(pente[i]+" ; "+pointX[i])
}
}
}
ctx.stroke()
}
// dérivée seconde
if(document.getElementById("checkDerivee2").checked){
undefined = true
ctx.beginPath()
ctx.strokeStyle = "rgba(150, 150, 150, 0.8)"
ctx.fillStyle = "rgba(150, 150, 150, 0.8)"
ctx.lineWidth = lineWidth
//ctx.moveTo(pointX[1]+decalageX, pente2[1]+decalageY)
for (i=1; i<fin-1; i++){
if(isNaN(pointX[i]) || isNaN(pente2[i])){
undefined = true
}
else{
if(undefined){
try{
ctx.moveTo(pointX[i+2]+decalageX, pente2[i+2]+decalageY)
}
catch(err){
}
}
undefined = false
try{
ctx.lineTo(pointX[i+2]+decalageX, pente2[i+2]+decalageY)
}
catch(err){
}
}
}
ctx.stroke()
}
// grille et échelle
var intervalX = Math.round(Math.abs(borneXGauche-borneXDroite)/10)
var intervalY = Math.round(Math.abs(borneYGauche-borneYDroite)/10)
var initialX = Math.round(-borneXGauche) % intervalX
var initialY = Math.round(-borneYGauche) % intervalY
ctx.beginPath()
ctx.fillStyle = couleurEchelle
ctx.lineWidth = "2"
for(var i=initialX;i<=Math.round(borneXDroite-borneXGauche);i=i+intervalX){
var position = Math.round((Math.round(borneXGauche) - borneXGauche +i) * multiplicateurX)
if(document.getElementById("checkGrille").checked){
ctx.moveTo(position, 0)
ctx.lineTo(position, hauteur)
}
if(document.getElementById("checkEchelle").checked){
ctx.fillText(Math.ceil(i+borneXGauche), position-6, (hauteur-(-borneYGauche*multiplicateurY))+decalageY-2)
}
}
for(var i=initialY;i<=Math.round(borneYDroite-borneYGauche);i=i+intervalY){
var position = hauteur - Math.round((Math.round(borneYGauche) - borneYGauche +i) * multiplicateurY)
if(document.getElementById("checkGrille").checked){
ctx.moveTo(0, position)
ctx.lineTo(largeur, position)
}
if(document.getElementById("checkEchelle").checked){
ctx.fillText(Math.ceil(i+borneYGauche), (-borneXGauche*multiplicateurX)+decalageX+2, position+6)
}
}
ctx.strokeStyle = couleurGrille
ctx.stroke()
// axes
if(document.getElementById("checkAxes").checked){
ctx.beginPath()
ctx.strokeStyle = couleurAxes
ctx.lineWidth = "2"
ctx.moveTo((-borneXGauche*multiplicateurX)+decalageX, 0+decalageY)
ctx.lineTo((-borneXGauche*multiplicateurX)+decalageX, hauteur+decalageY)
ctx.moveTo(0+decalageX, (hauteur-(-borneYGauche*multiplicateurY))+decalageY)
ctx.lineTo(largeur+decalageX, (hauteur-(-borneYGauche*multiplicateurY))+decalageY)
ctx.stroke()
}
}