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
|
<!DOCTYPE html>
<html>
<head>
<title>Tableaux</title>
<script>
// Ajout de méthodes à l'objet Array
Array.prototype.supprime=function(ind) {
for(var i=0;i<this.length-1;i++){
if(i>=ind){
this[i]=this[i+1]
}
}
this.length--
return this
}
Array.prototype.echange=function(i,j){
var t=this[i]
this[i]=this[j]
this[j]=t
return this
}
Array.prototype.alea=function(taille,min,max){
for (var i=0; i<taille; i++) {
this[i]=min+Math.floor(Math.random()*(max-min+1))
}
return this
}
Array.prototype.max=function(){
var max=this[0]
for (var i=0; i<this.length; i++) {
if(this[i]>max){max=this[i]}
}
return max
}
Array.prototype.triSelection=function(){
var aux=this
for (var i=0; i<aux.length-1; i++) {
for (var j=i+1; j<aux.length; j++) {
var min=i
if (aux[j]<aux[min]) {
min=j
}
aux=aux.echange(i,min)
}
}
return aux
}
Array.prototype.visuel=function(ctx,w,h){
ctx.clearRect(0,0,w,h);
var qx=w/this.length
var qy=h/this.max()
for (var i=0; i<this.length; i++) {
ctx.beginPath()
ctx.arc(qx/2+i*qx,qy/2+ this[i]*qy,1,0,2*Math.PI,true)
ctx.stroke()
}
}
Array.prototype.triVisuel2=function(ctx,w,h){
var aux=this;
aux.visuel(ctx,w,h);
var c=1;
for (var i=0; i<aux.length-1; i++) {
for (var j=i+1; j<aux.length; j++) {
if (aux[j]<aux[i]) {
aux.echange(i,j);
}
//alert(aux)
(function(aux,delai){
setTimeout(function(){
aux.visuel(ctx,w,h);
}, delai);
})(aux,1000*c);
c++;
}
}
}
// Tests des nouvelles méthodes
function $(id){return document.getElementById(id)}
function charger(){
var x=[20,40,12,1]
var y=[]
var ctx=$('canvas').getContext('2d')
//y.alea(50,0,50)
x.triVisuel2(ctx,$("canvas").width,$("canvas").height)
}
</script>
</head>
<body onload="charger()">
<canvas id="canvas" width="500" height="500" style="border: medium solid rgb(255,0,0)"></canvas>
</body>
</html> |
Partager