Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/schteppe/poly-decomp.js
Decompose 2D polygons into convex pieces.
https://github.com/schteppe/poly-decomp.js
Last synced: 10 days ago
JSON representation
Decompose 2D polygons into convex pieces.
- Host: GitHub
- URL: https://github.com/schteppe/poly-decomp.js
- Owner: schteppe
- License: mit
- Created: 2013-10-12T09:31:41.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2023-08-08T20:14:46.000Z (over 1 year ago)
- Last Synced: 2024-10-29T20:25:13.787Z (10 days ago)
- Language: JavaScript
- Homepage: http://schteppe.github.io/poly-decomp.js/
- Size: 147 KB
- Stars: 434
- Watchers: 14
- Forks: 105
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-game-engine-dev - Poly-Decomp.js - Decompose 2D polygons into convex pieces. (Libraries / JavaScript)
README
poly-decomp.js
==============Library for decomposing a 2D polygon into convex pieces.
![Decomposing a convcave polygon into convex regions](https://cloud.githubusercontent.com/assets/1063152/18008563/edccfe86-6ba8-11e6-9e20-a090c1812c95.gif)
[Launch the demo!](http://schteppe.github.io/poly-decomp.js/)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=Z2G8VHLDJ9N3L&lc=GB&item_name=Stefan%20Hedman&item_number=schteppe¤cy_code=USD&bn=PP%2dDonationsBF%3aDonate%2dPayPal%2dgreen%2esvg%3aNonHosted)
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](https://mpen.ca/406/bayazit) by [Mark Penner](https://mpen.ca).### Install
##### Browser
Download [decomp.js](build/decomp.js) or [decomp.min.js](build/decomp.min.js) and include the script in your HTML:
```html```
Then you can use the ```decomp``` global.
##### Node.js
```
npm install poly-decomp
```Then require it like so:
```js
var decomp = require('poly-decomp');
```### Basic usage
```js
// Create a concave polygon
var concavePolygon = [
[ -1, 1],
[ -1, 0],
[ 1, 0],
[ 1, 1],
[0.5, 0.5]
];// Make sure the polygon has counter-clockwise winding. Skip this step if you know it's already counter-clockwise.
decomp.makeCCW(concavePolygon);// Decompose into convex polygons, using the faster algorithm
var convexPolygons = decomp.quickDecomp(concavePolygon);// ==> [ [[1,0],[1,1],[0.5,0.5]], [[0.5,0.5],[-1,1],[-1,0],[1,0]] ]
// Decompose using the slow (but optimal) algorithm
var convexPolygons = decomp.decomp(concavePolygon);// ==> [ [[-1,1],[-1,0],[1,0],[0.5,0.5]], [[1,0],[1,1],[0.5,0.5]] ]
```### Advanced usage
```js
// Get user input as an array of points.
var polygon = getUserInput();// Check if the polygon self-intersects
if(decomp.isSimple(polygon)){
// Reverse the polygon to make sure it uses counter-clockwise winding
decomp.makeCCW(polygon);
// Decompose into convex pieces
var convexPolygons = decomp.quickDecomp(polygon);
// Draw each point on an HTML5 Canvas context
for(var i=0; i