Gathering detailed insights and metrics for poly-decomp
Gathering detailed insights and metrics for poly-decomp
Gathering detailed insights and metrics for poly-decomp
Gathering detailed insights and metrics for poly-decomp
npm install poly-decomp
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
435 Stars
67 Commits
105 Forks
14 Watching
3 Branches
1 Contributors
Updated on 20 Nov 2024
JavaScript (78%)
HTML (22%)
Cumulative downloads
Total Downloads
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
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.
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.
npm install poly-decomp
Then require it like so:
1var decomp = require('poly-decomp');
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]] ]
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}
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.
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.
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.
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.
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.
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]]
removeDuplicatePoints
.makeCCW
now returns true if the polygon was changed.Polygon.prototype.removeCollinearPoints
.thresholdAngle
to Point.collinear(a,b,c,thresholdAngle)
.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
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
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