Skip to content

Commit bd75278

Browse files
committed
Merge pull request phaserjs#150 from beeglebug/master
added getMagnitude, setMagnitude, normalize and isZero methods to Point
2 parents 787abc1 + 903c2e7 commit bd75278

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

src/geom/Point.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ Phaser.Point.prototype = {
215215
* @return {number} The distance between this Point object and the destination Point object.
216216
*/
217217
distance: function (dest, round) {
218-
219218
return Phaser.Point.distance(this, dest, round);
220-
221219
},
222220

223221
/**
@@ -244,6 +242,51 @@ Phaser.Point.prototype = {
244242
return Phaser.Point.rotate(this, x, y, angle, asDegrees, distance);
245243
},
246244

245+
/**
246+
* Calculates the length of the vector
247+
* @method Phaser.Point#getMagnitude
248+
* @return {number} the length of the vector
249+
*/
250+
getMagnitude: function() {
251+
return Math.sqrt((this.x * this.x) + (this.y * this.y));
252+
},
253+
254+
/**
255+
* Alters the length of the vector without changing the direction
256+
* @method Phaser.Point#getMagnitude
257+
* @param {number} magnitude the desired magnitude of the resulting vector
258+
* @return {Phaser.Point} the modified original vector
259+
*/
260+
setMagnitude: function(magnitude) {
261+
return this.normalize().multiply(magnitude, magnitude);
262+
},
263+
264+
/**
265+
* Alters the vector so that its length is 1, but it retains the same direction
266+
* @method Phaser.Point#normalize
267+
* @return {Phaser.Point} the modified original vector
268+
*/
269+
normalize: function() {
270+
271+
if(!this.isZero()) {
272+
var m = this.getMagnitude();
273+
this.x /= m;
274+
this.y /= m;
275+
}
276+
277+
return this;
278+
279+
},
280+
281+
/**
282+
* Determine if this point is at 0,0
283+
* @method Phaser.Point#isZero
284+
* @return {boolean} True if this Point is 0,0, otherwise false
285+
*/
286+
isZero: function() {
287+
return (this.x === 0 && this.y === 0);
288+
},
289+
247290
/**
248291
* Returns a string representation of this object.
249292
* @method Phaser.Point#toString

0 commit comments

Comments
 (0)