pax_global_header00006660000000000000000000000064130605643140014514gustar00rootroot0000000000000052 comment=1bfcdc8b63578b936c785639fa73cec566713c45 d3-polygon-1.0.3/000077500000000000000000000000001306056431400135105ustar00rootroot00000000000000d3-polygon-1.0.3/.eslintrc000066400000000000000000000001471306056431400153360ustar00rootroot00000000000000parserOptions: sourceType: module extends: "eslint:recommended" rules: no-cond-assign: 0 d3-polygon-1.0.3/.gitignore000066400000000000000000000001001306056431400154670ustar00rootroot00000000000000*.sublime-workspace .DS_Store build/ node_modules npm-debug.log d3-polygon-1.0.3/.npmignore000066400000000000000000000000361306056431400155060ustar00rootroot00000000000000*.sublime-* build/*.zip test/ d3-polygon-1.0.3/LICENSE000066400000000000000000000027031306056431400145170ustar00rootroot00000000000000Copyright 2010-2016 Mike Bostock All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. d3-polygon-1.0.3/README.md000066400000000000000000000061251306056431400147730ustar00rootroot00000000000000# d3-polygon This module provides a few basic geometric operations for two-dimensional polygons. Each polygon is represented as an array of two-element arrays [​[x1, y1], [x2, y2], …], and may either be closed (wherein the first and last point are the same) or open (wherein they are not). Typically polygons are in counterclockwise order, assuming a coordinate system where the origin ⟨0,0⟩ is in the top-left corner. ## Installing If you use NPM, `npm install d3-polygon`. Otherwise, download the [latest release](https://github.com/d3/d3-polygon/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-polygon.v1.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported: ```html ``` [Try d3-polygon in your browser.](https://tonicdev.com/npm/d3-polygon) ## API Reference # d3.polygonArea(polygon) [<>](https://github.com/d3/d3-polygon/blob/master/src/area.js#L1 "Source Code") Returns the signed area of the specified *polygon*. If the vertices of the polygon are in counterclockwise order (assuming a coordinate system where the origin ⟨0,0⟩ is in the top-left corner), the returned area is positive; otherwise it is negative, or zero. # d3.polygonCentroid(polygon) [<>](https://github.com/d3/d3-polygon/blob/master/src/centroid.js#L1 "Source Code") Returns the [centroid](https://en.wikipedia.org/wiki/Centroid) of the specified *polygon*. # d3.polygonHull(points) [<>](https://github.com/d3/d3-polygon/blob/master/src/hull.js#L23 "Source Code") Returns the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) of the specified *points* using [Andrew’s monotone chain algorithm](http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain). The returned hull is represented as an array containing a subset of the input *points* arranged in counterclockwise order. Returns null if *points* has fewer than three elements. # d3.polygonContains(polygon, point) [<>](https://github.com/d3/d3-polygon/blob/master/src/contains.js#L1 "Source Code") Returns true if and only if the specified *point* is [inside the specified *polygon*](https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html). # d3.polygonLength(polygon) [<>](https://github.com/d3/d3-polygon/blob/master/src/length.js#L1 "Source Code") Returns the length of the perimeter of the specified *polygon*. d3-polygon-1.0.3/d3-polygon.sublime-project000066400000000000000000000002711306056431400205310ustar00rootroot00000000000000{ "folders": [ { "path": ".", "file_exclude_patterns": [ "*.sublime-workspace" ], "folder_exclude_patterns": [ "build" ] } ] } d3-polygon-1.0.3/img/000077500000000000000000000000001306056431400142645ustar00rootroot00000000000000d3-polygon-1.0.3/img/hull.png000066400000000000000000000257531306056431400157520ustar00rootroot00000000000000PNG  IHDR/ iCCPICC ProfileHPSϽBc**!JbG\ *"TV",*X#}o{3ߜ||䜙 _fi,A=*: 8)a2AAw}l_%Nd!d!| _5VegyAg3-?ϽssBd 8~GlCF#lcsy["Jb"됑g0--} Ou8/drD<-s{p35vo PG$ ]ٳt?.0=,' }l S])X|[`Az>/u~C b,pNRXgs#.pfJwQ^ (}cZbo,ZYIa>=Da'xxp|~&?5hToQ>3;Tnr8X'H? VϞ+_#r-I3x,cCwn'}OKb.$Y15|bNrvpf%df:fi TFX_o+xP$ 2B٢QT"Jڀ*@PM.T&jEStG Rt ݋AOc(%Da8U|L1ӊ|b4냍&cbw`a!(v p@Ý>IxU9 sY s4AE'؄5]jB:a0M$adfb xD"H$.iBL!}&KX\K&'P(J %RO@yB$F3c6{#Nw_!#^,~Rk SbDD]IIdd+/pRRRl*TU'U>s/2tWz*KPSRQU M먇7? jj$jјT \٨@eu@OkJ[G;R{v 9NN#]nn-=^!A}XJ?ILl`m58d0d13V5"e5ӌsۍߘhĘ13njejZmL,׬읹9˼bE[KÖVV[zYX m4mlmJlshw} ?R^,Yzɨ#ӱq؉tiY͙\EÅRU57S7[۔znGǀgg/u/WׄZn eQϘ]G +{/ | %\,,d]H_(5tehC0]auÅ=SEQ&Q룮E+Ds;bp1151<_6k{g˯PXJ̕'0qq q_*d<#<~:zvac'8&%u}aM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OB<ԏ0=-.uסOs"Hp}|*/amZ4:jDeG΁GMe6c݇~9?D&9a `e͹'gwpģ?ߥP`ϣBTs=c+LHI2Q9W`Q':s| ^fkBX){} q@yKCƣHBJrCoп]f߯y9%>s.Pe@V.|) ;$.º_$ޢaM%>.%w)~'*IU7B($83H<>$jD;"/@t| f_'Q(-Z^/JNB\4*Y Ч Oӟmѐ)o'#|NB|E ڝuI }opNZ@hӹt 0nױfRuN^bw q{_͉R8)s zU*/+x!G=Qvm]6prCflՐpr ЩiϭyTjrodN_m~,#t/ ЩҎg;].8Es]GNSP#evߦ@?Ox.HJݦԍ RmJ)ݞԍ b=5_ Rg&^_tRX*$S :5=2(f=QCW(u/5S:u<1q$M8{1ۼO իHfa &tn8E{ ]1tS7_8u+Puv+u) ]Mf6tySbJ$7pC2uv+u]M2n/;Ѓ)b]ԭ!M螧n*8@8u{!M1'螥n1`n8A uS 1`n8Aw3uv+uS+ΗlMqSt+Aw0ug;Nx?8CNSbJ~Kvno;Ѓ} ߡە- \Ϟ \ ϒ/ \@OT_ \`[OkZg$VR7_@A8u  S7ALF \Рng7uKRK3.?nwD޻4b"udy޻,3p~i|.x./R7#.SBﭿ:"uS"b,b,b,b,b,b,b,b,Y Y Y Y Y Y Y Y :::::::/V:$IENDB`d3-polygon-1.0.3/index.js000066400000000000000000000004231306056431400151540ustar00rootroot00000000000000export {default as polygonArea} from "./src/area"; export {default as polygonCentroid} from "./src/centroid"; export {default as polygonHull} from "./src/hull"; export {default as polygonContains} from "./src/contains"; export {default as polygonLength} from "./src/length"; d3-polygon-1.0.3/package.json000066400000000000000000000026611306056431400160030ustar00rootroot00000000000000{ "name": "d3-polygon", "version": "1.0.3", "description": "Operations for two-dimensional polygons.", "keywords": [ "d3", "d3-module", "polygon", "hull", "geometry", "graphics" ], "homepage": "https://d3js.org/d3-polygon/", "license": "BSD-3-Clause", "author": { "name": "Mike Bostock", "url": "http://bost.ocks.org/mike" }, "main": "build/d3-polygon.js", "module": "index", "jsnext:main": "index", "repository": { "type": "git", "url": "https://github.com/d3/d3-polygon.git" }, "scripts": { "pretest": "rm -rf build && mkdir build && rollup --banner \"$(preamble)\" -f umd -n d3 -o build/d3-polygon.js -- index.js", "test": "tape 'test/**/*-test.js' && eslint index.js src", "prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-polygon.js -c -m -o build/d3-polygon.min.js", "postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-polygon/build/d3-polygon.js d3-polygon.v1.js && cp ../d3-polygon/build/d3-polygon.min.js d3-polygon.v1.min.js && git add d3-polygon.v1.js d3-polygon.v1.min.js && git commit -m \"d3-polygon ${npm_package_version}\" && git push && cd - && zip -j build/d3-polygon.zip -- LICENSE README.md build/d3-polygon.js build/d3-polygon.min.js" }, "devDependencies": { "eslint": "3", "package-preamble": "0.0", "rollup": "0.41", "tape": "4", "uglify-js": "^2.8.11" } } d3-polygon-1.0.3/src/000077500000000000000000000000001306056431400142775ustar00rootroot00000000000000d3-polygon-1.0.3/src/area.js000066400000000000000000000003631306056431400155470ustar00rootroot00000000000000export default function(polygon) { var i = -1, n = polygon.length, a, b = polygon[n - 1], area = 0; while (++i < n) { a = b; b = polygon[i]; area += a[1] * b[0] - a[0] * b[1]; } return area / 2; } d3-polygon-1.0.3/src/centroid.js000066400000000000000000000005321306056431400164440ustar00rootroot00000000000000export default function(polygon) { var i = -1, n = polygon.length, x = 0, y = 0, a, b = polygon[n - 1], c, k = 0; while (++i < n) { a = b; b = polygon[i]; k += c = a[0] * b[1] - b[0] * a[1]; x += (a[0] + b[0]) * c; y += (a[1] + b[1]) * c; } return k *= 3, [x / k, y / k]; } d3-polygon-1.0.3/src/contains.js000066400000000000000000000006331306056431400164550ustar00rootroot00000000000000export default function(polygon, point) { var n = polygon.length, p = polygon[n - 1], x = point[0], y = point[1], x0 = p[0], y0 = p[1], x1, y1, inside = false; for (var i = 0; i < n; ++i) { p = polygon[i], x1 = p[0], y1 = p[1]; if (((y1 > y) !== (y0 > y)) && (x < (x0 - x1) * (y - y1) / (y0 - y1) + x1)) inside = !inside; x0 = x1, y0 = y1; } return inside; } d3-polygon-1.0.3/src/cross.js000066400000000000000000000006221306056431400157660ustar00rootroot00000000000000// Returns the 2D cross product of AB and AC vectors, i.e., the z-component of // the 3D cross product in a quadrant I Cartesian coordinate system (+x is // right, +y is up). Returns a positive value if ABC is counter-clockwise, // negative if clockwise, and zero if the points are collinear. export default function(a, b, c) { return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); } d3-polygon-1.0.3/src/hull.js000066400000000000000000000032561306056431400156070ustar00rootroot00000000000000import cross from "./cross"; function lexicographicOrder(a, b) { return a[0] - b[0] || a[1] - b[1]; } // Computes the upper convex hull per the monotone chain algorithm. // Assumes points.length >= 3, is sorted by x, unique in y. // Returns an array of indices into points in left-to-right order. function computeUpperHullIndexes(points) { var n = points.length, indexes = [0, 1], size = 2; for (var i = 2; i < n; ++i) { while (size > 1 && cross(points[indexes[size - 2]], points[indexes[size - 1]], points[i]) <= 0) --size; indexes[size++] = i; } return indexes.slice(0, size); // remove popped points } export default function(points) { if ((n = points.length) < 3) return null; var i, n, sortedPoints = new Array(n), flippedPoints = new Array(n); for (i = 0; i < n; ++i) sortedPoints[i] = [+points[i][0], +points[i][1], i]; sortedPoints.sort(lexicographicOrder); for (i = 0; i < n; ++i) flippedPoints[i] = [sortedPoints[i][0], -sortedPoints[i][1]]; var upperIndexes = computeUpperHullIndexes(sortedPoints), lowerIndexes = computeUpperHullIndexes(flippedPoints); // Construct the hull polygon, removing possible duplicate endpoints. var skipLeft = lowerIndexes[0] === upperIndexes[0], skipRight = lowerIndexes[lowerIndexes.length - 1] === upperIndexes[upperIndexes.length - 1], hull = []; // Add upper hull in right-to-l order. // Then add lower hull in left-to-right order. for (i = upperIndexes.length - 1; i >= 0; --i) hull.push(points[sortedPoints[upperIndexes[i]][2]]); for (i = +skipLeft; i < lowerIndexes.length - skipRight; ++i) hull.push(points[sortedPoints[lowerIndexes[i]][2]]); return hull; } d3-polygon-1.0.3/src/length.js000066400000000000000000000005671306056431400161260ustar00rootroot00000000000000export default function(polygon) { var i = -1, n = polygon.length, b = polygon[n - 1], xa, ya, xb = b[0], yb = b[1], perimeter = 0; while (++i < n) { xa = xb; ya = yb; b = polygon[i]; xb = b[0]; yb = b[1]; xa -= xb; ya -= yb; perimeter += Math.sqrt(xa * xa + ya * ya); } return perimeter; } d3-polygon-1.0.3/test/000077500000000000000000000000001306056431400144675ustar00rootroot00000000000000d3-polygon-1.0.3/test/area-test.js000066400000000000000000000030461306056431400167150ustar00rootroot00000000000000var tape = require("tape"), polygon = require("../"); tape("polygonArea(polygon) returns the expected value for closed counterclockwise polygons", function(test) { test.equal(polygon.polygonArea([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), 1); test.end(); }); tape("polygonArea(polygon) returns the expected value for closed clockwise polygons", function(test) { test.equal(polygon.polygonArea([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), -1); test.equal(polygon.polygonArea([[1, 1], [3, 2], [2, 3], [1, 1]]), -1.5); test.end(); }); tape("polygonArea(polygon) returns the expected value for open counterclockwise polygons", function(test) { test.equal(polygon.polygonArea([[0, 0], [0, 1], [1, 1], [1, 0]]), 1); test.end(); }); tape("polygonArea(polygon) returns the expected value for open clockwise polygons", function(test) { test.equal(polygon.polygonArea([[0, 0], [1, 0], [1, 1], [0, 1]]), -1); test.equal(polygon.polygonArea([[1, 1], [3, 2], [2, 3]]), -1.5); test.end(); }); tape("polygonArea(polygon) returns the expected value for a very large polygon", function(test) { var start = 0, stop = 1e8, step = 1e4, points = []; for (var value = 0; value < stop; value += step) points.push([0, value]); for (var value = 0; value < stop; value += step) points.push([value, stop]); for (var value = stop - step; value >= 0; value -= step) points.push([stop, value]); for (var value = stop - step; value >= 0; value -= step) points.push([value, 0]); test.equal(polygon.polygonArea(points), 1e16 - 5e7); test.end(); }); d3-polygon-1.0.3/test/centroid-test.js000066400000000000000000000032611306056431400176130ustar00rootroot00000000000000var tape = require("tape"), polygon = require("../"); tape("polygonCentroid(points) returns the expected value for closed counterclockwise polygons", function(test) { test.deepEqual(polygon.polygonCentroid([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), [.5, .5]); test.end(); }); tape("polygonCentroid(points) returns the expected value for closed clockwise polygons", function(test) { test.deepEqual(polygon.polygonCentroid([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), [.5, .5]); test.deepEqual(polygon.polygonCentroid([[1, 1], [3, 2], [2, 3], [1, 1]]), [2, 2]); test.end(); }); tape("polygonCentroid(points) returns the expected value for open counterclockwise polygons", function(test) { test.deepEqual(polygon.polygonCentroid([[0, 0], [0, 1], [1, 1], [1, 0]]), [.5, .5]); test.end(); }); tape("polygonCentroid(points) returns the expected value for closed counterclockwise polygons", function(test) { test.deepEqual(polygon.polygonCentroid([[0, 0], [1, 0], [1, 1], [0, 1]]), [.5, .5]); test.deepEqual(polygon.polygonCentroid([[1, 1], [3, 2], [2, 3]]), [2, 2]); test.end(); }); tape("polygonCentroid(polygon) returns the expected value for a very large polygon", function(test) { var start = 0, stop = 1e8, step = 1e4, points = []; for (var value = 0; value < stop; value += step) points.push([0, value]); for (var value = 0; value < stop; value += step) points.push([value, stop]); for (var value = stop - step; value >= 0; value -= step) points.push([stop, value]); for (var value = stop - step; value >= 0; value -= step) points.push([value, 0]); test.deepEqual(polygon.polygonCentroid(points), [49999999.75000187, 49999999.75001216]); test.end(); }); d3-polygon-1.0.3/test/contains-test.js000066400000000000000000000030241306056431400176170ustar00rootroot00000000000000var tape = require("tape"), polygon = require("../"); tape("polygonContains(polygon, point) returns the expected value for closed counterclockwise polygons", function(test) { test.equal(polygon.polygonContains([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]], [0.5, 0.5]), true); test.equal(polygon.polygonContains([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]], [1.5, 0.5]), false); test.equal(polygon.polygonContains([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]], [-0.5, 0.5]), false); test.equal(polygon.polygonContains([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]], [0.5, 1.5]), false); test.equal(polygon.polygonContains([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]], [0.5, -0.5]), false); test.end(); }); tape("polygonContains(polygon, point) returns the expected value for closed clockwise polygons", function(test) { test.equal(polygon.polygonContains([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]], [0.5, 0.5]), true); test.equal(polygon.polygonContains([[1, 1], [3, 2], [2, 3], [1, 1]], [1.5, 1.5]), true); test.end(); }); tape("polygonContains(polygon, point) returns the expected value for open counterclockwise polygons", function(test) { test.equal(polygon.polygonContains([[0, 0], [0, 1], [1, 1], [1, 0]], [0.5, 0.5]), true); test.end(); }); tape("polygonContains(polygon, point) returns the expected value for open clockwise polygons", function(test) { test.equal(polygon.polygonContains([[0, 0], [1, 0], [1, 1], [0, 1]], [0.5, 0.5]), true); test.equal(polygon.polygonContains([[1, 1], [3, 2], [2, 3]], [1.5, 1.5]), true); test.end(); }); d3-polygon-1.0.3/test/hull-test.js000066400000000000000000000037111306056431400167500ustar00rootroot00000000000000var tape = require("tape"), polygon = require("../"); tape("polygonHull(points) returns null if points has fewer than three elements", function(test) { test.equal(polygon.polygonHull([]), null); test.equal(polygon.polygonHull([[0, 1]]), null); test.equal(polygon.polygonHull([[0, 1], [1, 0]]), null); test.end(); }); tape("polygonHull(points) returns the convex hull of the specified points", function(test) { test.deepEqual(polygon.polygonHull([[200, 200], [760, 300], [500, 500]]), [[760, 300], [200, 200], [500, 500]]); test.deepEqual(polygon.polygonHull([[200, 200], [760, 300], [500, 500], [400, 400]]), [[760, 300], [200, 200], [500, 500]]); test.end(); }); tape("polygonHull(points) handles points with duplicate ordinates", function(test) { test.deepEqual(polygon.polygonHull([[-10, -10], [10, 10], [10, -10], [-10, 10]]), [[10, 10], [10, -10], [-10, -10], [-10, 10]]); test.end(); }); tape("polygonHull(points) handles overlapping upper and lower hulls", function(test) { test.deepEqual(polygon.polygonHull([[0, -10], [0, 10], [0, 0], [10, 0], [-10, 0]]), [[10, 0], [0, -10], [-10, 0], [0, 10]]); test.end(); }); // Cases below taken from http://uva.onlinejudge.org/external/6/681.html tape("polygonHull(points) handles various non-trivial hulls", function(test) { test.deepEqual(polygon.polygonHull([[60, 20], [250, 140], [180, 170], [79, 140], [50, 60], [60, 20]]), [[250, 140], [60, 20], [50, 60], [79, 140], [180, 170]]); test.deepEqual(polygon.polygonHull([[50, 60], [60, 20], [70, 45], [100, 70], [125, 90], [200, 113], [250, 140], [180, 170], [105, 140], [79, 140], [60, 85], [50, 60]]), [[250, 140], [60, 20], [50, 60], [79, 140], [180, 170]]); test.deepEqual(polygon.polygonHull([[30, 30], [50, 60], [60, 20], [70, 45], [86, 39], [112, 60], [200, 113], [250, 50], [300, 200], [130, 240], [76, 150], [47, 76], [36, 40], [33, 35], [30, 30]]), [[300, 200], [250, 50], [60, 20], [30, 30], [47, 76], [76, 150], [130, 240]]); test.end(); }); d3-polygon-1.0.3/test/length-test.js000066400000000000000000000020561306056431400172660ustar00rootroot00000000000000var tape = require("tape"), polygon = require("../"); tape("polygonLength(polygon) returns the expected value for closed counterclockwise polygons", function(test) { test.equal(polygon.polygonLength([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), 4); test.end(); }); tape("polygonLength(polygon) returns the expected value for closed clockwise polygons", function(test) { test.equal(polygon.polygonLength([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), 4); test.equal(polygon.polygonLength([[1, 1], [3, 2], [2, 3], [1, 1]]), Math.sqrt(20) + Math.sqrt(2)); test.end(); }); tape("polygonLength(polygon) returns the expected value for open counterclockwise polygons", function(test) { test.equal(polygon.polygonLength([[0, 0], [0, 1], [1, 1], [1, 0]]), 4); test.end(); }); tape("polygonLength(polygon) returns the expected value for open clockwise polygons", function(test) { test.equal(polygon.polygonLength([[0, 0], [1, 0], [1, 1], [0, 1]]), 4); test.equal(polygon.polygonLength([[1, 1], [3, 2], [2, 3]]), Math.sqrt(20) + Math.sqrt(2)); test.end(); });