'use strict';
exports.__esModule = true;
var _Point = require('../Point');
var _Point2 = _interopRequireDefault(_Point);
var _const = require('../../const');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* @class
* @memberof PIXI
*/
var Polygon = function () {
/**
* @param {PIXI.Point[]|number[]} points - This can be an array of Points
* that form the polygon, a flat array of numbers that will be interpreted as [x,y, x,y, ...], or
* the arguments passed can be all the points of the polygon e.g.
* `new PIXI.Polygon(new PIXI.Point(), new PIXI.Point(), ...)`, or the arguments passed can be flat
* x,y values e.g. `new Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are Numbers.
*/
function Polygon() {
for (var _len = arguments.length, points = Array(_len), _key = 0; _key < _len; _key++) {
points[_key] = arguments[_key];
}
_classCallCheck(this, Polygon);
if (Array.isArray(points[0])) {
points = points[0];
}
// if this is an array of points, convert it to a flat array of numbers
if (points[0] instanceof _Point2.default) {
var p = [];
for (var i = 0, il = points.length; i < il; i++) {
p.push(points[i].x, points[i].y);
}
points = p;
}
this.closed = true;
/**
* An array of the points of this polygon
*
* @member {number[]}
*/
this.points = points;
/**
* The type of the object, mainly used to avoid `instanceof` checks
*
* @member {number}
* @readOnly
* @default PIXI.SHAPES.POLY
* @see PIXI.SHAPES
*/
this.type = _const.SHAPES.POLY;
}
/**
* Creates a clone of this polygon
*
* @return {PIXI.Polygon} a copy of the polygon
*/
Polygon.prototype.clone = function clone() {
return new Polygon(this.points.slice());
};
/**
* Closes the polygon, adding points if necessary.
*
*/
Polygon.prototype.close = function close() {
var points = this.points;
// close the poly if the value is true!
if (points[0] !== points[points.length - 2] || points[1] !== points[points.length - 1]) {
points.push(points[0], points[1]);
}
};
/**
* Checks whether the x and y coordinates passed to this function are contained within this polygon
*
* @param {number} x - The X coordinate of the point to test
* @param {number} y - The Y coordinate of the point to test
* @return {boolean} Whether the x/y coordinates are within this polygon
*/
Polygon.prototype.contains = function contains(x, y) {
var inside = false;
// use some raycasting to test hits
// https://github.com/substack/point-in-polygon/blob/master/index.js
var length = this.points.length / 2;
for (var i = 0, j = length - 1; i < length; j = i++) {
var xi = this.points[i * 2];
var yi = this.points[i * 2 + 1];
var xj = this.points[j * 2];
var yj = this.points[j * 2 + 1];
var intersect = yi > y !== yj > y && x < (xj - xi) * ((y - yi) / (yj - yi)) + xi;
if (intersect) {
inside = !inside;
}
}
return inside;
};
return Polygon;
}();
exports.default = Polygon;
//# sourceMappingURL=Polygon.js.map