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() { var Point = Base.extend({
/**
* 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 {
beans: true, beans: true,
initialize: function() { initialize: function() {
@ -43,36 +29,36 @@ var Point = Base.extend(new function() {
}, },
clone: function() { clone: function() {
return createPoint(this.x, this.y); return Point.create(this.x, this.y);
}, },
add: function() { add: function() {
var point = Point.read(arguments); 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() { subtract: function() {
var point = Point.read(arguments); 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() { multiply: function() {
var point = Point.read(arguments); 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() { divide: function() {
var point = Point.read(arguments); 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() { modulo: function() {
var point = Point.read(arguments); 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() { negate: function() {
return createPoint(-this.x, -this.y); return Point.create(-this.x, -this.y);
}, },
equals: function() { equals: function() {
@ -127,17 +113,21 @@ var Point = Base.extend(new function() {
length = 1; length = 1;
var len = this.length; var len = this.length;
var scale = len != 0 ? length / len : 0; 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. // Preserve angle.
res._angle = this._angle; res._angle = this._angle;
return res; return res;
}, },
getAngleInRadians: function() { 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); return Math.atan2(this.y, this.x);
}, },
getAngleInDegrees: function() { 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; 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; angle = angle * Math.PI / 180;
var s = Math.sin(angle); var s = Math.sin(angle);
var c = Math.cos(angle); var c = Math.cos(angle);
return createPoint( return Point.create(
this.x * c - this.y * s, this.x * c - this.y * s,
this.y * c + this.x * s this.y * c + this.x * s
); );
@ -215,7 +205,7 @@ var Point = Base.extend(new function() {
}, },
interpolate: function(point, t) { interpolate: function(point, t) {
return createPoint( return Point.create(
this.x * (1 - t) + point.x * t, this.x * (1 - t) + point.x * t,
this.y * (1 - t) + point.y * t this.y * (1 - t) + point.y * t
); );
@ -244,19 +234,19 @@ var Point = Base.extend(new function() {
}, },
round: 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() { 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() { 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() { 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() { dot: function() {
@ -272,10 +262,10 @@ var Point = Base.extend(new function() {
project: function() { project: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
if (point.isZero()) { if (point.isZero()) {
return createPoint(0, 0); return Point.create(0, 0);
} else { } else {
var scale = this.dot(point) / point.dot(point); var scale = this.dot(point) / point.dot(point);
return createPoint( return Point.create(
point.x * scale, point.x * scale,
point.y * scale point.y * scale
); );
@ -287,6 +277,18 @@ var Point = Base.extend(new function() {
}, },
statics: { 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) { read: function(args, index) {
var index = index || 0, length = args.length - index; var index = index || 0, length = args.length - index;
if (length == 1 && args[index] instanceof Point) { if (length == 1 && args[index] instanceof Point) {
@ -301,20 +303,19 @@ var Point = Base.extend(new function() {
}, },
min: function(point1, point2) { min: function(point1, point2) {
return createPoint( return Point.create(
Math.min(point1.x, point2.x), Math.min(point1.x, point2.x),
Math.min(point1.y, point2.y)); Math.min(point1.y, point2.y));
}, },
max: function(point1, point2) { max: function(point1, point2) {
return createPoint( return Point.create(
Math.max(point1.x, point2.x), Math.max(point1.x, point2.x),
Math.max(point1.y, point2.y)); Math.max(point1.y, point2.y));
}, },
random: function() { random: function() {
return createPoint(Math.random(), Math.random()); return Point.create(Math.random(), Math.random());
} }
} }
};
}); });