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:
parent
ebc00f83ef
commit
bd84d4866e
1 changed files with 15 additions and 5 deletions
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue