phaser/src/geom/polygon/Perimeter.js

43 lines
986 B
JavaScript
Raw Normal View History

2018-09-01 00:21:34 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2018-09-01 00:21:34 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Length = require('../line/Length');
var Line = require('../line/Line');
/**
* Returns the perimeter of the given Polygon.
*
* @function Phaser.Geom.Polygon.Perimeter
* @since 3.12.0
*
* @param {Phaser.Geom.Polygon} polygon - The Polygon to get the perimeter of.
*
* @return {number} The perimeter of the Polygon.
*/
var Perimeter = function (polygon)
{
var points = polygon.points;
var perimeter = 0;
for (var i = 0; i < points.length; i++)
{
var pointA = points[i];
var pointB = points[(i + 1) % points.length];
var line = new Line(
pointA.x,
pointA.y,
pointB.x,
pointB.y
);
perimeter += Length(line);
}
return perimeter;
};
module.exports = Perimeter;