2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-10-29 21:46:23 +00:00
|
|
|
var Contains = require('./Contains');
|
2017-01-07 01:06:57 +00:00
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class Polygon
|
|
|
|
* @memberOf Phaser.Geom
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Geom.Point[]} [points] - [description]
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
var Polygon = new Class({
|
|
|
|
|
|
|
|
initialize:
|
2017-01-07 01:06:57 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function Polygon (points)
|
2017-01-07 01:06:57 +00:00
|
|
|
{
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* The area of this Polygon.
|
2018-01-26 13:14:41 +00:00
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @name Phaser.Geom.Polygon#area
|
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.area = 0;
|
2017-01-07 01:06:57 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* An array of number pair objects that make up this polygon. I.e. [ {x,y}, {x,y}, {x,y} ]
|
2018-01-26 13:14:41 +00:00
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @name Phaser.Geom.Polygon#points
|
|
|
|
* @type {Phaser.Geom.Point[]}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.points = [];
|
2017-01-07 01:06:57 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
if (points)
|
|
|
|
{
|
|
|
|
this.setTo(points);
|
|
|
|
}
|
|
|
|
},
|
2017-01-07 01:06:57 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @method Phaser.Geom.Polygon#contains
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} x - [description]
|
|
|
|
* @param {[type]} y - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-10-29 21:46:23 +00:00
|
|
|
contains: function (x, y)
|
|
|
|
{
|
|
|
|
return Contains(this, x, y);
|
|
|
|
},
|
|
|
|
|
2017-01-07 01:06:57 +00:00
|
|
|
/**
|
|
|
|
* Sets this Polygon to the given points.
|
|
|
|
*
|
|
|
|
* The points can be set from a variety of formats:
|
|
|
|
*
|
|
|
|
* - An array of Point objects: `[new Phaser.Point(x1, y1), ...]`
|
|
|
|
* - An array of objects with public x/y properties: `[obj1, obj2, ...]`
|
|
|
|
* - An array of paired numbers that represent point coordinates: `[x1,y1, x2,y2, ...]`
|
|
|
|
* - An array of arrays with two elements representing x/y coordinates: `[[x1, y1], [x2, y2], ...]`
|
|
|
|
*
|
|
|
|
* `setTo` may also be called without any arguments to remove all points.
|
2018-01-26 13:14:41 +00:00
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @method Phaser.Geom.Polygon#setTo
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} points - [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {Phaser.Geom.Polygon} This Polygon object.
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2017-01-07 01:06:57 +00:00
|
|
|
setTo: function (points)
|
|
|
|
{
|
|
|
|
this.area = 0;
|
2017-01-07 01:42:09 +00:00
|
|
|
this.points = [];
|
2017-01-07 01:06:57 +00:00
|
|
|
|
|
|
|
if (!Array.isArray(points))
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
var entry;
|
|
|
|
var y0 = Number.MAX_VALUE;
|
|
|
|
var p;
|
|
|
|
|
|
|
|
// The points argument is an array, so iterate through it
|
|
|
|
for (var i = 0; i < points.length; i++)
|
|
|
|
{
|
|
|
|
p = { x: 0, y: 0 };
|
|
|
|
|
|
|
|
if (typeof points[i] === 'number')
|
|
|
|
{
|
|
|
|
p.x = points[i];
|
|
|
|
p.y = points[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (Array.isArray(entry))
|
|
|
|
{
|
|
|
|
// An array of arrays?
|
|
|
|
p.x = points[i][0];
|
|
|
|
p.y = points[i][1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.x = points[i].x;
|
|
|
|
p.y = points[i].y;
|
|
|
|
}
|
|
|
|
|
2017-01-07 01:42:09 +00:00
|
|
|
this.points.push(p);
|
2017-01-07 01:06:57 +00:00
|
|
|
|
|
|
|
// Lowest boundary
|
|
|
|
if (p.y < y0)
|
|
|
|
{
|
|
|
|
y0 = p.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.calculateArea(y0);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-01-07 01:42:09 +00:00
|
|
|
* Calculates the area of the Polygon. This is available in the property Polygon.area
|
2018-01-26 13:14:41 +00:00
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @method Phaser.Geom.Polygon#calculateArea
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {number} [description]
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2018-02-07 15:27:21 +00:00
|
|
|
calculateArea: function ()
|
2017-01-07 01:06:57 +00:00
|
|
|
{
|
2017-01-07 01:42:09 +00:00
|
|
|
if (this.points.length < 3)
|
|
|
|
{
|
|
|
|
this.area = 0;
|
|
|
|
|
|
|
|
return this.area;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sum = 0;
|
2017-01-07 01:06:57 +00:00
|
|
|
var p1;
|
|
|
|
var p2;
|
|
|
|
|
2017-01-07 01:42:09 +00:00
|
|
|
for (var i = 0; i < this.points.length - 1; i++)
|
2017-01-07 01:06:57 +00:00
|
|
|
{
|
2017-01-07 01:42:09 +00:00
|
|
|
p1 = this.points[i];
|
|
|
|
p2 = this.points[i + 1];
|
2017-01-07 01:06:57 +00:00
|
|
|
|
2017-01-07 01:42:09 +00:00
|
|
|
sum += (p2.x - p1.x) * (p1.y + p2.y);
|
2017-01-07 01:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 01:42:09 +00:00
|
|
|
p1 = this.points[0];
|
|
|
|
p2 = this.points[this.points.length - 1];
|
|
|
|
|
|
|
|
sum += (p1.x - p2.x) * (p2.y + p1.y);
|
|
|
|
|
|
|
|
this.area = -sum * 0.5;
|
|
|
|
|
2017-01-07 01:06:57 +00:00
|
|
|
return this.area;
|
|
|
|
}
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
});
|
2017-01-06 23:59:45 +00:00
|
|
|
|
|
|
|
module.exports = Polygon;
|