Skip to content

Commit

Permalink
Updated to support new Pixi style of points.
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Oct 22, 2014
1 parent 157d515 commit e14c6ef
Showing 1 changed file with 66 additions and 16 deletions.
82 changes: 66 additions & 16 deletions src/geom/Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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;

0 comments on commit e14c6ef

Please sign in to comment.