From 35d60f6b4812b64b7505528408912d56d21df513 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Mon, 28 Jan 2013 21:25:28 -0500 Subject: [PATCH] added computeAreas to get area --- rhill-voronoi-core.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/rhill-voronoi-core.js b/rhill-voronoi-core.js index 6da81d8..eaa04cd 100644 --- a/rhill-voronoi-core.js +++ b/rhill-voronoi-core.js @@ -1380,6 +1380,23 @@ Voronoi.prototype.closeCells = function(bbox) { } }; +// http://www.wikihow.com/Calculate-the-Area-of-a-Polygon +Voronoi.prototype.computeAreas = function() { + for (var i = 0 ; i < this.cells.length ; i++){ + var Stotal=0 + var Ttotal=0 + var cell=this.cells[i]; + for ( var j = 0 ; j < cell.halfedges.length ; j++ ) { + var vertexa = cell.halfedges[j].getStartpoint(); + var vertexb = cell.halfedges[j].getEndpoint(); + Stotal+=vertexa.x*vertexb.y + Ttotal+=vertexa.y*vertexb.x + } + cell.area=(Ttotal-Stotal)/2 + } + +} + // --------------------------------------------------------------------------- // Top-level Fortune loop @@ -1453,7 +1470,7 @@ Voronoi.prototype.compute = function(sites, bbox) { // add missing edges in order to close opened cells this.closeCells(bbox); - + this.computeAreas(); // to measure execution time var stopTime = new Date();