polygon-points
Advanced tools
Comparing version 0.1.7 to 0.2.0
38
index.js
@@ -13,4 +13,6 @@ // jshint esversion: 6, globalstrict: true, strict: true | ||
for (const vertex of vertexes) { | ||
if (!Array.isArray(vertex) || vertex.length !== 2 || vertex[0] < 0 || vertex[1] < 0) { | ||
throw new Error('Each vertex of the polygon must consist of an array of 2 unsigned integers.'); | ||
const x = vertex.x; | ||
const y = vertex.y; | ||
if (x < 0 || y < 0 || parseInt(x) !== x || parseInt(y) !== y) { | ||
throw new Error('Each vertex of the polygon must consist of an object with an x and y unsigned integer.'); | ||
} | ||
@@ -25,13 +27,13 @@ } | ||
PolygonPoints.prototype._calculateBoundingBox = function () { | ||
this._maxX = this._vertexes[0][0]; | ||
this._minX = this._vertexes[0][0]; | ||
this._maxY = this._vertexes[0][1]; | ||
this._minY = this._vertexes[0][1]; | ||
this._maxX = this._vertexes[0].x; | ||
this._minX = this._vertexes[0].x; | ||
this._maxY = this._vertexes[0].y; | ||
this._minY = this._vertexes[0].y; | ||
for (let i = 1; i < this._vertexesLength; i++) { | ||
this._maxX = Math.max(this._maxX, this._vertexes[i][0]); | ||
this._minX = Math.min(this._minX, this._vertexes[i][0]); | ||
this._maxY = Math.max(this._maxY, this._vertexes[i][1]); | ||
this._minY = Math.min(this._minY, this._vertexes[i][1]); | ||
this._maxX = Math.max(this._maxX, this._vertexes[i].x); | ||
this._minX = Math.min(this._minX, this._vertexes[i].x); | ||
this._maxY = Math.max(this._maxY, this._vertexes[i].y); | ||
this._minY = Math.min(this._minY, this._vertexes[i].y); | ||
} | ||
this._boundingBox = [[this._minX, this._minY], [this._minX, this._maxY], [this._maxX, this._maxY], [this._maxX, this._minY]]; | ||
this._boundingBox = [{x: this._minX, y: this._minY}, {x :this._minX, y: this._maxY}, {x: this._maxX, y: this._maxY}, {x: this._maxX, y: this._minY}]; | ||
}; | ||
@@ -43,3 +45,3 @@ | ||
for (let x = this._minX; x < this._maxX; x++) { | ||
if (this.containsPoint([x, y]) === true) { | ||
if (this.containsPoint({x: x, y: y}) === true) { | ||
counter++; | ||
@@ -62,4 +64,4 @@ } | ||
//algorithm based on http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html | ||
const x = point[0]; | ||
const y = point[1]; | ||
const x = point.x; | ||
const y = point.y; | ||
if (x < this._minX || x > this._maxX || y < this._minY || y > this._maxY) { | ||
@@ -72,9 +74,9 @@ return false; | ||
for (let i = 0, j = length - 1; i < length; j = i++) { | ||
const xi = array[i][0]; | ||
const yi = array[i][1]; | ||
const xi = array[i].x; | ||
const yi = array[i].y; | ||
if (x === xi && y === yi) { | ||
return true; | ||
} | ||
const xj = array[j][0]; | ||
const yj = array[j][1]; | ||
const xj = array[j].x; | ||
const yj = array[j].y; | ||
const intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); | ||
@@ -81,0 +83,0 @@ if (intersect) { |
{ | ||
"name": "polygon-points", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "Determine if an x y coordinate exists in a polygon.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,3 +12,3 @@ # polygon-points | ||
const polygonPoints = new PP([[0, 0], [0, 100], [100, 100], [100, 0]]); | ||
const polygonPoints = new PP([{x: 0, y: 0}, {x: 0, y: 100}, {x: 100, y: 100}, {x: 100, y: 0}]); | ||
@@ -18,3 +18,3 @@ //public methods | ||
//check if a point exists in the polygon | ||
polygonPoints.containsPoint([1, 1]);//returns true | ||
polygonPoints.containsPoint({x: 1, y: 1});//returns true | ||
@@ -21,0 +21,0 @@ //get bounding box of polygon |
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
6067
77