Skip to content

Commit 148e907

Browse files
committed
Add Math.Distance methods
- BetweenPoints() - BetweenPointsSquared() - Chebyshev() - Snake()
1 parent bd6cabe commit 148e907

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author samme
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Calculate the distance between two points.
9+
*
10+
* @function Phaser.Math.Distance.BetweenPoints
11+
* @since 3.22.0
12+
*
13+
* @param {Phaser.Types.Math.Vector2Like} a - The first point.
14+
* @param {Phaser.Types.Math.Vector2Like} b - The second point.
15+
*
16+
* @return {number} The distance between the points.
17+
*/
18+
var DistanceBetweenPoints = function (a, b)
19+
{
20+
var dx = a.x - b.x;
21+
var dy = a.y - b.y;
22+
23+
return Math.sqrt(dx * dx + dy * dy);
24+
};
25+
26+
module.exports = DistanceBetweenPoints;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author samme
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Calculate the squared distance between two points.
9+
*
10+
* @function Phaser.Math.Distance.BetweenPointsSquared
11+
* @since 3.22.0
12+
*
13+
* @param {Phaser.Types.Math.Vector2Like} a - The first point.
14+
* @param {Phaser.Types.Math.Vector2Like} b - The second point.
15+
*
16+
* @return {number} The squared distance between the points.
17+
*/
18+
var DistanceBetweenPointsSquared = function (a, b)
19+
{
20+
var dx = a.x - b.x;
21+
var dy = a.y - b.y;
22+
23+
return dx * dx + dy * dy;
24+
};
25+
26+
module.exports = DistanceBetweenPointsSquared;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author samme
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Calculate the Chebyshev distance between two sets of coordinates (points).
9+
*
10+
* Chebyshev distance (or chessboard distance) is the maximum of the horizontal and vertical distances.
11+
* It's the effective distance when movement can be horizontal, vertical, or diagonal.
12+
*
13+
* @function Phaser.Math.Distance.Chebyshev
14+
* @since 3.22.0
15+
*
16+
* @param {number} x1 - The x coordinate of the first point.
17+
* @param {number} y1 - The y coordinate of the first point.
18+
* @param {number} x2 - The x coordinate of the second point.
19+
* @param {number} y2 - The y coordinate of the second point.
20+
*
21+
* @return {number} The distance between each point.
22+
*/
23+
var ChebyshevDistance = function (x1, y1, x2, y2)
24+
{
25+
return Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2));
26+
};
27+
28+
module.exports = ChebyshevDistance;

src/math/distance/DistanceSnake.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author samme
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Calculate the snake distance between two sets of coordinates (points).
9+
*
10+
* Snake distance (rectilinear distance, Manhattan distance) is the sum of the horizontal and vertical distances.
11+
* It's the effective distance when movement is allowed only horizontally or vertically (but not both).
12+
*
13+
* @function Phaser.Math.Distance.Snake
14+
* @since 3.22.0
15+
*
16+
* @param {number} x1 - The x coordinate of the first point.
17+
* @param {number} y1 - The y coordinate of the first point.
18+
* @param {number} x2 - The x coordinate of the second point.
19+
* @param {number} y2 - The y coordinate of the second point.
20+
*
21+
* @return {number} The distance between each point.
22+
*/
23+
var SnakeDistance = function (x1, y1, x2, y2)
24+
{
25+
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
26+
};
27+
28+
module.exports = SnakeDistance;

src/math/distance/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
module.exports = {
1212

1313
Between: require('./DistanceBetween'),
14+
BetweenPoints: require('./DistanceBetweenPoints'),
15+
BetweenPointsSquared: require('./DistanceBetweenPointsSquared'),
16+
Chebyshev: require('./DistanceChebyshev'),
1417
Power: require('./DistancePower'),
18+
Snake: require('./DistanceSnake'),
1519
Squared: require('./DistanceSquared')
1620

1721
};

0 commit comments

Comments
 (0)