From 903c2e73facdd59542d5a429e52ab8d254b766ff Mon Sep 17 00:00:00 2001 From: Stuart Lee Date: Thu, 31 Oct 2013 14:11:03 +0000 Subject: [PATCH] added getMagnitude, setMagnitude, normalize and isZero methods to Point --- src/geom/Point.js | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/geom/Point.js b/src/geom/Point.js index ef8799f64..bcd584feb 100644 --- a/src/geom/Point.js +++ b/src/geom/Point.js @@ -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