From 4da95369f99387f34de432692e9e43cf53776de2 Mon Sep 17 00:00:00 2001 From: montagdude Date: Wed, 23 Jan 2019 10:35:18 -0500 Subject: [PATCH] Fix zone area calculation (#2437) Previous method resulted in bogus zone areas (in the range of 1000s of % of frame area) when entering points with the keyboard, even after applying commit 4937a68650aeafaff78559f27818616ecd4dfbce. This change implements the method here: http://mathworld.wolfram.com/PolygonArea.html It has been tested on ZoneMinder 1.32.3 and works correctly when either entering coordinates with the keyboard or dragging points with the mouse. --- web/skins/classic/views/js/zone.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/web/skins/classic/views/js/zone.js b/web/skins/classic/views/js/zone.js index b8e486199..cb9a83386 100644 --- a/web/skins/classic/views/js/zone.js +++ b/web/skins/classic/views/js/zone.js @@ -725,11 +725,13 @@ function Polygon_calcArea( coords ) { var n_coords = coords.length; var float_area = 0.0; - for ( i = 0, j = n_coords-1; i < n_coords; j = i++ ) { - var trap_area = ( ( coords[i].x - coords[j].x ) * ( coords[i].y + coords[j].y ) ) / 2; + for ( i = 0; i < n_coords-1; i++ ) { + var trap_area = (coords[i].x*coords[i+1].y - coords[i+1].x*coords[i].y) / 2; float_area += trap_area; //printf( "%.2f (%.2f)\n", float_area, trap_area ); } + float_area += (coords[n_coords-1].x*coords[0].y - coords[0].x*coords[n_coords-1].y) / 2; + return Math.round( Math.abs( float_area ) ); }