Move private createPoint() to Point.create().

This commit is contained in:
Jürg Lehni 2011-02-21 00:23:36 +01:00
parent 245c1e9b90
commit 04d2ed29c6

View file

@ -1,18 +1,4 @@
var Point = Base.extend(new function() {
/**
* Provide a faster internal creator for Points out of two coordinates that
* does not rely on Point#initialize at all. This speeds up all math
* operations a lot.
*/
function createPoint(x, y) {
var point = new Point(Point.dont);
point.x = x;
point.y = y;
return point;
}
return {
var Point = Base.extend({
beans: true,
initialize: function() {
@ -43,36 +29,36 @@ var Point = Base.extend(new function() {
},
clone: function() {
return createPoint(this.x, this.y);
return Point.create(this.x, this.y);
},
add: function() {
var point = Point.read(arguments);
return createPoint(this.x + point.x, this.y + point.y);
return Point.create(this.x + point.x, this.y + point.y);
},
subtract: function() {
var point = Point.read(arguments);
return createPoint(this.x - point.x, this.y - point.y);
return Point.create(this.x - point.x, this.y - point.y);
},
multiply: function() {
var point = Point.read(arguments);
return createPoint(this.x * point.x, this.y * point.y);
return Point.create(this.x * point.x, this.y * point.y);
},
divide: function() {
var point = Point.read(arguments);
return createPoint(this.x / point.x, this.y / point.y);
return Point.create(this.x / point.x, this.y / point.y);
},
modulo: function() {
var point = Point.read(arguments);
return createPoint(this.x % point.x, this.y % point.y);
return Point.create(this.x % point.x, this.y % point.y);
},
negate: function() {
return createPoint(-this.x, -this.y);
return Point.create(-this.x, -this.y);
},
equals: function() {
@ -127,17 +113,21 @@ var Point = Base.extend(new function() {
length = 1;
var len = this.length;
var scale = len != 0 ? length / len : 0;
var res = createPoint(this.x * scale, this.y * scale);
var res = Point.create(this.x * scale, this.y * scale);
// Preserve angle.
res._angle = this._angle;
return res;
},
getAngleInRadians: function() {
// TODO: Not sur we want this one, but if so, just rely on
// this.getAngle(), which caches values internally?
return Math.atan2(this.y, this.x);
},
getAngleInDegrees: function() {
// TODO: Not sur we want this one, but if so, just rely on
// this.getAngle(), which caches values internally?
return Math.atan2(this.y, this.x) * 180 / Math.PI;
},
@ -202,7 +192,7 @@ var Point = Base.extend(new function() {
angle = angle * Math.PI / 180;
var s = Math.sin(angle);
var c = Math.cos(angle);
return createPoint(
return Point.create(
this.x * c - this.y * s,
this.y * c + this.x * s
);
@ -215,7 +205,7 @@ var Point = Base.extend(new function() {
},
interpolate: function(point, t) {
return createPoint(
return Point.create(
this.x * (1 - t) + point.x * t,
this.y * (1 - t) + point.y * t
);
@ -244,19 +234,19 @@ var Point = Base.extend(new function() {
},
round: function() {
return createPoint(Math.round(this.x), Math.round(this.y));
return Point.create(Math.round(this.x), Math.round(this.y));
},
ceil: function() {
return createPoint(Math.ceil(this.x), Math.ceil(this.y));
return Point.create(Math.ceil(this.x), Math.ceil(this.y));
},
floor: function() {
return createPoint(Math.floor(this.x), Math.floor(this.y));
return Point.create(Math.floor(this.x), Math.floor(this.y));
},
abs: function() {
return createPoint(Math.abs(this.x), Math.abs(this.y));
return Point.create(Math.abs(this.x), Math.abs(this.y));
},
dot: function() {
@ -272,10 +262,10 @@ var Point = Base.extend(new function() {
project: function() {
var point = Point.read(arguments);
if (point.isZero()) {
return createPoint(0, 0);
return Point.create(0, 0);
} else {
var scale = this.dot(point) / point.dot(point);
return createPoint(
return Point.create(
point.x * scale,
point.y * scale
);
@ -287,6 +277,18 @@ var Point = Base.extend(new function() {
},
statics: {
/**
* Provide a faster creator for Points out of two coordinates that
* does not rely on Point#initialize at all. This speeds up all math
* operations a lot.
*/
create: function(x, y) {
var point = new Point(Point.dont);
point.x = x;
point.y = y;
return point;
},
read: function(args, index) {
var index = index || 0, length = args.length - index;
if (length == 1 && args[index] instanceof Point) {
@ -301,20 +303,19 @@ var Point = Base.extend(new function() {
},
min: function(point1, point2) {
return createPoint(
return Point.create(
Math.min(point1.x, point2.x),
Math.min(point1.y, point2.y));
},
max: function(point1, point2) {
return createPoint(
return Point.create(
Math.max(point1.x, point2.x),
Math.max(point1.y, point2.y));
},
random: function() {
return createPoint(Math.random(), Math.random());
return Point.create(Math.random(), Math.random());
}
}
};
});