/
Smooth.js
70 lines (59 loc) · 1.54 KB
/
Smooth.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Igor Ognichenko <ognichenko.igor@gmail.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* @ignore
*/
var copy = function (out, a)
{
out[0] = a[0];
out[1] = a[1];
return out;
};
/**
* Takes a Polygon object and applies Chaikin's smoothing algorithm on its points.
*
* @function Phaser.Geom.Polygon.Smooth
* @since 3.13.0
*
* @generic {Phaser.Geom.Polygon} O - [polygon,$return]
*
* @param {Phaser.Geom.Polygon} polygon - The polygon to be smoothed. The polygon will be modified in-place and returned.
*
* @return {Phaser.Geom.Polygon} The input polygon.
*/
var Smooth = function (polygon)
{
var i;
var points = [];
var data = polygon.points;
for (i = 0; i < data.length; i++)
{
points.push([ data[i].x, data[i].y ]);
}
var output = [];
if (points.length > 0)
{
output.push(copy([ 0, 0 ], points[0]));
}
for (i = 0; i < points.length - 1; i++)
{
var p0 = points[i];
var p1 = points[i + 1];
var p0x = p0[0];
var p0y = p0[1];
var p1x = p1[0];
var p1y = p1[1];
output.push([ 0.85 * p0x + 0.15 * p1x, 0.85 * p0y + 0.15 * p1y ]);
output.push([ 0.15 * p0x + 0.85 * p1x, 0.15 * p0y + 0.85 * p1y ]);
}
if (points.length > 1)
{
output.push(copy([ 0, 0 ], points[points.length - 1]));
}
return polygon.setTo(output);
};
module.exports = Smooth;