added getMagnitude, setMagnitude, normalize and isZero methods to Point

This commit is contained in:
Stuart Lee 2013-10-31 14:11:03 +00:00
parent 01f64c6694
commit 903c2e73fa

View file

@ -215,9 +215,7 @@ Phaser.Point.prototype = {
* @return {number} The distance between this Point object and the destination Point object.
*/
distance: function (dest, round) {
return Phaser.Point.distance(this, dest, round);
},
/**
@ -244,6 +242,51 @@ Phaser.Point.prototype = {
return Phaser.Point.rotate(this, x, y, angle, asDegrees, distance);
},
/**
* Calculates the length of the vector
* @method Phaser.Point#getMagnitude
* @return {number} the length of the vector
*/
getMagnitude: function() {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
},
/**
* Alters the length of the vector without changing the direction
* @method Phaser.Point#getMagnitude
* @param {number} magnitude the desired magnitude of the resulting vector
* @return {Phaser.Point} the modified original vector
*/
setMagnitude: function(magnitude) {
return this.normalize().multiply(magnitude, magnitude);
},
/**
* Alters the vector so that its length is 1, but it retains the same direction
* @method Phaser.Point#normalize
* @return {Phaser.Point} the modified original vector
*/
normalize: function() {
if(!this.isZero()) {
var m = this.getMagnitude();
this.x /= m;
this.y /= m;
}
return this;
},
/**
* Determine if this point is at 0,0
* @method Phaser.Point#isZero
* @return {boolean} True if this Point is 0,0, otherwise false
*/
isZero: function() {
return (this.x === 0 && this.y === 0);
},
/**
* Returns a string representation of this object.
* @method Phaser.Point#toString