
المشاركة الأصلية كتبت بواسطة المبدع
كود:
function Vector2D(x, y) {
كود:
switch (arguments.length) {
case 1 :
if (arguments[0]._x != undefined) {
this.init(arguments[0]._x, arguments[0]._y);
} else {
this.init(arguments[0].x, arguments[0].y);
}
break;
case 2 :
this.init(x, y);
break;
}
}
var o=Vector2D.prototype;
//
o.init = function(x, y) {
this.x = x;
this.y = y;
};
o.dot = function(that) {
return this.x*that.x+this.y*that.y;
};
o.fromPoints = function(p1, p2) {
return new Vector2D(p2.x-p1.x, p2.y-p1.y);
};
o.pAdd = function(that) {
return new Vector2D(this.x+that.x, this.y+that.y);
};
o.addEquals = function(that) {
this.x += that.x;
this.y += that.y;
return this;
};
o.subtract = function(that) {
return new Vector2D(this.x-that.x, this.y-that.y);
};
o.subtractEquals = function(that) {
this.x -= that.x;
this.y -= that.y;
return this;
};
o.getLength = function() {
return Math.sqrt(this.x*this.x+this.y*this.y);
};
o.cross = function(that) {
return this.x*that.y-this.y*that.x;
};
o.unit = function() {
return this.divide(this.getLength());
};
o.unitEquals = function() {
this.divideEquals(this.getLength());
return this;
};
o.divide = function(scalar) {
return new Vector2D(this.x/scalar, this.y/scalar);
};
o.divideEquals = function(scalar) {
this.x /= scalar;
this.y /= scalar;
return this;
};
o.multiply = function(scalar) {
return new Vector2D(this.x*scalar, this.y*scalar);
};
o.multiplyEquals = function(scalar) {
this.x *= scalar;
this.y *= scalar;
return this;
};
o.getNormal = function() {
var v = new Vector2D(this.y, -this.x);
return v.unit();
};
o.setLengthEquals= function(l) {
return this.unitEquals().multiplyEquals(l);
};
o.setLength = function(l) {
return this.unit().multiplyEquals(l);
};
delete o
هذه عينه من اللغه لا الحصر بس لكي تكون لديكم فكره
المفضلات