{{ message }}

Instantly share code, notes, and snippets.

Created Feb 2, 2012
areaPolygon in 140byt.es
 function(a) //array of points, e.g. `[[1, 2], [3, 4]]` { for ( //for all points in the array var b = 0, //initially, the area is zero c = a.length, //start from the last point in the array d; //declare `d` c; //continue as long as `c` is not zero b += (d[0] - a[--c][0]) //this calculates the sum of a series of triangles * (d[1] + a[ c][1]) //from the previous point `d` to the current one / 2) //note that this is executed after the following line d = a[d ? c : 0]; //`d` becomes a shortcut for `a[c % a.length]` return b < 0 ? -b : b //short replacement for `Math.abs` }
 function(a){for(var b=0,c=a.length,d;c;b+=(d[0]-a[--c][0])*(d[1]+a[c][1])/2)d=a[d?c:0];return b<0?-b:b}
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2012 Thiemo Mättig Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
 { "name": "areaPolygon", "description": "Calculate the area of non-self-intersecting polygons.", "keywords": [ "area", "math", "polygon" ] }

 Save 1 byte. `function(a){for(var b=0,c=a.length,d;c;b+=(d[0]-a[--c][0])*(d[1]+a[c][1])/2)d=a[d?c:0];return b<0?-b:b}`
 That's impossi... oh, wait, it's not. This is funny because I had so many iterations while golfing this down (they are all included here) but the `/2` never moved.