{"name":"poly-decomp-es","version":"0.4.2","license":"MIT","description":"Convex decomposition for 2D polygons","homepage":"https://github.com/pmndrs/poly-decomp-es","author":{"name":"Stefan Hedman","email":"schteppe@gmail.com","url":"http://steffe.se"},"contributors":[{"name":"Isaac Mason","email":"isaac@isaacmason.com","url":"https://github.com/isaac-mason"}],"keywords":["convex","decomposition","polygon","2d"],"main":"./dist/poly-decomp-es.cjs.js","module":"./dist/poly-decomp-es.js","react-native":"./dist/poly-decomp-es.js","types":"./dist/poly-decomp-es.d.ts","sideEffects":false,"scripts":{"eslint":"eslint .","eslint-fix":"eslint --fix .","prepare":"husky install","prebuild":"rimraf dist","build":"npm run typegen && rollup -c","prepublishOnly":"npm run build && npm run test","prettier":"prettier --list-different .","prettier-fix":"prettier --write .","typecheck":"tsc --noEmit --emitDeclarationOnly false --strict","typegen":"tsc --outFile dist/poly-decomp-es.d.ts","test":"jest","change":"yarn changeset","bump":"yarn changeset version && yarn install --mode update-lockfile","release":"yarn build && yarn test && changeset publish"},"repository":{"type":"git","url":"git+https://github.com/pmndrs/poly-decomp-es.git"},"bugs":{"url":"https://github.com/pmndrs/poly-decomp-es/issues"},"devDependencies":{"@babel/core":"^7.16.5","@babel/preset-env":"^7.16.5","@babel/preset-typescript":"^7.16.5","@changesets/cli":"^2.22.0","@rollup/plugin-babel":"^6.0.3","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.1.0","@rollup/plugin-replace":"^5.0.2","@types/jest":"^27.0.3","@types/node":"^20.3.0","@typescript-eslint/eslint-plugin":"^5.17.0","@typescript-eslint/parser":"^5.17.0","eslint":"^8.12.0","eslint-config-prettier":"^8.5.0","eslint-plugin-es":"^4.1.0","eslint-plugin-simple-import-sort":"^10.0.0","eslint-plugin-typescript-enum":"^2.1.0","husky":"^8.0.3","jest":"^27.4.5","lint-staged":"^13.2.2","prettier":"^2.5.1","rimraf":"^5.0.1","rollup":"^2.62.0","rollup-plugin-filesize":"^10.0.0","ts-jest":"^27.1.2","ts-node":"^10.4.0","typescript":"^4.5.4"},"lint-staged":{"*.{js,json,jsx,ts,tsx,md,yaml,yml}":"prettier --write"},"gitHead":"23f764641d7b63b215ea191b8b122f4c0d1b8f4f","_id":"poly-decomp-es@0.4.2","_nodeVersion":"18.17.0","_npmVersion":"9.6.7","dist":{"integrity":"sha512-akjZkaTeyfECMNkMqv0g9gFiU2HjpZ5ubJA/aczw1ILdgWQbv1OPwCi05/9JbY2GnGpjm9k85Jdbd/j/p/lg/g==","shasum":"2b874984e6f1d0efa3a27d0b13bbf18904a58156","tarball":"http://registry-npm.ywwl.com/poly-decomp-es/download/poly-decomp-es-0.4.2.tgz","fileCount":7,"unpackedSize":68853,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCIOv4FQJfX9wgSlbhd8x3k7nYMSOgjAwEFMrsy68lNAwIgECp80PmjfZv6TFxgcUvjYAV5wVARbbol/RWB3G9sKzU="}],"size":25006,"noattachment":false,"key":"/poly-decomp-es/-/poly-decomp-es-0.4.2.tgz"},"_npmUser":{"name":"isaacmason","email":"isaac@isaacmason.com"},"directories":{},"maintainers":[{"name":"drcmda","email":"drcmda@gmail.com"},{"name":"joergjaeckel","email":"jojaecke@gmail.com"},{"name":"isaacmason","email":"isaac@isaacmason.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/poly-decomp-es_0.4.2_1691407299150_0.07933402894548292"},"_hasShrinkwrap":false,"readme":"# poly-decomp-es\n\nThis is a maintained fork of poly-decomp.js, originally created by Stefan Hedman @schteppe.\n\n`poly-decomp-es` is a library for decomposing a 2D polygon into convex pieces.\n\n```\nyarn add poly-decomp-es\n```\n\n![Decomposing a convcave polygon into convex regions](./readme.gif)\n\n[**Launch the demo!**](https://poly-decomp-es.vercel.app/)\n\nThe library implements two algorithms, one optimal (but slow) and one less optimal (but fast).\n\nIt's is a manual port of the C++ library [Poly Decomp](https://mpen.ca/406/bayazit) by [Mark Penner](https://mpen.ca).\n\n### Basic usage\n\n```js\nimport { decomp, makeCCW, quickDecomp } from 'poly-decomp-es'\n\n// Create a concave polygon\nconst concavePolygon = [\n  [-1, 1],\n  [-1, 0],\n  [1, 0],\n  [1, 1],\n  [0.5, 0.5],\n]\n\n// Make sure the polygon has counter-clockwise winding. Skip this step if you know it's already counter-clockwise.\nmakeCCW(concavePolygon)\n\n// Decompose into convex polygons, using the faster algorithm\nconst convexPolygons = quickDecomp(concavePolygon)\n\n// ==> [  [[1,0],[1,1],[0.5,0.5]],  [[0.5,0.5],[-1,1],[-1,0],[1,0]]  ]\n\n// Decompose using the slow (but optimal) algorithm\nconst optimalConvexPolygons = decomp(concavePolygon)\n\n// ==> [  [[-1,1],[-1,0],[1,0],[0.5,0.5]],  [[1,0],[1,1],[0.5,0.5]]  ]\n```\n\n### Advanced usage\n\n```js\nimport { isSimple, makeCCW, quickDecomp } from 'poly-decomp-es'\n\n// Get user input as an array of points.\nconst polygon = getUserInput()\n\n// Check if the polygon self-intersects\nif (isSimple(polygon)) {\n  // Reverse the polygon to make sure it uses counter-clockwise winding\n  makeCCW(polygon)\n\n  // Decompose into convex pieces\n  const convexPolygons = quickDecomp(polygon)\n\n  // Draw each point on an HTML5 Canvas context\n  for (let i = 0; i < convexPolygons.length; i++) {\n    const convexPolygon = convexPolygons[i]\n\n    ctx.beginPath()\n    const firstPoint = convexPolygon[0]\n    ctx.moveTo(firstPoint[0], firstPoint[1])\n\n    for (let j = 1; j < convexPolygon.length; j++) {\n      const point = convexPolygon[j]\n      const x = point[0]\n      const y = point[1]\n      c.lineTo(x, y)\n    }\n    ctx.closePath()\n    ctx.fill()\n  }\n}\n```\n\n### Documentation\n\n#### type Point = [number, number]\n\n#### type Polygon = Point[]\n\n#### quickDecomp(polygon: Polygon): Polygon[]\n\n```js\nimport { quickDecomp } from 'poly-decomp-es'\n\nconst convexPolygons = quickDecomp(polygon)\n```\n\nSlices the polygon into convex sub-polygons, using a fast algorithm. Note that the input points objects will be re-used in the result array.\n\nIf the polygon is not simple, the decomposition will produce unexpected results.\n\n#### decomp(polygon: Polygon): Polygon[] | false\n\n```js\nimport { decomp } from 'poly-decomp-es'\n\nconst convexPolygons = decomp(polygon)\n```\n\nDecomposes 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.\n\nReturns false if the decomposition fails.\n\n#### isSimple(polygon: Polygon): boolean\n\n```js\nimport { isSimple, quickDecomp } from 'poly-decomp-es'\n\nif (isSimple(polygon)) {\n  // Polygon does not self-intersect - it's safe to decompose.\n  const convexPolygons = quickDecomp(polygon)\n}\n```\n\nReturns true if the polygon does not self-intersect. Use this to check if the input polygon is OK to decompose.\n\n#### makeCCW(polygon: Polygon): void\n\n```js\nimport { makeCCW } from 'poly-decomp-es'\n\nconsole.log('Polygon with clockwise winding:', polygon)\nmakeCCW(polygon)\nconsole.log('Polygon with counter-clockwise winding:', polygon)\n```\n\nReverses the polygon, if its vertices are not ordered counter-clockwise. Note that the input polygon array will be modified in place.\n\n#### removeCollinearPoints(polygon: Polygon, thresholdAngle = 0): void\n\n```js\nimport { removeCollinearPoints } from 'poly-decomp-es'\n\nconst before = polygon.length\nremoveCollinearPoints(polygon, 0.1)\nconst numRemoved = before - polygon.length\nconsole.log(numRemoved + ' collinear points could be removed')\n```\n\nRemoves 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.\n\n#### removeDuplicatePoints(polygon: Polygon, precision = 0): void\n\n```js\nimport { removeDuplicatePoints } from 'poly-decomp-es'\n\nconst polygon = [\n  [0, 0],\n  [1, 1],\n  [2, 2],\n  [0, 0],\n]\nremoveDuplicatePoints(polygon, 0.01)\n\n// polygon is now [[1,1],[2,2],[0,0]]\n```\n","publish_time":1691407299328,"_cnpm_publish_time":1691407299328,"dist-tags":{"latest":"0.4.2"}}