d3-polygon
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-polygon/ Version 1.0.1. Copyright 2016 Mike Bostock. | ||
// https://d3js.org/d3-polygon/ Version 1.0.2. Copyright 2016 Mike Bostock. | ||
(function (global, factory) { | ||
@@ -6,146 +6,146 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
(factory((global.d3 = global.d3 || {}))); | ||
}(this, function (exports) { 'use strict'; | ||
}(this, (function (exports) { 'use strict'; | ||
function area(polygon) { | ||
var i = -1, | ||
n = polygon.length, | ||
a, | ||
b = polygon[n - 1], | ||
area = 0; | ||
var area = 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; | ||
while (++i < n) { | ||
a = b; | ||
b = polygon[i]; | ||
area += a[1] * b[0] - a[0] * b[1]; | ||
} | ||
function centroid(polygon) { | ||
var i = -1, | ||
n = polygon.length, | ||
x = 0, | ||
y = 0, | ||
a, | ||
b = polygon[n - 1], | ||
c, | ||
k = 0; | ||
return area / 2; | ||
}; | ||
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; | ||
} | ||
var centroid = function(polygon) { | ||
var i = -1, | ||
n = polygon.length, | ||
x = 0, | ||
y = 0, | ||
a, | ||
b = polygon[n - 1], | ||
c, | ||
k = 0; | ||
return k *= 3, [x / k, y / k]; | ||
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; | ||
} | ||
// 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. | ||
function cross(a, b, c) { | ||
return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); | ||
} | ||
return k *= 3, [x / k, y / k]; | ||
}; | ||
function lexicographicOrder(a, b) { | ||
return a[0] - b[0] || a[1] - b[1]; | ||
} | ||
// 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. | ||
var cross = function(a, b, c) { | ||
return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); | ||
}; | ||
// 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; | ||
function lexicographicOrder(a, b) { | ||
return a[0] - b[0] || a[1] - b[1]; | ||
} | ||
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; | ||
} | ||
// 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; | ||
return indexes.slice(0, size); // remove popped points | ||
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; | ||
} | ||
function hull(points) { | ||
if ((n = points.length) < 3) return null; | ||
return indexes.slice(0, size); // remove popped points | ||
} | ||
var i, | ||
n, | ||
sortedPoints = new Array(n), | ||
flippedPoints = new Array(n); | ||
var hull = function(points) { | ||
if ((n = points.length) < 3) return null; | ||
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 i, | ||
n, | ||
sortedPoints = new Array(n), | ||
flippedPoints = new Array(n); | ||
var upperIndexes = computeUpperHullIndexes(sortedPoints), | ||
lowerIndexes = computeUpperHullIndexes(flippedPoints); | ||
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]]; | ||
// Construct the hull polygon, removing possible duplicate endpoints. | ||
var skipLeft = lowerIndexes[0] === upperIndexes[0], | ||
skipRight = lowerIndexes[lowerIndexes.length - 1] === upperIndexes[upperIndexes.length - 1], | ||
hull = []; | ||
var upperIndexes = computeUpperHullIndexes(sortedPoints), | ||
lowerIndexes = computeUpperHullIndexes(flippedPoints); | ||
// 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]]); | ||
// Construct the hull polygon, removing possible duplicate endpoints. | ||
var skipLeft = lowerIndexes[0] === upperIndexes[0], | ||
skipRight = lowerIndexes[lowerIndexes.length - 1] === upperIndexes[upperIndexes.length - 1], | ||
hull = []; | ||
return 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]]); | ||
function contains(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; | ||
return hull; | ||
}; | ||
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; | ||
} | ||
var contains = 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; | ||
return inside; | ||
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; | ||
} | ||
function length(polygon) { | ||
var i = -1, | ||
n = polygon.length, | ||
b = polygon[n - 1], | ||
xa, | ||
ya, | ||
xb = b[0], | ||
yb = b[1], | ||
perimeter = 0; | ||
return inside; | ||
}; | ||
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); | ||
} | ||
var length = function(polygon) { | ||
var i = -1, | ||
n = polygon.length, | ||
b = polygon[n - 1], | ||
xa, | ||
ya, | ||
xb = b[0], | ||
yb = b[1], | ||
perimeter = 0; | ||
return perimeter; | ||
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); | ||
} | ||
exports.polygonArea = area; | ||
exports.polygonCentroid = centroid; | ||
exports.polygonHull = hull; | ||
exports.polygonContains = contains; | ||
exports.polygonLength = length; | ||
return perimeter; | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.polygonArea = area; | ||
exports.polygonCentroid = centroid; | ||
exports.polygonHull = hull; | ||
exports.polygonContains = contains; | ||
exports.polygonLength = length; | ||
})); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-polygon/ Version 1.0.1. Copyright 2016 Mike Bostock. | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(n.d3=n.d3||{})}(this,function(n){"use strict";function e(n){for(var e,r=-1,t=n.length,o=n[t-1],f=0;++r<t;)e=o,o=n[r],f+=e[1]*o[0]-e[0]*o[1];return f/2}function r(n){for(var e,r,t=-1,o=n.length,f=0,u=0,l=n[o-1],i=0;++t<o;)e=l,l=n[t],i+=r=e[0]*l[1]-l[0]*e[1],f+=(e[0]+l[0])*r,u+=(e[1]+l[1])*r;return i*=3,[f/i,u/i]}function t(n,e,r){return(e[0]-n[0])*(r[1]-n[1])-(e[1]-n[1])*(r[0]-n[0])}function o(n,e){return n[0]-e[0]||n[1]-e[1]}function f(n){for(var e=n.length,r=[0,1],o=2,f=2;f<e;++f){for(;o>1&&t(n[r[o-2]],n[r[o-1]],n[f])<=0;)--o;r[o++]=f}return r.slice(0,o)}function u(n){if((r=n.length)<3)return null;var e,r,t=new Array(r),u=new Array(r);for(e=0;e<r;++e)t[e]=[+n[e][0],+n[e][1],e];for(t.sort(o),e=0;e<r;++e)u[e]=[t[e][0],-t[e][1]];var l=f(t),i=f(u),g=i[0]===l[0],c=i[i.length-1]===l[l.length-1],h=[];for(e=l.length-1;e>=0;--e)h.push(n[t[l[e]][2]]);for(e=+g;e<i.length-c;++e)h.push(n[t[i[e]][2]]);return h}function l(n,e){for(var r,t,o=n.length,f=n[o-1],u=e[0],l=e[1],i=f[0],g=f[1],c=!1,h=0;h<o;++h)f=n[h],r=f[0],t=f[1],t>l!=g>l&&u<(i-r)*(l-t)/(g-t)+r&&(c=!c),i=r,g=t;return c}function i(n){for(var e,r,t=-1,o=n.length,f=n[o-1],u=f[0],l=f[1],i=0;++t<o;)e=u,r=l,f=n[t],u=f[0],l=f[1],e-=u,r-=l,i+=Math.sqrt(e*e+r*r);return i}n.polygonArea=e,n.polygonCentroid=r,n.polygonHull=u,n.polygonContains=l,n.polygonLength=i,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
// https://d3js.org/d3-polygon/ Version 1.0.2. Copyright 2016 Mike Bostock. | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(n.d3=n.d3||{})}(this,function(n){"use strict";function r(n,r){return n[0]-r[0]||n[1]-r[1]}function e(n){for(var r=n.length,e=[0,1],t=2,o=2;o<r;++o){for(;t>1&&f(n[e[t-2]],n[e[t-1]],n[o])<=0;)--t;e[t++]=o}return e.slice(0,t)}var t=function(n){for(var r,e=-1,t=n.length,o=n[t-1],f=0;++e<t;)r=o,o=n[e],f+=r[1]*o[0]-r[0]*o[1];return f/2},o=function(n){for(var r,e,t=-1,o=n.length,f=0,u=0,l=n[o-1],i=0;++t<o;)r=l,l=n[t],i+=e=r[0]*l[1]-l[0]*r[1],f+=(r[0]+l[0])*e,u+=(r[1]+l[1])*e;return i*=3,[f/i,u/i]},f=function(n,r,e){return(r[0]-n[0])*(e[1]-n[1])-(r[1]-n[1])*(e[0]-n[0])},u=function(n){if((o=n.length)<3)return null;var t,o,f=new Array(o),u=new Array(o);for(t=0;t<o;++t)f[t]=[+n[t][0],+n[t][1],t];for(f.sort(r),t=0;t<o;++t)u[t]=[f[t][0],-f[t][1]];var l=e(f),i=e(u),g=i[0]===l[0],a=i[i.length-1]===l[l.length-1],c=[];for(t=l.length-1;t>=0;--t)c.push(n[f[l[t]][2]]);for(t=+g;t<i.length-a;++t)c.push(n[f[i[t]][2]]);return c},l=function(n,r){for(var e,t,o=n.length,f=n[o-1],u=r[0],l=r[1],i=f[0],g=f[1],a=!1,c=0;c<o;++c)f=n[c],e=f[0],t=f[1],t>l!=g>l&&u<(i-e)*(l-t)/(g-t)+e&&(a=!a),i=e,g=t;return a},i=function(n){for(var r,e,t=-1,o=n.length,f=n[o-1],u=f[0],l=f[1],i=0;++t<o;)r=u,e=l,f=n[t],u=f[0],l=f[1],r-=u,e-=l,i+=Math.sqrt(r*r+e*e);return i};n.polygonArea=t,n.polygonCentroid=o,n.polygonHull=u,n.polygonContains=l,n.polygonLength=i,Object.defineProperty(n,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-polygon", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Operations for two-dimensional polygons.", | ||
@@ -33,5 +33,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"eslint": "2", | ||
"eslint": "3", | ||
"package-preamble": "0.0", | ||
"rollup": "0.34", | ||
"rollup": "0.36", | ||
"tape": "4", | ||
@@ -38,0 +38,0 @@ "uglify-js": "2" |
@@ -22,11 +22,11 @@ # d3-polygon | ||
<a href="#polygonArea" name="polygonArea">#</a> d3.<b>polygonArea</b>(<i>polygon</i>) | ||
<a href="#polygonArea" name="polygonArea">#</a> d3.<b>polygonArea</b>(<i>polygon</i>) [<>](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. | ||
<a href="#polygonCentroid" name="polygonCentroid">#</a> d3.<b>polygonCentroid</b>(<i>polygon</i>) | ||
<a href="#polygonCentroid" name="polygonCentroid">#</a> d3.<b>polygonCentroid</b>(<i>polygon</i>) [<>](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*. | ||
<a href="#polygonHull" name="polygonHull">#</a> d3.<b>polygonHull</b>(<i>points</i>) | ||
<a href="#polygonHull" name="polygonHull">#</a> d3.<b>polygonHull</b>(<i>points</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/hull.js#L23 "Source Code") | ||
@@ -37,8 +37,8 @@ <a href="http://bl.ocks.org/mbostock/6f14f7b7f267a85f7cdc"><img src="https://raw.githubusercontent.com/d3/d3-polygon/master/img/hull.png" width="250" height="250"></a> | ||
<a href="#polygonContains" name="polygonContains">#</a> d3.<b>polygonContains</b>(<i>polygon</i>, <i>point</i>) | ||
<a href="#polygonContains" name="polygonContains">#</a> d3.<b>polygonContains</b>(<i>polygon</i>, <i>point</i>) [<>](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). | ||
<a href="#polygonLength" name="polygonLength">#</a> d3.<b>polygonLength</b>(<i>polygon</i>) | ||
<a href="#polygonLength" name="polygonLength">#</a> d3.<b>polygonLength</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/length.js#L1 "Source Code") | ||
Returns the length of the perimeter of the specified *polygon*. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26919
240