Add Math.Distance methods

- BetweenPoints()
- BetweenPointsSquared()
- Chebyshev()
- Snake()
This commit is contained in:
samme 2019-11-19 15:24:16 -08:00
parent bd6cabe997
commit 148e907b71
5 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,26 @@
/**
* @author samme
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Calculate the distance between two points.
*
* @function Phaser.Math.Distance.BetweenPoints
* @since 3.22.0
*
* @param {Phaser.Types.Math.Vector2Like} a - The first point.
* @param {Phaser.Types.Math.Vector2Like} b - The second point.
*
* @return {number} The distance between the points.
*/
var DistanceBetweenPoints = function (a, b)
{
var dx = a.x - b.x;
var dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
};
module.exports = DistanceBetweenPoints;

View file

@ -0,0 +1,26 @@
/**
* @author samme
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Calculate the squared distance between two points.
*
* @function Phaser.Math.Distance.BetweenPointsSquared
* @since 3.22.0
*
* @param {Phaser.Types.Math.Vector2Like} a - The first point.
* @param {Phaser.Types.Math.Vector2Like} b - The second point.
*
* @return {number} The squared distance between the points.
*/
var DistanceBetweenPointsSquared = function (a, b)
{
var dx = a.x - b.x;
var dy = a.y - b.y;
return dx * dx + dy * dy;
};
module.exports = DistanceBetweenPointsSquared;

View file

@ -0,0 +1,28 @@
/**
* @author samme
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Calculate the Chebyshev distance between two sets of coordinates (points).
*
* Chebyshev distance (or chessboard distance) is the maximum of the horizontal and vertical distances.
* It's the effective distance when movement can be horizontal, vertical, or diagonal.
*
* @function Phaser.Math.Distance.Chebyshev
* @since 3.22.0
*
* @param {number} x1 - The x coordinate of the first point.
* @param {number} y1 - The y coordinate of the first point.
* @param {number} x2 - The x coordinate of the second point.
* @param {number} y2 - The y coordinate of the second point.
*
* @return {number} The distance between each point.
*/
var ChebyshevDistance = function (x1, y1, x2, y2)
{
return Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2));
};
module.exports = ChebyshevDistance;

View file

@ -0,0 +1,28 @@
/**
* @author samme
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Calculate the snake distance between two sets of coordinates (points).
*
* Snake distance (rectilinear distance, Manhattan distance) is the sum of the horizontal and vertical distances.
* It's the effective distance when movement is allowed only horizontally or vertically (but not both).
*
* @function Phaser.Math.Distance.Snake
* @since 3.22.0
*
* @param {number} x1 - The x coordinate of the first point.
* @param {number} y1 - The y coordinate of the first point.
* @param {number} x2 - The x coordinate of the second point.
* @param {number} y2 - The y coordinate of the second point.
*
* @return {number} The distance between each point.
*/
var SnakeDistance = function (x1, y1, x2, y2)
{
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
};
module.exports = SnakeDistance;

View file

@ -11,7 +11,11 @@
module.exports = {
Between: require('./DistanceBetween'),
BetweenPoints: require('./DistanceBetweenPoints'),
BetweenPointsSquared: require('./DistanceBetweenPointsSquared'),
Chebyshev: require('./DistanceChebyshev'),
Power: require('./DistancePower'),
Snake: require('./DistanceSnake'),
Squared: require('./DistanceSquared')
};