Installations
npm install poly-decomp
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
*
Typescript Support
No
Node Version
8.10.0
NPM Version
5.6.0
Statistics
435 Stars
67 Commits
105 Forks
14 Watching
3 Branches
1 Contributors
Updated on 20 Nov 2024
Languages
JavaScript (78%)
HTML (22%)
Total Downloads
Cumulative downloads
Total Downloads
1,039,460
Last day
10.4%
861
Compared to previous day
Last week
9.8%
4,648
Compared to previous week
Last month
-4.3%
18,583
Compared to previous month
Last year
23.3%
309,098
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
poly-decomp.js
Library for decomposing a 2D polygon into convex pieces.
The library implements two algorithms, one optimal (but slow) and one less optimal (but fast). It's is a manual port of the C++ library Poly Decomp by Mark Penner.
Install
Browser
Download decomp.js or decomp.min.js and include the script in your HTML:
1<script src="decomp.js" type="text/javascript"></script> 2<!-- or: --> 3<script src="decomp.min.js" type="text/javascript"></script>
Then you can use the decomp
global.
Node.js
npm install poly-decomp
Then require it like so:
1var decomp = require('poly-decomp');
Basic usage
1// Create a concave polygon 2var concavePolygon = [ 3 [ -1, 1], 4 [ -1, 0], 5 [ 1, 0], 6 [ 1, 1], 7 [0.5, 0.5] 8]; 9 10// Make sure the polygon has counter-clockwise winding. Skip this step if you know it's already counter-clockwise. 11decomp.makeCCW(concavePolygon); 12 13// Decompose into convex polygons, using the faster algorithm 14var convexPolygons = decomp.quickDecomp(concavePolygon); 15 16// ==> [ [[1,0],[1,1],[0.5,0.5]], [[0.5,0.5],[-1,1],[-1,0],[1,0]] ] 17 18// Decompose using the slow (but optimal) algorithm 19var convexPolygons = decomp.decomp(concavePolygon); 20 21// ==> [ [[-1,1],[-1,0],[1,0],[0.5,0.5]], [[1,0],[1,1],[0.5,0.5]] ]
Advanced usage
1// Get user input as an array of points. 2var polygon = getUserInput(); 3 4// Check if the polygon self-intersects 5if(decomp.isSimple(polygon)){ 6 7 // Reverse the polygon to make sure it uses counter-clockwise winding 8 decomp.makeCCW(polygon); 9 10 // Decompose into convex pieces 11 var convexPolygons = decomp.quickDecomp(polygon); 12 13 // Draw each point on an HTML5 Canvas context 14 for(var i=0; i<convexPolygons.length; i++){ 15 var convexPolygon = convexPolygons[i]; 16 17 ctx.beginPath(); 18 var firstPoint = convexPolygon[0]; 19 ctx.moveTo(firstPoint[0], firstPoint[1]); 20 21 for(var j=1; j<convexPolygon.length; j++){ 22 var point = convexPolygon[j]; 23 var x = point[0]; 24 var y = point[1]; 25 c.lineTo(x, y); 26 } 27 ctx.closePath(); 28 ctx.fill(); 29 } 30}
Documentation
quickDecomp(polygon: Array<Point>): Array<Array<Point>>
1var convexPolygons = decomp.quickDecomp(polygon);
Slices the polygon into convex sub-polygons, using a fast algorithm. Note that the input points objects will be re-used in the result array.
decomp(polygon: Array<Point>): Array<Array<Point>>
1var convexPolygons = decomp.quickDecomp(polygon);
Decomposes the polygon into one or more convex sub-polygons using an optimal algorithm. Note that the input points objects will be re-used in the result array.
isSimple(polygon: Array<Point>): boolean
1if(decomp.isSimple(polygon)){ 2 // Polygon does not self-intersect - it's safe to decompose. 3 var convexPolygons = decomp.quickDecomp(polygon); 4}
Returns true if any of the line segments in the polygon intersects. Use this to check if the input polygon is OK to decompose.
makeCCW(polygon: Array<Point>): void
1console.log('Polygon with clockwise winding:', polygon); 2decomp.makeCCW(polygon); 3console.log('Polygon with counter-clockwise winding:', polygon);
Reverses the polygon, if its vertices are not ordered counter-clockwise. Note that the input polygon array will be modified in place.
removeCollinearPoints(polygon: Array<Point>, thresholdAngle: number): void
1var before = polygon.length; 2decomp.removeCollinearPoints(polygon, 0.1); 3var numRemoved = before - polygon.length; 4console.log(numRemoved + ' collinear points could be removed');
Removes collinear points in the polygon. This means that if three points are placed along the same line, the middle one will be removed. The thresholdAngle
is measured in radians and determines whether the points are collinear or not. Note that the input array will be modified in place.
removeDuplicatePoints(polygon: Array<Point>, precision: number): void
1var polygon = [ 2 [0,0], 3 [1,1], 4 [2,2], 5 [0,0] 6]; 7decomp.removeDuplicatePoints(polygon, 0.01); 8 9// polygon is now [[1,1],[2,2],[0,0]]
Change log
0.3.0
- Added
removeDuplicatePoints
. makeCCW
now returns true if the polygon was changed.- Fixed case 5 mentioned here and discussed here.
0.2.1
- Fixed bug in the collinear point removal, after this fix the algorithm is more agressive and more correct.
0.2.0
- Rewrote the class based API to a minimal array-based one. See docs.
0.1
- Added method
Polygon.prototype.removeCollinearPoints
. - Added optional parameter
thresholdAngle
toPoint.collinear(a,b,c,thresholdAngle)
.
Contribute
Make sure you have git, Node.js, NPM and grunt installed.
git clone https://github.com/schteppe/poly-decomp.js.git; # Clone the repo
cd poly-decomp.js;
npm install; # Install dependencies
# (make changes to source)
grunt; # Builds build/decomp.js
The most recent commits are currently pushed to the master
branch. Thanks for contributing!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More