Skip to content

Commit

Permalink
Export symbols with “polygon” prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 11, 2016
1 parent 7847f78 commit 47af8c9
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 103 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ This module provides a few basic geometric operations for two-dimensional polygo
If you use NPM, `npm install d3-polygon`. Otherwise, download the [latest release](https://github.com/d3/d3-polygon/releases/latest). The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom build using [Rollup](https://github.com/rollup/rollup) or your preferred bundler. You can also load directly from [d3js.org](https://d3js.org):

```html
<script src="https://d3js.org/d3-polygon.v0.1.min.js"></script>
<script src="https://d3js.org/d3-polygon.v0.2.min.js"></script>
```

In a vanilla environment, a `d3_polygon` global is exported. [Try d3-polygon in your browser.](https://tonicdev.com/npm/d3-polygon)

## API Reference

<a href="#area" name="area">#</a> d3_polygon.<b>area</b>(<i>polygon</i>)
<a href="#polygonArea" name="polygonArea">#</a> d3.<b>polygonArea</b>(<i>polygon</i>)

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="#centroid" name="centroid">#</a> d3_polygon.<b>centroid</b>(<i>polygon</i>)
<a href="#polygonCentroid" name="polygonCentroid">#</a> d3.<b>polygonCentroid</b>(<i>polygon</i>)

Returns the [centroid](https://en.wikipedia.org/wiki/Centroid) of the specified *polygon*.

<a href="#hull" name="hull">#</a> d3_polygon.<b>hull</b>(<i>points</i>)
<a href="#polygonHull" name="polygonHull">#</a> d3.<b>polygonHull</b>(<i>points</i>)

<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>

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.

<a href="#inside" name="inside">#</a> d3_polygon.<b>inside</b>(<i>polygon</i>, <i>point</i>)
<a href="#polygonContains" name="polygonContains">#</a> d3.<b>polygonContains</b>(<i>polygon</i>, <i>point</i>)

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="#perimeter" name="perimeter">#</a> d3_polygon.<b>perimeter</b>(<i>polygon</i>)
<a href="#polygonLength" name="polygonLength">#</a> d3.<b>polygonLength</b>(<i>polygon</i>)

Returns the length of the perimeter of the specified *polygon*.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {default as area} from "./src/area";
export {default as centroid} from "./src/centroid";
export {default as hull} from "./src/hull";
export {default as inside} from "./src/inside";
export {default as perimeter} from "./src/perimeter";
export {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";
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "d3-polygon",
"version": "0.1.0",
"version": "0.2.0",
"description": "Operations for two-dimensional polygons.",
"keywords": [
"d3",
"polygon",
"hull",
"geometry",
"graphics"
],
"homepage": "https://github.com/d3/d3-polygon",
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 12 additions & 12 deletions test/area-test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var tape = require("tape"),
polygon = require("../");

tape("area(polygon) returns the expected value for closed counterclockwise polygons", function(test) {
test.equal(polygon.area([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), 1);
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("area(polygon) returns the expected value for closed clockwise polygons", function(test) {
test.equal(polygon.area([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), -1);
test.equal(polygon.area([[1, 1], [3, 2], [2, 3], [1, 1]]), -1.5);
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("area(polygon) returns the expected value for open counterclockwise polygons", function(test) {
test.equal(polygon.area([[0, 0], [0, 1], [1, 1], [1, 0]]), 1);
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("area(polygon) returns the expected value for open clockwise polygons", function(test) {
test.equal(polygon.area([[0, 0], [1, 0], [1, 1], [0, 1]]), -1);
test.equal(polygon.area([[1, 1], [3, 2], [2, 3]]), -1.5);
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("area(polygon) returns the expected value for a very large polygon", function(test) {
tape("polygonArea(polygon) returns the expected value for a very large polygon", function(test) {
var start = 0,
stop = 1e8,
step = 1e4,
Expand All @@ -32,6 +32,6 @@ tape("area(polygon) returns the expected value for a very large polygon", functi
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.area(points), 1e16 - 5e7);
test.equal(polygon.polygonArea(points), 1e16 - 5e7);
test.end();
});
24 changes: 12 additions & 12 deletions test/centroid-test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var tape = require("tape"),
polygon = require("../");

tape("centroid(points) returns the expected value for closed counterclockwise polygons", function(test) {
test.deepEqual(polygon.centroid([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]), [.5, .5]);
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("centroid(points) returns the expected value for closed clockwise polygons", function(test) {
test.deepEqual(polygon.centroid([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]), [.5, .5]);
test.deepEqual(polygon.centroid([[1, 1], [3, 2], [2, 3], [1, 1]]), [2, 2]);
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("centroid(points) returns the expected value for open counterclockwise polygons", function(test) {
test.deepEqual(polygon.centroid([[0, 0], [0, 1], [1, 1], [1, 0]]), [.5, .5]);
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("centroid(points) returns the expected value for closed counterclockwise polygons", function(test) {
test.deepEqual(polygon.centroid([[0, 0], [1, 0], [1, 1], [0, 1]]), [.5, .5]);
test.deepEqual(polygon.centroid([[1, 1], [3, 2], [2, 3]]), [2, 2]);
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("centroid(polygon) returns the expected value for a very large polygon", function(test) {
tape("polygonCentroid(polygon) returns the expected value for a very large polygon", function(test) {
var start = 0,
stop = 1e8,
step = 1e4,
Expand All @@ -32,6 +32,6 @@ tape("centroid(polygon) returns the expected value for a very large polygon", fu
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.centroid(points), [49999999.75000187, 49999999.75001216]);
test.deepEqual(polygon.polygonCentroid(points), [49999999.75000187, 49999999.75001216]);
test.end();
});
28 changes: 28 additions & 0 deletions test/contains-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var 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();
});
30 changes: 15 additions & 15 deletions test/hull-test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
var tape = require("tape"),
polygon = require("../");

tape("hull(points) returns null if points has fewer than three elements", function(test) {
test.equal(polygon.hull([]), null);
test.equal(polygon.hull([[0, 1]]), null);
test.equal(polygon.hull([[0, 1], [1, 0]]), null);
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("hull(points) returns the convex hull of the specified points", function(test) {
test.deepEqual(polygon.hull([[200, 200], [760, 300], [500, 500]]), [[760, 300], [200, 200], [500, 500]]);
test.deepEqual(polygon.hull([[200, 200], [760, 300], [500, 500], [400, 400]]), [[760, 300], [200, 200], [500, 500]]);
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("hull(points) handles points with duplicate ordinates", function(test) {
test.deepEqual(polygon.hull([[-10, -10], [10, 10], [10, -10], [-10, 10]]), [[10, 10], [10, -10], [-10, -10], [-10, 10]]);
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("hull(points) handles overlapping upper and lower hulls", function(test) {
test.deepEqual(polygon.hull([[0, -10], [0, 10], [0, 0], [10, 0], [-10, 0]]), [[10, 0], [0, -10], [-10, 0], [0, 10]]);
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("hull(points) handles various non-trivial hulls", function(test) {
test.deepEqual(polygon.hull([[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.hull([[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.hull([[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]]);
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();
});
28 changes: 0 additions & 28 deletions test/inside-test.js

This file was deleted.

24 changes: 24 additions & 0 deletions test/length-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var 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();
});
24 changes: 0 additions & 24 deletions test/perimeter-test.js

This file was deleted.

0 comments on commit 47af8c9

Please sign in to comment.