Skip to content

Commit 4431e72

Browse files
committed
Merge pull request phaserjs#1770 from jeremyosborne/distsq
Fix for phaserjs#1761: [Feature Request] Add Math.distanceSq().
2 parents ab594e9 + 61f24f1 commit 4431e72

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

src/math/Math.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Phaser.Math = {
340340
* Find the angle of a segment from (x1, y1) -> (x2, y2).
341341
* Note that the difference between this method and Math.angleBetween is that this assumes the y coordinate travels
342342
* down the screen.
343-
*
343+
*
344344
* @method Phaser.Math#angleBetweenY
345345
* @param {number} x1
346346
* @param {number} y1
@@ -577,7 +577,7 @@ Phaser.Math = {
577577
*
578578
* @method Phaser.Math#isOdd
579579
* @param {integer} n - The number to check.
580-
* @return {boolean} True if the given number is odd. False if the given number is even.
580+
* @return {boolean} True if the given number is odd. False if the given number is even.
581581
*/
582582
isOdd: function (n) {
583583
// Does not work with extremely large values
@@ -597,7 +597,7 @@ Phaser.Math = {
597597
},
598598

599599
/**
600-
* Variation of Math.min that can be passed either an array of numbers or the numbers as parameters.
600+
* Variation of Math.min that can be passed either an array of numbers or the numbers as parameters.
601601
*
602602
* Prefer the standard `Math.min` function when appropriate.
603603
*
@@ -606,7 +606,7 @@ Phaser.Math = {
606606
* @see {@link http://jsperf.com/math-s-min-max-vs-homemade}
607607
*/
608608
min: function () {
609-
609+
610610
if (arguments.length === 1 && typeof arguments[0] === 'object')
611611
{
612612
var data = arguments[0];
@@ -615,7 +615,7 @@ Phaser.Math = {
615615
{
616616
var data = arguments;
617617
}
618-
618+
619619
for (var i = 1, min = 0, len = data.length; i < len; i++)
620620
{
621621
if (data[i] < data[min])
@@ -638,7 +638,7 @@ Phaser.Math = {
638638
* @see {@link http://jsperf.com/math-s-min-max-vs-homemade}
639639
*/
640640
max: function () {
641-
641+
642642
if (arguments.length === 1 && typeof arguments[0] === 'object')
643643
{
644644
var data = arguments[0];
@@ -647,7 +647,7 @@ Phaser.Math = {
647647
{
648648
var data = arguments;
649649
}
650-
650+
651651
for (var i = 1, max = 0, len = data.length; i < len; i++)
652652
{
653653
if (data[i] > data[max])
@@ -763,7 +763,7 @@ Phaser.Math = {
763763

764764
/**
765765
* A Linear Interpolation Method, mostly used by Phaser.Tween.
766-
*
766+
*
767767
* @method Phaser.Math#linearInterpolation
768768
* @param {Array} v - The input array of values to interpolate between.
769769
* @param {number} k - The percentage of interpolation, between 0 and 1.
@@ -791,7 +791,7 @@ Phaser.Math = {
791791

792792
/**
793793
* A Bezier Interpolation Method, mostly used by Phaser.Tween.
794-
*
794+
*
795795
* @method Phaser.Math#bezierInterpolation
796796
* @param {Array} v - The input array of values to interpolate between.
797797
* @param {number} k - The percentage of interpolation, between 0 and 1.
@@ -813,7 +813,7 @@ Phaser.Math = {
813813

814814
/**
815815
* A Catmull Rom Interpolation Method, mostly used by Phaser.Tween.
816-
*
816+
*
817817
* @method Phaser.Math#catmullRomInterpolation
818818
* @param {Array} v - The input array of values to interpolate between.
819819
* @param {number} k - The percentage of interpolation, between 0 and 1.
@@ -853,7 +853,7 @@ Phaser.Math = {
853853

854854
/**
855855
* Calculates a linear (interpolation) value over t.
856-
*
856+
*
857857
* @method Phaser.Math#linear
858858
* @param {number} p0
859859
* @param {number} p1
@@ -900,7 +900,7 @@ Phaser.Math = {
900900

901901
/**
902902
* Calculates a catmum rom value.
903-
*
903+
*
904904
* @method Phaser.Math#catmullRom
905905
* @protected
906906
* @param {number} p0
@@ -940,7 +940,7 @@ Phaser.Math = {
940940
* @param {any[]} objects - An array of objects.
941941
* @param {integer} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
942942
* @param {integer} length - Optional restriction on the number of values you want to randomly select from.
943-
* @return {object} The random object that was selected.
943+
* @return {object} The random object that was selected.
944944
* @deprecated 2.2.0 - Use {@link Phaser.ArrayUtils.getRandomItem}
945945
*/
946946
getRandom: function (objects, startIndex, length) {
@@ -1095,6 +1095,26 @@ Phaser.Math = {
10951095

10961096
},
10971097

1098+
/**
1099+
* Returns the euclidian distance squared between the two given set of
1100+
* coordinates (cuts out a square root operation before returning).
1101+
*
1102+
* @method Phaser.Math#distanceSq
1103+
* @param {number} x1
1104+
* @param {number} y1
1105+
* @param {number} x2
1106+
* @param {number} y2
1107+
* @return {number} The distance squared between the two sets of coordinates.
1108+
*/
1109+
distanceSq: function (x1, y1, x2, y2) {
1110+
1111+
var dx = x1 - x2;
1112+
var dy = y1 - y2;
1113+
1114+
return dx * dx + dy * dy;
1115+
1116+
},
1117+
10981118
/**
10991119
* Returns the distance between the two given set of coordinates at the power given.
11001120
*

0 commit comments

Comments
 (0)