Simplify Point#setLength().

This commit is contained in:
Jürg Lehni 2011-05-01 23:28:57 +01:00
parent b0282b9bd4
commit 4b832e0ea9

View file

@ -149,33 +149,18 @@ var Point = this.Point = Base.extend({
},
setLength: function(length) {
// Whenever setting x/y, use #set() instead of direct assignment,
// so LinkedPoint does not report changes twice.
if (this.isZero()) {
if (this._angle != null) {
var a = this._angle;
// Use #set() instead of direct assignment, so LinkedPoint
// can optimise
this.set(
Math.cos(a) * length,
Math.sin(a) * length
);
} else {
// Assume angle = 0
this.x = length;
// y is already 0
}
var angle = this._angle || 0;
this.set(Math.cos(angle) * length, Math.sin(angle) * length);
} else {
var scale = length / this.getLength();
if (scale == 0) {
// Calculate angle now, so it will be preserved even when
// x and y are 0
// Force calculation of angle now, so it will be preserved even when
// x and y are 0
if (scale == 0)
this.getAngle();
}
// Use #set() instead of direct assignment, so LinkedPoint
// can optimise
this.set(
this.x * scale,
this.y * scale
);
this.set(this.x * scale, this.y * scale);
}
return this;
},