-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
GetPoints.js
71 lines (60 loc) · 2.31 KB
/
GetPoints.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Length = require('../line/Length');
var Line = require('../line/Line');
var Perimeter = require('./Perimeter');
/**
* Returns an array of Point objects containing the coordinates of the points around the perimeter of the Polygon,
* based on the given quantity or stepRate values.
*
* @function Phaser.Geom.Polygon.GetPoints
* @since 3.12.0
*
* @param {Phaser.Geom.Polygon} polygon - The Polygon to get the points from.
* @param {number} quantity - The amount of points to return. If a falsey value the quantity will be derived from the `stepRate` instead.
* @param {number} [stepRate] - Sets the quantity by getting the perimeter of the Polygon and dividing it by the stepRate.
* @param {array} [output] - An array to insert the points in to. If not provided a new array will be created.
*
* @return {Phaser.Geom.Point[]} An array of Point objects pertaining to the points around the perimeter of the Polygon.
*/
var GetPoints = function (polygon, quantity, stepRate, out)
{
if (out === undefined) { out = []; }
var points = polygon.points;
var perimeter = Perimeter(polygon);
// 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 / stepRate;
}
for (var i = 0; i < quantity; i++)
{
var position = perimeter * (i / quantity);
var accumulatedPerimeter = 0;
for (var j = 0; j < points.length; j++)
{
var pointA = points[j];
var pointB = points[(j + 1) % points.length];
var line = new Line(
pointA.x,
pointA.y,
pointB.x,
pointB.y
);
var length = Length(line);
if (position < accumulatedPerimeter || position > accumulatedPerimeter + length)
{
accumulatedPerimeter += length;
continue;
}
var point = line.getPoint((position - accumulatedPerimeter) / length);
out.push(point);
break;
}
}
return out;
};
module.exports = GetPoints;