Skip to content

Commit c562900

Browse files
committed
Updated Phaser.Point.centroid function.
Performance boosts and bugfix with averaging.
1 parent 5e2e520 commit c562900

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/geom/Point.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ Phaser.Point.rotate = function (a, x, y, angle, asDegrees, distance) {
455455

456456
};
457457

458+
/**
459+
* Calculates centroid (or midpoint) from an array of points. If only one point is provided, that point is returned.
460+
* @method Phaser.Point.centroid
461+
* @param {Phaser.Point[]} points - The array of one or more points.
462+
* @param {Phaser.Point} [out] - Optional Point to store the value in, if not supplied a new Point object will be created.
463+
* @return {Phaser.Point} The new Point object.
464+
*/
458465
/**
459466
* Calculates centroid (or midpoint) from an array of points. If only one point is provided, that point is returned.
460467
* @method Phaser.Point.centroid
@@ -479,13 +486,11 @@ Phaser.Point.centroid = function (points, out) {
479486
return out;
480487
}
481488

482-
var totalpoints = Math.round(points.length / 2);
483-
484489
for (var i = 0; i < points.length; i++) {
485490
Phaser.Point.add(out, points[i], out);
486491
}
487492

488-
out.divide(totalpoints, totalpoints);
493+
out.divide(points.length, points.length);
489494

490495
return out;
491496
};

0 commit comments

Comments
 (0)