Merge remote branch 'origin/master'

This commit is contained in:
Jonathan Puckey 2011-02-20 23:51:56 +01:00
commit 245c1e9b90

View file

@ -1,4 +1,18 @@
var Point = Base.extend({ 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 {
beans: true, beans: true,
initialize: function() { initialize: function() {
@ -29,36 +43,36 @@ var Point = Base.extend({
}, },
clone: function() { clone: function() {
return new Point(this.x, this.y); return createPoint(this.x, this.y);
}, },
add: function() { add: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
return new Point(this.x + point.x, this.y + point.y); return createPoint(this.x + point.x, this.y + point.y);
}, },
subtract: function() { subtract: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
return new Point(this.x - point.x, this.y - point.y); return createPoint(this.x - point.x, this.y - point.y);
}, },
multiply: function() { multiply: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
return new Point(this.x * point.x, this.y * point.y); return createPoint(this.x * point.x, this.y * point.y);
}, },
divide: function() { divide: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
return new Point(this.x / point.x, this.y / point.y); return createPoint(this.x / point.x, this.y / point.y);
}, },
modulo: function() { modulo: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
return new Point(this.x % point.x, this.y % point.y); return createPoint(this.x % point.x, this.y % point.y);
}, },
negate: function() { negate: function() {
return new Point(-this.x, -this.y); return createPoint(-this.x, -this.y);
}, },
equals: function() { equals: function() {
@ -113,7 +127,7 @@ var Point = Base.extend({
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 = new Point(this.x * scale, this.y * scale); var res = createPoint(this.x * scale, this.y * scale);
// Preserve angle. // Preserve angle.
res._angle = this._angle; res._angle = this._angle;
return res; return res;
@ -188,25 +202,26 @@ var Point = Base.extend({
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 new Point( return createPoint(
this.x * c - this.y * s, this.x * c - this.y * s,
this.y * c + this.x * s this.y * c + this.x * s
); );
}, },
// TODO: Shouldn't center just be the optional 2nd argument to rotate()?
rotateAround: function(angle, center) { rotateAround: function(angle, center) {
center = new Point(center); center = new Point(center);
return this.subtract(center).rotate(angle).add(this); return this.subtract(center).rotate(angle).add(this);
}, },
interpolate: function(point, t) { interpolate: function(point, t) {
return new Point( return createPoint(
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
); );
}, },
// Need to adapt Rectangle.java first // TODO: Need to adapt Rectangle.java first
// isInside: function(rect) { // isInside: function(rect) {
// return rect.contains(this); // return rect.contains(this);
// }, // },
@ -229,19 +244,19 @@ var Point = Base.extend({
}, },
round: function() { round: function() {
return new Point(Math.round(this.x), Math.round(this.y)); return createPoint(Math.round(this.x), Math.round(this.y));
}, },
ceil: function() { ceil: function() {
return new Point(Math.ceil(this.x), Math.ceil(this.y)); return createPoint(Math.ceil(this.x), Math.ceil(this.y));
}, },
floor: function() { floor: function() {
return new Point(Math.floor(this.x), Math.floor(this.y)); return createPoint(Math.floor(this.x), Math.floor(this.y));
}, },
abs: function() { abs: function() {
return new Point(Math.abs(this.x), Math.abs(this.y)); return createPoint(Math.abs(this.x), Math.abs(this.y));
}, },
dot: function() { dot: function() {
@ -257,10 +272,10 @@ var Point = Base.extend({
project: function() { project: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
if (point.isZero()) { if (point.isZero()) {
return new Point(0, 0); return createPoint(0, 0);
} else { } else {
var scale = this.dot(point) / point.dot(point); var scale = this.dot(point) / point.dot(point);
return new Point( return createPoint(
point.x * scale, point.x * scale,
point.y * scale point.y * scale
); );
@ -286,19 +301,20 @@ var Point = Base.extend({
}, },
min: function(point1, point2) { min: function(point1, point2) {
return new Point( return createPoint(
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 new Point( return createPoint(
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 new Point(Math.random(), Math.random()); return createPoint(Math.random(), Math.random());
} }
} }
};
}); });