0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-12 16:08:50 -05:00

fixed Vertices.centre calculation

This commit is contained in:
liabru 2014-05-02 18:59:49 +01:00
parent ebc00f83ef
commit bd84d4866e

View file

@ -48,23 +48,30 @@ var Vertices = {};
* @return {vector} The centre point
*/
Vertices.centre = function(vertices) {
var cx = 0, cy = 0;
var area = Vertices.area(vertices, true),
centre = { x: 0, y: 0 },
cross,
temp,
j;
for (var i = 0; i < vertices.length; i++) {
cx += vertices[i].x;
cy += vertices[i].y;
j = (i + 1) % vertices.length;
cross = Vector.cross(vertices[i], vertices[j]);
temp = Vector.mult(Vector.add(vertices[i], vertices[j]), cross);
centre = Vector.add(centre, temp);
}
return { x: cx / vertices.length, y: cy / vertices.length };
return Vector.div(centre, 6 * area);
};
/**
* Description
* @method area
* @param {vertices} vertices
* @param {bool} signed
* @return {number} The area
*/
Vertices.area = function(vertices) {
Vertices.area = function(vertices, signed) {
var area = 0,
j = vertices.length - 1;
@ -73,6 +80,9 @@ var Vertices = {};
j = i;
}
if (signed)
return area / 2;
return Math.abs(area) / 2;
};