Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 <http://maettig.com>
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"
]
}
<!DOCTYPE html>
<style type="text/css">
svg, pre { background: #DDE; border: 2px solid #66B; font-family: Consolas, monospace; padding: .3em; }
svg { width: 200px; }
</style>
<pre>This page requires JavaScript.</pre>
<svg id="s" viewBox="-1 -1 2 2" xmlns="http://www.w3.org/2000/svg">
<defs><marker id="m" markerHeight="5" markerWidth="5" viewBox="-1 -1 2 2"><circle r="1" /></marker></defs>
<path d="M0 0h1v1z" fill="#E66" fill-rule="evenodd" marker-end="url(#m)" marker-mid="url(#m)" stroke="#600" stroke-width=".01" />
<path d="M0 -1v2M-1 0h2" stroke="#009" stroke-width=".01" />
</svg>
<script type="text/javascript">
// 103 bytes
var areaPolygon = 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`
}
var p = new Array(3 + Math.random() * 3 | 0);
var d = '';
for (var i = p.length; i--; )
{
p[i] = [(Math.random() * 200 - 100 | 0) / 100, (Math.random() * 200 - 100 | 0) / 100];
d += (d ? 'L' : 'M') + p[i][0] + ' ' + p[i][1];
}
document.getElementsByTagName('PRE')[0].firstChild.data = 'area([[' + p.join('],\n [') + ']]) = ' + areaPolygon(p);
// Workaround for Firefox since getElementsByTagName does not work with inline SVG.
var n = document.getElementById('s').firstChild;
while (n && (n.nodeType != 1 || n.nodeName.toLowerCase() != 'path')) n = n.nextSibling;
n.setAttribute('d', d + 'z');
</script>
@tsaniel

This comment has been minimized.

Copy link

commented Feb 3, 2012

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}
@maettig

This comment has been minimized.

Copy link
Owner Author

commented Feb 3, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.