diff --git a/src/geometry/Vertices.js b/src/geometry/Vertices.js index d2801cd..9e6ea5f 100644 --- a/src/geometry/Vertices.js +++ b/src/geometry/Vertices.js @@ -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; };