@math.gl/polygon
Advanced tools
Comparing version 3.3.2 to 3.4.0-alpha.1
@@ -20,2 +20,32 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "getPolygonSignedArea", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polygonUtils.getPolygonSignedArea; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getPolygonWindingDirection", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polygonUtils.getPolygonWindingDirection; | ||
} | ||
}); | ||
Object.defineProperty(exports, "forEachSegmentInPolygon", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polygonUtils.forEachSegmentInPolygon; | ||
} | ||
}); | ||
Object.defineProperty(exports, "modifyPolygonWindingDirection", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polygonUtils.modifyPolygonWindingDirection; | ||
} | ||
}); | ||
Object.defineProperty(exports, "WINDING", { | ||
enumerable: true, | ||
get: function get() { | ||
return _polygonUtils.WINDING; | ||
} | ||
}); | ||
Object.defineProperty(exports, "clipPolygon", { | ||
@@ -60,2 +90,4 @@ enumerable: true, | ||
var _polygonUtils = require("./polygon-utils"); | ||
var _lineclip = require("./lineclip"); | ||
@@ -62,0 +94,0 @@ |
@@ -16,7 +16,16 @@ "use strict"; | ||
var _polygonUtils = require("./polygon-utils"); | ||
var Polygon = function () { | ||
function Polygon(points) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
(0, _classCallCheck2["default"])(this, Polygon); | ||
this.points = points; | ||
this.isClosed = (0, _core.equals)(this.points[this.points.length - 1], this.points[0]); | ||
this.isFlatArray = !(0, _core.isArray)(points[0]); | ||
this.options = { | ||
start: options.start || 0, | ||
end: options.end || points.length, | ||
size: options.size || 2, | ||
isClosed: options.isClosed | ||
}; | ||
Object.freeze(this); | ||
@@ -28,7 +37,4 @@ } | ||
value: function getSignedArea() { | ||
var area = 0; | ||
this.forEachSegment(function (p1, p2) { | ||
area += (p1[0] + p2[0]) * (p1[1] - p2[1]); | ||
}); | ||
return area / 2; | ||
if (this.isFlatArray) return (0, _polygonUtils.getPolygonSignedArea)(this.points, this.options); | ||
return (0, _polygonUtils.getPolygonSignedAreaPoints)(this.points, this.options); | ||
} | ||
@@ -48,11 +54,18 @@ }, { | ||
value: function forEachSegment(visitor) { | ||
var length = this.points.length; | ||
for (var i = 0; i < length - 1; i++) { | ||
visitor(this.points[i], this.points[i + 1], i, i + 1); | ||
if (this.isFlatArray) { | ||
(0, _polygonUtils.forEachSegmentInPolygon)(this.points, function (x1, y1, x2, y2, i1, i2) { | ||
visitor([x1, y1], [x2, y2], i1, i2); | ||
}, this.options); | ||
} else { | ||
(0, _polygonUtils.forEachSegmentInPolygonPoints)(this.points, visitor, this.options); | ||
} | ||
} | ||
}, { | ||
key: "modifyWindingDirection", | ||
value: function modifyWindingDirection(direction) { | ||
if (this.isFlatArray) { | ||
return (0, _polygonUtils.modifyPolygonWindingDirection)(this.points, direction, this.options); | ||
} | ||
if (!this.isClosed) { | ||
visitor(this.points[length - 1], this.points[0], length - 1, 0); | ||
} | ||
return (0, _polygonUtils.modifyPolygonWindingDirectionPoints)(this.points, direction, this.options); | ||
} | ||
@@ -59,0 +72,0 @@ }]); |
export { default as Polygon } from './polygon'; | ||
export { getPolygonSignedArea, getPolygonWindingDirection, forEachSegmentInPolygon, modifyPolygonWindingDirection, WINDING } from './polygon-utils'; | ||
export { clipPolygon, clipPolyline } from './lineclip'; | ||
@@ -3,0 +4,0 @@ export { cutPolygonByGrid, cutPolylineByGrid } from './cut-by-grid'; |
@@ -1,6 +0,13 @@ | ||
import { equals } from '@math.gl/core'; | ||
import { isArray } from '@math.gl/core'; | ||
import { getPolygonSignedArea, forEachSegmentInPolygon, modifyPolygonWindingDirection, getPolygonSignedAreaPoints, forEachSegmentInPolygonPoints, modifyPolygonWindingDirectionPoints } from './polygon-utils'; | ||
export default class Polygon { | ||
constructor(points) { | ||
constructor(points, options = {}) { | ||
this.points = points; | ||
this.isClosed = equals(this.points[this.points.length - 1], this.points[0]); | ||
this.isFlatArray = !isArray(points[0]); | ||
this.options = { | ||
start: options.start || 0, | ||
end: options.end || points.length, | ||
size: options.size || 2, | ||
isClosed: options.isClosed | ||
}; | ||
Object.freeze(this); | ||
@@ -10,7 +17,4 @@ } | ||
getSignedArea() { | ||
let area = 0; | ||
this.forEachSegment((p1, p2) => { | ||
area += (p1[0] + p2[0]) * (p1[1] - p2[1]); | ||
}); | ||
return area / 2; | ||
if (this.isFlatArray) return getPolygonSignedArea(this.points, this.options); | ||
return getPolygonSignedAreaPoints(this.points, this.options); | ||
} | ||
@@ -27,11 +31,17 @@ | ||
forEachSegment(visitor) { | ||
const length = this.points.length; | ||
if (this.isFlatArray) { | ||
forEachSegmentInPolygon(this.points, (x1, y1, x2, y2, i1, i2) => { | ||
visitor([x1, y1], [x2, y2], i1, i2); | ||
}, this.options); | ||
} else { | ||
forEachSegmentInPolygonPoints(this.points, visitor, this.options); | ||
} | ||
} | ||
for (let i = 0; i < length - 1; i++) { | ||
visitor(this.points[i], this.points[i + 1], i, i + 1); | ||
modifyWindingDirection(direction) { | ||
if (this.isFlatArray) { | ||
return modifyPolygonWindingDirection(this.points, direction, this.options); | ||
} | ||
if (!this.isClosed) { | ||
visitor(this.points[length - 1], this.points[0], length - 1, 0); | ||
} | ||
return modifyPolygonWindingDirectionPoints(this.points, direction, this.options); | ||
} | ||
@@ -38,0 +48,0 @@ |
export { default as Polygon } from './polygon'; | ||
export { getPolygonSignedArea, getPolygonWindingDirection, forEachSegmentInPolygon, modifyPolygonWindingDirection, WINDING } from './polygon-utils'; | ||
export { clipPolygon, clipPolyline } from './lineclip'; | ||
@@ -3,0 +4,0 @@ export { cutPolygonByGrid, cutPolylineByGrid } from './cut-by-grid'; |
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
import { equals } from '@math.gl/core'; | ||
import { isArray } from '@math.gl/core'; | ||
import { getPolygonSignedArea, forEachSegmentInPolygon, modifyPolygonWindingDirection, getPolygonSignedAreaPoints, forEachSegmentInPolygonPoints, modifyPolygonWindingDirectionPoints } from './polygon-utils'; | ||
var Polygon = function () { | ||
function Polygon(points) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, Polygon); | ||
this.points = points; | ||
this.isClosed = equals(this.points[this.points.length - 1], this.points[0]); | ||
this.isFlatArray = !isArray(points[0]); | ||
this.options = { | ||
start: options.start || 0, | ||
end: options.end || points.length, | ||
size: options.size || 2, | ||
isClosed: options.isClosed | ||
}; | ||
Object.freeze(this); | ||
@@ -17,7 +26,4 @@ } | ||
value: function getSignedArea() { | ||
var area = 0; | ||
this.forEachSegment(function (p1, p2) { | ||
area += (p1[0] + p2[0]) * (p1[1] - p2[1]); | ||
}); | ||
return area / 2; | ||
if (this.isFlatArray) return getPolygonSignedArea(this.points, this.options); | ||
return getPolygonSignedAreaPoints(this.points, this.options); | ||
} | ||
@@ -37,11 +43,18 @@ }, { | ||
value: function forEachSegment(visitor) { | ||
var length = this.points.length; | ||
for (var i = 0; i < length - 1; i++) { | ||
visitor(this.points[i], this.points[i + 1], i, i + 1); | ||
if (this.isFlatArray) { | ||
forEachSegmentInPolygon(this.points, function (x1, y1, x2, y2, i1, i2) { | ||
visitor([x1, y1], [x2, y2], i1, i2); | ||
}, this.options); | ||
} else { | ||
forEachSegmentInPolygonPoints(this.points, visitor, this.options); | ||
} | ||
} | ||
}, { | ||
key: "modifyWindingDirection", | ||
value: function modifyWindingDirection(direction) { | ||
if (this.isFlatArray) { | ||
return modifyPolygonWindingDirection(this.points, direction, this.options); | ||
} | ||
if (!this.isClosed) { | ||
visitor(this.points[length - 1], this.points[0], length - 1, 0); | ||
} | ||
return modifyPolygonWindingDirectionPoints(this.points, direction, this.options); | ||
} | ||
@@ -48,0 +61,0 @@ }]); |
@@ -8,3 +8,3 @@ { | ||
}, | ||
"version": "3.3.2", | ||
"version": "3.4.0-alpha.1", | ||
"keywords": [ | ||
@@ -29,5 +29,5 @@ "webgl", | ||
"dependencies": { | ||
"@math.gl/core": "3.3.2" | ||
"@math.gl/core": "3.4.0-alpha.1" | ||
}, | ||
"gitHead": "7888ec8b0b010bf322c3da09b120cfe563a52be9" | ||
"gitHead": "6d7f63a0187ce1890df618b89c33931d593762d9" | ||
} |
export {default as Polygon} from './polygon'; | ||
export { | ||
getPolygonSignedArea, | ||
getPolygonWindingDirection, | ||
forEachSegmentInPolygon, | ||
modifyPolygonWindingDirection, | ||
WINDING | ||
} from './polygon-utils'; | ||
export {clipPolygon, clipPolyline} from './lineclip'; | ||
@@ -4,0 +12,0 @@ |
@@ -1,18 +0,34 @@ | ||
import {equals} from '@math.gl/core'; | ||
/* eslint-disable no-undef */ | ||
/* eslint-disable no-console */ | ||
import {isArray} from '@math.gl/core'; | ||
import { | ||
getPolygonSignedArea, | ||
forEachSegmentInPolygon, | ||
modifyPolygonWindingDirection, | ||
getPolygonSignedAreaPoints, | ||
forEachSegmentInPolygonPoints, | ||
modifyPolygonWindingDirectionPoints | ||
} from './polygon-utils'; | ||
export default class Polygon { | ||
constructor(points) { | ||
constructor(points, options = {}) { | ||
this.points = points; | ||
this.isClosed = equals(this.points[this.points.length - 1], this.points[0]); | ||
this.isFlatArray = !isArray(points[0]); | ||
this.options = { | ||
start: options.start || 0, | ||
end: options.end || points.length, | ||
size: options.size || 2, | ||
isClosed: options.isClosed | ||
}; | ||
Object.freeze(this); | ||
} | ||
// https://en.wikipedia.org/wiki/Shoelace_formula | ||
getSignedArea() { | ||
let area = 0; | ||
this.forEachSegment((p1, p2) => { | ||
// the "cancelling" cross-products: (p1.x + p2.x) * (p1.y - p2.y) | ||
area += (p1[0] + p2[0]) * (p1[1] - p2[1]); | ||
}); | ||
return area / 2; | ||
if (this.isFlatArray) return getPolygonSignedArea(this.points, this.options); | ||
return getPolygonSignedAreaPoints(this.points, this.options); | ||
} | ||
@@ -29,11 +45,23 @@ | ||
forEachSegment(visitor) { | ||
const length = this.points.length; | ||
for (let i = 0; i < length - 1; i++) { | ||
visitor(this.points[i], this.points[i + 1], i, i + 1); | ||
if (this.isFlatArray) { | ||
forEachSegmentInPolygon( | ||
this.points, | ||
// eslint-disable-next-line max-params | ||
(x1, y1, x2, y2, i1, i2) => { | ||
// TODO @igorDykhta original visitor uses arrays for each point, but with flat arrays performance degrades if we allocate points for each segment | ||
visitor([x1, y1], [x2, y2], i1, i2); | ||
}, | ||
this.options | ||
); | ||
} else { | ||
forEachSegmentInPolygonPoints(this.points, visitor, this.options); | ||
} | ||
if (!this.isClosed) { | ||
// Call function with points and indices | ||
visitor(this.points[length - 1], this.points[0], length - 1, 0); | ||
} | ||
modifyWindingDirection(direction) { | ||
if (this.isFlatArray) { | ||
return modifyPolygonWindingDirection(this.points, direction, this.options); | ||
} | ||
return modifyPolygonWindingDirectionPoints(this.points, direction, this.options); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
299316
68
3528
2
+ Added@math.gl/core@3.4.0-alpha.1(transitive)
- Removed@math.gl/core@3.3.2(transitive)
Updated@math.gl/core@3.4.0-alpha.1