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
| /****************************************************************
* @prototype : Vector2D
*****************************************************************/
var Vector2D = {
/**** Methodes d'instance ****/
scale : function(s) {
this.x *= (s instanceof Object && s.x!==undefined)? s.x : s;
this.y *= (s instanceof Object && s.y!==undefined)? s.y : s;
return this;
},
square : function( ) { return this.x*this.x + this.y*this.y; },
length : function() { return Math.sqrt(this.square()); },
setLength : function(l)
{
this.scale(l/this.length());
return this;
},
normalize : function()
{
var l = this.Length();
if (l != 0.0)
this.scale(1.0 / l)
return this;
},
translate : function(t)
{
this.x += t.x;
this.y += t.y;
return this;
},
/**** Methodes statiques ****/
Add : function ()
{
var v = { x : arguments[0].x, y : arguments[0].y };
v.__proto__ = Vector2D;
for(var i=1;i<arguments.length;i++)
v.translate(arguments[i]);
return v;
},
Sub : function ()
{
var v = { x : arguments[0].x, y : arguments[0].y };
v.__proto__ = Vector2D;
for(var i=1;i<arguments.length;i++)
{
arguments[i].__proto__ = Vector2D;
v.translate(arguments[i].scale(-1));
}
return v;
}
}
var VertexBuffer = [
{ x : 12.65, y : -6.245 },
{ x : -3.2565, y : 65.78 },
{ x : 89.2565, y : 53.41}
];
VertexBuffer.forEach(function(vertice)
{
vertice.__proto__ = Vector2D;
})
console.log(VertexBuffer[0].length());
console.log(Vector2D.Add.apply(null, VertexBuffer));
console.log(Vector2D.Sub.apply(null, VertexBuffer)); |
Partager