https://github.com/danasilver/polygon
A standalone library for working with polygons.
https://github.com/danasilver/polygon
Last synced: 8 months ago
JSON representation
A standalone library for working with polygons.
- Host: GitHub
- URL: https://github.com/danasilver/polygon
- Owner: danasilver
- License: mit
- Created: 2014-06-21T01:59:28.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-06-26T04:34:07.000Z (almost 12 years ago)
- Last Synced: 2025-02-24T02:31:16.560Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 145 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## polygon
A standalone library for working with polygons.
Runtimes are given in terms of the number of points.
### Testing
Install the dev dependencies:
```sh
$ npm install
```
And run the tests:
```sh
$ npm test
```
### Construction
```js
var p = polygon();
```
### Points
Call `points` once or multiple times to add points.
```js
p.points([[0, 0], [1, 0], [1, 1], [1, 2], [0, 1]]);
```
Call `points` with no arguments to get the points.
```js
p.points();
//=> [[0, 0], [1, 0], [1, 1], [1, 2], [0, 1]]
```
### Clear
```js
p.clear();
```
### Area
Calculated as the sum of the determinants of consecutive vertices as described
on [Wolfram MathWorld](http://mathworld.wolfram.com/PolygonArea.html).
Area is signed and defined for a convex polygon to be positive if the points are
arranged in counterclockwise order and negative if they are in clockwise order.
O(n)
```js
p.area();
//=> 1.5
```
### Perimeter
Calculated as the sum of the
[Euclidean distances](http://en.wikipedia.org/wiki/Euclidean_distance) between
consecutive vertices.
O(n)
```js
p.perimeter();
//=> 5.414213562373095
```
### Centroid


[http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon](http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon)
O(n)
```js
p.centroid();
//=> [0.5555555555555556, 0.7777777777777777]
```
### Point in Polygon
See http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html.
### Convex
### Simple
### Intersecting
### Triangulation
### Minimum Weight Triangulation