Skip to content

Commit c34c7ba

Browse files
committed
Docs updates.
1 parent dfd9203 commit c34c7ba

1 file changed

Lines changed: 87 additions & 41 deletions

File tree

src/math/Math.js

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ Phaser.Math = {
147147
* @return {number} n mod 1
148148
*/
149149
shear: function (n) {
150+
150151
return n % 1;
152+
151153
},
152154

153155
/**
@@ -158,8 +160,8 @@ Phaser.Math = {
158160
* @method Phaser.Math#snapTo
159161
* @param {number} input - The value to snap.
160162
* @param {number} gap - The interval gap of the grid.
161-
* @param {number} [start] - Optional starting offset for gap.
162-
* @return {number}
163+
* @param {number} [start=0] - Optional starting offset for gap.
164+
* @return {number} The snapped value.
163165
*/
164166
snapTo: function (input, gap, start) {
165167

@@ -185,8 +187,8 @@ Phaser.Math = {
185187
* @method Phaser.Math#snapToFloor
186188
* @param {number} input - The value to snap.
187189
* @param {number} gap - The interval gap of the grid.
188-
* @param {number} [start] - Optional starting offset for gap.
189-
* @return {number}
190+
* @param {number} [start=0] - Optional starting offset for gap.
191+
* @return {number} The snapped value.
190192
*/
191193
snapToFloor: function (input, gap, start) {
192194

@@ -212,8 +214,8 @@ Phaser.Math = {
212214
* @method Phaser.Math#snapToCeil
213215
* @param {number} input - The value to snap.
214216
* @param {number} gap - The interval gap of the grid.
215-
* @param {number} [start] - Optional starting offset for gap.
216-
* @return {number}
217+
* @param {number} [start=0] - Optional starting offset for gap.
218+
* @return {number} The snapped value.
217219
*/
218220
snapToCeil: function (input, gap, start) {
219221

@@ -261,9 +263,9 @@ Phaser.Math = {
261263
*
262264
* @method Phaser.Math#roundTo
263265
* @param {number} value - The value to round.
264-
* @param {number} place - The place to round to.
265-
* @param {number} base - The base to round in... default is 10 for decimal.
266-
* @return {number}
266+
* @param {number} [place=0] - The place to round to.
267+
* @param {number} [base=10] - The base to round in. Default is 10 for decimal.
268+
* @return {number} The rounded value.
267269
*/
268270
roundTo: function (value, place, base) {
269271

@@ -277,11 +279,14 @@ Phaser.Math = {
277279
},
278280

279281
/**
282+
* Floors to some place comparative to a `base`, default is 10 for decimal place.
283+
* The `place` is represented by the power applied to `base` to get that place.
284+
*
280285
* @method Phaser.Math#floorTo
281286
* @param {number} value - The value to round.
282-
* @param {number} place - The place to round to.
283-
* @param {number} base - The base to round in... default is 10 for decimal.
284-
* @return {number}
287+
* @param {number} [place=0] - The place to round to.
288+
* @param {number} [base=10] - The base to round in. Default is 10 for decimal.
289+
* @return {number} The rounded value.
285290
*/
286291
floorTo: function (value, place, base) {
287292

@@ -295,11 +300,14 @@ Phaser.Math = {
295300
},
296301

297302
/**
303+
* Ceils to some place comparative to a `base`, default is 10 for decimal place.
304+
* The `place` is represented by the power applied to `base` to get that place.
305+
*
298306
* @method Phaser.Math#ceilTo
299307
* @param {number} value - The value to round.
300-
* @param {number} place - The place to round to.
301-
* @param {number} base - The base to round in... default is 10 for decimal.
302-
* @return {number}
308+
* @param {number} [place=0] - The place to round to.
309+
* @param {number} [base=10] - The base to round in. Default is 10 for decimal.
310+
* @return {number} The rounded value.
303311
*/
304312
ceilTo: function (value, place, base) {
305313

@@ -314,42 +322,51 @@ Phaser.Math = {
314322

315323
/**
316324
* Find the angle of a segment from (x1, y1) -> (x2, y2).
325+
*
317326
* @method Phaser.Math#angleBetween
318-
* @param {number} x1
319-
* @param {number} y1
320-
* @param {number} x2
321-
* @param {number} y2
327+
* @param {number} x1 - The x coordinate of the first value.
328+
* @param {number} y1 - The y coordinate of the first value.
329+
* @param {number} x2 - The x coordinate of the second value.
330+
* @param {number} y2 - The y coordinate of the second value.
322331
* @return {number} The angle, in radians.
323332
*/
324333
angleBetween: function (x1, y1, x2, y2) {
334+
325335
return Math.atan2(y2 - y1, x2 - x1);
336+
326337
},
327338

328339
/**
329340
* Find the angle of a segment from (x1, y1) -> (x2, y2).
330-
* Note that the difference between this method and Math.angleBetween is that this assumes the y coordinate travels
341+
*
342+
* The difference between this method and Math.angleBetween is that this assumes the y coordinate travels
331343
* down the screen.
332344
*
333345
* @method Phaser.Math#angleBetweenY
334-
* @param {number} x1
335-
* @param {number} y1
336-
* @param {number} x2
337-
* @param {number} y2
346+
* @param {number} x1 - The x coordinate of the first value.
347+
* @param {number} y1 - The y coordinate of the first value.
348+
* @param {number} x2 - The x coordinate of the second value.
349+
* @param {number} y2 - The y coordinate of the second value.
338350
* @return {number} The angle, in radians.
339351
*/
340352
angleBetweenY: function (x1, y1, x2, y2) {
353+
341354
return Math.atan2(x2 - x1, y2 - y1);
355+
342356
},
343357

344358
/**
345359
* Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
360+
*
346361
* @method Phaser.Math#angleBetweenPoints
347-
* @param {Phaser.Point} point1
348-
* @param {Phaser.Point} point2
349-
* @return {number} The angle, in radians.
362+
* @param {Phaser.Point} point1 - The first point.
363+
* @param {Phaser.Point} point2 - The second point.
364+
* @return {number} The angle between the two points, in radians.
350365
*/
351366
angleBetweenPoints: function (point1, point2) {
367+
352368
return Math.atan2(point2.y - point1.y, point2.x - point1.x);
369+
353370
},
354371

355372
/**
@@ -360,24 +377,28 @@ Phaser.Math = {
360377
* @return {number} The angle, in radians.
361378
*/
362379
angleBetweenPointsY: function (point1, point2) {
380+
363381
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
382+
364383
},
365384

366385
/**
367386
* Reverses an angle.
368387
* @method Phaser.Math#reverseAngle
369388
* @param {number} angleRad - The angle to reverse, in radians.
370-
* @return {number} Returns the reverse angle, in radians.
389+
* @return {number} The reverse angle, in radians.
371390
*/
372391
reverseAngle: function (angleRad) {
392+
373393
return this.normalizeAngle(angleRad + Math.PI, true);
394+
374395
},
375396

376397
/**
377398
* Normalizes an angle to the [0,2pi) range.
378399
* @method Phaser.Math#normalizeAngle
379400
* @param {number} angleRad - The angle to normalize, in radians.
380-
* @return {number} Returns the angle, fit within the [0,2pi] range, in radians.
401+
* @return {number} The angle, fit within the [0,2pi] range, in radians.
381402
*/
382403
normalizeAngle: function (angleRad) {
383404

@@ -393,10 +414,12 @@ Phaser.Math = {
393414
* @param {number} value - The value to add the amount to.
394415
* @param {number} amount - The amount to add to the value.
395416
* @param {number} max - The maximum the value is allowed to be.
396-
* @return {number}
417+
* @return {number} The new value.
397418
*/
398419
maxAdd: function (value, amount, max) {
420+
399421
return Math.min(value + amount, max);
422+
400423
},
401424

402425
/**
@@ -409,7 +432,9 @@ Phaser.Math = {
409432
* @return {number} The new value.
410433
*/
411434
minSub: function (value, amount, min) {
435+
412436
return Math.max(value - amount, min);
437+
413438
},
414439

415440
/**
@@ -474,8 +499,10 @@ Phaser.Math = {
474499
* @return {boolean} True if the given number is odd. False if the given number is even.
475500
*/
476501
isOdd: function (n) {
502+
477503
// Does not work with extremely large values
478504
return !!(n & 1);
505+
479506
},
480507

481508
/**
@@ -486,8 +513,10 @@ Phaser.Math = {
486513
* @return {boolean} True if the given number is even. False if the given number is odd.
487514
*/
488515
isEven: function (n) {
516+
489517
// Does not work with extremely large values
490518
return !(n & 1);
519+
491520
},
492521

493522
/**
@@ -724,11 +753,13 @@ Phaser.Math = {
724753
* @method Phaser.Math#linear
725754
* @param {number} p0
726755
* @param {number} p1
727-
* @param {number} t
756+
* @param {number} t - A value between 0 and 1.
728757
* @return {number}
729758
*/
730759
linear: function (p0, p1, t) {
760+
731761
return (p1 - p0) * t + p0;
762+
732763
},
733764

734765
/**
@@ -739,15 +770,17 @@ Phaser.Math = {
739770
* @return {number}
740771
*/
741772
bernstein: function (n, i) {
773+
742774
return this.factorial(n) / this.factorial(i) / this.factorial(n - i);
775+
743776
},
744777

745778
/**
746779
* @method Phaser.Math#factorial
747780
* @param {number} value - the number you want to evaluate
748781
* @return {number}
749782
*/
750-
factorial : function( value ){
783+
factorial: function (value) {
751784

752785
if (value === 0)
753786
{
@@ -786,15 +819,17 @@ Phaser.Math = {
786819
},
787820

788821
/**
789-
* The (absolute) difference between two values.
822+
* The absolute difference between two values.
790823
*
791824
* @method Phaser.Math#difference
792-
* @param {number} a
793-
* @param {number} b
794-
* @return {number}
825+
* @param {number} a - The first value to check.
826+
* @param {number} b - The second value to check.
827+
* @return {number} The absolute difference between the two values.
795828
*/
796829
difference: function (a, b) {
830+
797831
return Math.abs(a - b);
832+
798833
},
799834

800835
/**
@@ -945,7 +980,9 @@ Phaser.Math = {
945980
* @return {number}
946981
*/
947982
clampBottom: function (x, a) {
983+
948984
return x < a ? a : x;
985+
949986
},
950987

951988
/**
@@ -959,22 +996,26 @@ Phaser.Math = {
959996
* @see {@link Phaser.Math.fuzzyEqual}
960997
*/
961998
within: function (a, b, tolerance) {
999+
9621000
return (Math.abs(a - b) <= tolerance);
1001+
9631002
},
9641003

9651004
/**
9661005
* Linear mapping from range <a1, a2> to range <b1, b2>
9671006
*
9681007
* @method Phaser.Math#mapLinear
969-
* @param {number} x the value to map
970-
* @param {number} a1 first endpoint of the range <a1, a2>
971-
* @param {number} a2 final endpoint of the range <a1, a2>
972-
* @param {number} b1 first endpoint of the range <b1, b2>
973-
* @param {number} b2 final endpoint of the range <b1, b2>
1008+
* @param {number} x - The value to map
1009+
* @param {number} a1 - First endpoint of the range <a1, a2>
1010+
* @param {number} a2 - Final endpoint of the range <a1, a2>
1011+
* @param {number} b1 - First endpoint of the range <b1, b2>
1012+
* @param {number} b2 - Final endpoint of the range <b1, b2>
9741013
* @return {number}
9751014
*/
9761015
mapLinear: function (x, a1, a2, b1, b2) {
1016+
9771017
return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1018+
9781019
},
9791020

9801021
/**
@@ -1006,8 +1047,11 @@ Phaser.Math = {
10061047
* @return {float} A value between 0 and 1.
10071048
*/
10081049
smootherstep: function (x, min, max) {
1050+
10091051
x = Math.max(0, Math.min(1, (x - min) / (max - min)));
1052+
10101053
return x * x * x * (x * (x * 6 - 15) + 10);
1054+
10111055
},
10121056

10131057
/**
@@ -1020,7 +1064,9 @@ Phaser.Math = {
10201064
* @return {integer} An integer in {-1, 0, 1}
10211065
*/
10221066
sign: function (x) {
1067+
10231068
return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
1069+
10241070
},
10251071

10261072
/**

0 commit comments

Comments
 (0)