phaser/src/geom/rectangle/GetPoints.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var GetPoint = require('./GetPoint');
var Perimeter = require('./Perimeter');
// Return an array of points from the perimeter of the rectangle
// each spaced out based on the quantity or step required
/**
2018-09-27 14:29:32 +00:00
* Return an array of points from the perimeter of the rectangle, each spaced out based on the quantity or step required.
*
* @function Phaser.Geom.Rectangle.GetPoints
* @since 3.0.0
*
2018-03-27 13:27:08 +00:00
* @generic {Phaser.Geom.Point[]} O - [out,$return]
*
2018-09-27 14:29:32 +00:00
* @param {Phaser.Geom.Rectangle} rectangle - The Rectangle object to get the points from.
2019-10-29 06:18:26 +00:00
* @param {number} step - Step between points. Used to calculate the number of points to return when quantity is falsey. Ignored if quantity is positive.
* @param {integer} quantity - The number of evenly spaced points from the rectangles perimeter to return. If falsey, step param will be used to calculate the number of points.
2018-09-27 14:29:32 +00:00
* @param {(array|Phaser.Geom.Point[])} [out] - An optional array to store the points in.
*
2018-09-27 14:29:32 +00:00
* @return {(array|Phaser.Geom.Point[])} An array of Points from the perimeter of the rectangle.
*/
var GetPoints = function (rectangle, quantity, stepRate, out)
{
if (out === undefined) { out = []; }
// If quantity is a falsey value (false, null, 0, undefined, etc) then we calculate it based on the stepRate instead.
if (!quantity && stepRate > 0)
{
quantity = Perimeter(rectangle) / stepRate;
}
for (var i = 0; i < quantity; i++)
{
var position = i / quantity;
out.push(GetPoint(rectangle, position));
}
return out;
};
module.exports = GetPoints;