polygon-points
Advanced tools
Comparing version 0.5.1 to 0.6.0
123
index.js
@@ -5,2 +5,6 @@ 'use strict'; | ||
/** | ||
* | ||
* @param {Array} vertexes - Array of objects containing x and y value. | ||
*/ | ||
constructor(vertexes) { | ||
@@ -10,2 +14,8 @@ this._checkVertexes(vertexes); | ||
/** | ||
* | ||
* Get the total number of x y points that exist in polygon. | ||
* @readonly | ||
* @returns {Number} | ||
*/ | ||
get pointsLength() { | ||
@@ -15,6 +25,56 @@ return this._pointsLength || this._countPointsInPolygon(); | ||
/** | ||
* | ||
* Get the bounding box as an array of x y coordinates. | ||
* @readonly | ||
* @returns {Array} | ||
*/ | ||
get boundingBox() { | ||
return [{x: this._minX, y: this._minY}, {x :this._minX, y: this._maxY}, {x: this._maxX, y: this._maxY}, {x: this._maxX, y: this._minY}]; | ||
return [{x: this.minX, y: this.minY}, {x: this.maxX, y: this.minY}, {x: this.maxX, y: this.maxY}, {x :this.minX, y: this.maxY}]; | ||
} | ||
/** | ||
* | ||
* Get the minimum x value. | ||
* @readonly | ||
* @returns {Number} | ||
*/ | ||
get minX() { | ||
return this._minX || 0; | ||
} | ||
/** | ||
* | ||
* Get the maximum x value. | ||
* @readonly | ||
* @returns {Number} | ||
*/ | ||
get maxX() { | ||
return this._maxX || 0; | ||
} | ||
/** | ||
* | ||
* Get the minimum y value. | ||
* @readonly | ||
* @returns {Number} | ||
*/ | ||
get minY() { | ||
return this._minY || 0; | ||
} | ||
/** | ||
* | ||
* Get the maximum y value. | ||
* @readonly | ||
* @returns {Number} | ||
*/ | ||
get maxY() { | ||
return this._maxY || 0; | ||
} | ||
/** | ||
* @ignore | ||
* @param {Array} value - Array of objects containing x and y value. | ||
*/ | ||
set vertexes(value) { | ||
@@ -25,6 +85,16 @@ this._checkVertexes(value); | ||
/** | ||
* | ||
* Get or set the array of vertexes. | ||
* @returns {Array} | ||
*/ | ||
get vertexes() { | ||
return this._vertexes; | ||
return this._vertexes || []; | ||
} | ||
/** | ||
* | ||
* @param {Array} vertexes - Array of objects containing x and y value. | ||
* @private | ||
*/ | ||
_checkVertexes(vertexes) { | ||
@@ -55,6 +125,11 @@ if (!Array.isArray(vertexes) || vertexes.length < 3) { | ||
/** | ||
* | ||
* @returns {Number} | ||
* @private | ||
*/ | ||
_countPointsInPolygon() { | ||
this._pointsLength = 0; | ||
for (let y = this._minY; y < this._maxY; y++) { | ||
for (let x = this._minX; x < this._maxX; x++) { | ||
for (let y = this._minY; y <= this._maxY; y++) { | ||
for (let x = this._minX; x <= this._maxX; x++) { | ||
if (this.containsPoint(x, y) === true) { | ||
@@ -68,2 +143,9 @@ this._pointsLength++; | ||
/** | ||
* | ||
* Check if x y point is contained in polygon. | ||
* @param {Number} x - x coordinate | ||
* @param {Number} y - y coordinate | ||
* @returns {Boolean} | ||
*/ | ||
containsPoint(x, y) { | ||
@@ -82,2 +164,10 @@ //algorithm based on http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html | ||
const yj = array[j].y; | ||
if ((xi === x && yi === y) || (xj === x && yj === y)) { | ||
// point is on corner | ||
return true; | ||
} | ||
if (Math.hypot(xj - xi, yj - yi) === Math.hypot(xj - x, yj - y) + Math.hypot(xi - x, yi - y)) { | ||
// point is on perimeter | ||
return true; | ||
} | ||
const intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); | ||
@@ -91,2 +181,9 @@ if (intersect) { | ||
/** | ||
* | ||
* Get a Buffer of 0's and 1's to indicate if point's index is in polygon. | ||
* @param {Number} width - width of coordinates | ||
* @param {Number} height - height of coordinates | ||
* @returns {{buffer: Buffer, count: number, length: number}} | ||
*/ | ||
getBitset(width, height) { | ||
@@ -96,6 +193,14 @@ const length = width * height; | ||
let count = 0; | ||
for (let y = 0, i = 0; y < width; y++) { | ||
let minX = width; | ||
let maxX = 0; | ||
let minY = height; | ||
let maxY = 0; | ||
for (let y = 0, i = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++, i++) { | ||
if (this.containsPoint(x, y) === true) { | ||
buffer[i] = true; | ||
minX = Math.min(minX, x); | ||
maxX = Math.max(maxX, x); | ||
minY = Math.min(minY, y); | ||
maxY = Math.max(maxY, y); | ||
buffer[i] = 1; | ||
count++; | ||
@@ -105,6 +210,10 @@ } | ||
} | ||
return {buffer: buffer, count: count, length: length}; | ||
return {buffer: buffer, count: count, length: length, minX: minX, maxX: maxX, minY: minY, maxY: maxY}; | ||
} | ||
} | ||
/** | ||
* | ||
* @type {PolygonPoints} | ||
*/ | ||
module.exports = PolygonPoints; |
{ | ||
"name": "polygon-points", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Determine if an x y coordinate exists in a polygon.", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"test": "node tests/test.js", | ||
"preversion": "npm test" | ||
"preversion": "npm test", | ||
"doc": "jsdoc index.js -d docs && git commit -m \"update docs\" -- docs" | ||
}, | ||
@@ -28,3 +29,6 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/kevinGodell/polygon-points#readme" | ||
"homepage": "https://github.com/kevinGodell/polygon-points#readme", | ||
"devDependencies": { | ||
"jsdoc": "^3.5.5" | ||
} | ||
} |
# polygon-points | ||
###### [![Build Status](https://travis-ci.org/kevinGodell/polygon-points.svg?branch=master)](https://travis-ci.org/kevinGodell/polygon-points) [![Build status](https://ci.appveyor.com/api/projects/status/al11is88xms9kqq9/branch/master?svg=true)](https://ci.appveyor.com/project/kevinGodell/polygon-points/branch/master) [![GitHub issues](https://img.shields.io/github/issues/kevinGodell/polygon-points.svg)](https://github.com/kevinGodell/polygon-points/issues) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/kevinGodell/polygon-points/master/LICENSE) | ||
###### [![Build Status](https://travis-ci.org/kevinGodell/polygon-points.svg?branch=master)](https://travis-ci.org/kevinGodell/polygon-points) [![Build status](https://ci.appveyor.com/api/projects/status/al11is88xms9kqq9/branch/master?svg=true)](https://ci.appveyor.com/project/kevinGodell/polygon-points/branch/master) [![GitHub issues](https://img.shields.io/github/issues/kevinGodell/polygon-points.svg)](https://github.com/kevinGodell/polygon-points/issues) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/kevinGodell/polygon-points/master/LICENSE) [![Greenkeeper badge](https://badges.greenkeeper.io/kevinGodell/polygon-points.svg)](https://greenkeeper.io/) | ||
Determine if an x y coordinate exists in a polygon. This is being used in a motion detection project where specific regions of an image are being filtered and measured. It works fast when iterating pixels because it caches the bounding box value and uses it to quickly eliminate non-targeted pixels from unnecessary difference calculations. It is designed to be used with positive integer values starting with an x y value of 0 0 at the top left. | ||
@@ -9,3 +9,3 @@ ### installation: | ||
### usage: | ||
``` | ||
```javascript | ||
const PP = require('polygon-points'); | ||
@@ -28,2 +28,5 @@ | ||
polygonPoints.vertexes = [{x: 0, y: 0}, {x: 0, y: 100}, {x: 100, y: 100}, {x: 100, y: 0}]; | ||
``` | ||
//get minX, maxX, minY, and maxY unsigned integers of bounding box | ||
polygonPoints.minX, polygonPoints.maxX, polygonPoints.minY, polygonPoints.maxY; | ||
``` |
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
10161
5
193
31
1