| [e4c61dd] | 1 | # d3-polygon
|
|---|
| 2 |
|
|---|
| 3 | This module provides a few basic geometric operations for two-dimensional polygons. Each polygon is represented as an array of two-element arrays [[<i>x1</i>, <i>y1</i>], [<i>x2</i>, <i>y2</i>], …], 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.
|
|---|
| 4 |
|
|---|
| 5 | ## Installing
|
|---|
| 6 |
|
|---|
| 7 | If you use npm, `npm install d3-polygon`. You can also download the [latest release on GitHub](https://github.com/d3/d3-polygon/releases/latest). For vanilla HTML in modern browsers, import d3-polygon from Skypack:
|
|---|
| 8 |
|
|---|
| 9 | ```html
|
|---|
| 10 | <script type="module">
|
|---|
| 11 |
|
|---|
| 12 | import {polygonHull} from "https://cdn.skypack.dev/d3-polygon@3";
|
|---|
| 13 |
|
|---|
| 14 | const hull = polygonHull(points);
|
|---|
| 15 |
|
|---|
| 16 | </script>
|
|---|
| 17 | ```
|
|---|
| 18 |
|
|---|
| 19 | For legacy environments, you can load d3-polygon’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:
|
|---|
| 20 |
|
|---|
| 21 | ```html
|
|---|
| 22 | <script src="https://cdn.jsdelivr.net/npm/d3-polygon@3"></script>
|
|---|
| 23 | <script>
|
|---|
| 24 |
|
|---|
| 25 | const hull = d3.polygonHull(points);
|
|---|
| 26 |
|
|---|
| 27 | </script>
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | ## API Reference
|
|---|
| 31 |
|
|---|
| 32 | <a href="#polygonArea" name="polygonArea">#</a> d3.<b>polygonArea</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/area.js "Source Code")
|
|---|
| 33 |
|
|---|
| 34 | 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.
|
|---|
| 35 |
|
|---|
| 36 | <a href="#polygonCentroid" name="polygonCentroid">#</a> d3.<b>polygonCentroid</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/centroid.js "Source Code")
|
|---|
| 37 |
|
|---|
| 38 | Returns the [centroid](https://en.wikipedia.org/wiki/Centroid) of the specified *polygon*.
|
|---|
| 39 |
|
|---|
| 40 | <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")
|
|---|
| 41 |
|
|---|
| 42 | <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>
|
|---|
| 43 |
|
|---|
| 44 | 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.
|
|---|
| 45 |
|
|---|
| 46 | <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 "Source Code")
|
|---|
| 47 |
|
|---|
| 48 | Returns true if and only if the specified *point* is inside the specified *polygon*.
|
|---|
| 49 |
|
|---|
| 50 | <a href="#polygonLength" name="polygonLength">#</a> d3.<b>polygonLength</b>(<i>polygon</i>) [<>](https://github.com/d3/d3-polygon/blob/master/src/length.js "Source Code")
|
|---|
| 51 |
|
|---|
| 52 | Returns the length of the perimeter of the specified *polygon*.
|
|---|