|
<!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> |
Save 1 byte.