mirror of
https://github.com/photonstorm/phaser
synced 2024-11-26 22:52:14 +00:00
Add Math.Distance methods
- BetweenPoints() - BetweenPointsSquared() - Chebyshev() - Snake()
This commit is contained in:
parent
bd6cabe997
commit
148e907b71
5 changed files with 112 additions and 0 deletions
26
src/math/distance/DistanceBetweenPoints.js
Normal file
26
src/math/distance/DistanceBetweenPoints.js
Normal 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;
|
26
src/math/distance/DistanceBetweenPointsSquared.js
Normal file
26
src/math/distance/DistanceBetweenPointsSquared.js
Normal 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;
|
28
src/math/distance/DistanceChebyshev.js
Normal file
28
src/math/distance/DistanceChebyshev.js
Normal 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;
|
28
src/math/distance/DistanceSnake.js
Normal file
28
src/math/distance/DistanceSnake.js
Normal 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;
|
|
@ -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')
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue