From e14c6efbc395cbcaaa01b341401f9bd21d5ca7ca Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 22 Oct 2014 23:52:46 +0100 Subject: [PATCH] Updated to support new Pixi style of points. --- src/geom/Polygon.js | 82 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js index 860eb83a27..57deed4218 100644 --- a/src/geom/Polygon.js +++ b/src/geom/Polygon.js @@ -22,13 +22,39 @@ Phaser.Polygon = function (points) { */ this.type = Phaser.POLYGON; + // If points isn't an array, use arguments as the array + if (!(points instanceof Array)) + { + points = Array.prototype.slice.call(arguments); + } + + // If this is a flat array of numbers, convert it to points + if (points[0] instanceof Phaser.Point) + { + var p = []; + + for (var i = 0, il = points.length; i < il; i++) + { + p.push(points[i].x, points[i].y); + } + + points = p; + } + + /** + * @property {array} points - An array of Points that make up this Polygon. + */ this.points = points; + + /** + * @property {boolean} closed - Is the Polygon closed or not? + */ + this.closed = true; + }; Phaser.Polygon.prototype = { - type: null, - /** * Creates a copy of the given Polygon. * This is a deep clone, the resulting copy contains new Phaser.Point objects @@ -39,12 +65,7 @@ Phaser.Polygon.prototype = { */ clone: function (output) { - var points = []; - - for (var i=0; i < this.points.length; i++) - { - points.push(this.points[i].clone()); - } + var points = this.points.slice(); if (typeof output === "undefined" || output === null) { @@ -72,12 +93,15 @@ Phaser.Polygon.prototype = { var inside = false; // use some raycasting to test hits https://github.com/substack/point-in-polygon/blob/master/index.js - for (var i = 0, j = this.points.length - 1; i < this.points.length; j = i++) + + var length = this.points.length / 2; + + for (var i = 0, j = length - 1; i < length; j = i++) { - var xi = this.points[i].x; - var yi = this.points[i].y; - var xj = this.points[j].x; - var yj = this.points[j].y; + var xi = this.points[i * 2].x; + var yi = this.points[i * 2 + 1].y; + var xj = this.points[j * 2].x; + var yj = this.points[j * 2 + 1].y; var intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); @@ -91,7 +115,33 @@ Phaser.Polygon.prototype = { }, - setTo : function(points) { + /** + * Sets this Polygon to the given points. + * + * @method Phaser.Polygon#setTo + * @param {Phaser.Point[]|number[]} points - The array of Points. + * @return {boolean} True if the coordinates are within this polygon, otherwise false. + */ + setTo: function (points) { + + // If points isn't an array, use arguments as the array + if (!(points instanceof Array)) + { + points = Array.prototype.slice.call(arguments); + } + + // If this is a flat array of numbers, convert it to points + if (points[0] instanceof Phaser.Point) + { + var p = []; + + for (var i = 0, il = points.length; i < il; i++) + { + p.push(points[i].x, points[i].y); + } + + points = p; + } this.points = points; @@ -144,7 +194,7 @@ Object.defineProperty(Phaser.Polygon.prototype, 'points', { /** * Returns the area of the polygon. * -* @name Phaser.Circle#right +* @name Phaser.Polygon#area * @readonly */ Object.defineProperty(Phaser.Polygon.prototype, 'area', { @@ -193,4 +243,4 @@ Object.defineProperty(Phaser.Polygon.prototype, 'area', { }); // Because PIXI uses its own Polygon, we'll replace it with ours to avoid duplicating code or confusion. -PIXI.Polygon = Phaser.Polygon; +// PIXI.Polygon = Phaser.Polygon;