Coder Social home page Coder Social logo

pmndrs / poly-decomp-es Goto Github PK

View Code? Open in Web Editor NEW

This project forked from schteppe/poly-decomp.js

29.0 1.0 3.0 544 KB

Decompose 2D polygons into convex pieces.

Home Page: https://poly-decomp-es.vercel.app

License: MIT License

JavaScript 30.46% HTML 1.74% CSS 2.05% TypeScript 65.75%

poly-decomp-es's Introduction

poly-decomp-es

This is a maintained fork of poly-decomp.js, originally created by Stefan Hedman @schteppe.

poly-decomp-es is a library for decomposing a 2D polygon into convex pieces.

npm install poly-decomp-es
yarn add poly-decomp-es

Decomposing a convcave polygon into convex regions

Launch the demo!

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.

Basic usage

import { decomp, makeCCW, quickDecomp } from 'poly-decomp-es'

// Create a concave polygon
const 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.
makeCCW(concavePolygon)

// Decompose into convex polygons, using the faster algorithm
const convexPolygons = 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
const optimalConvexPolygons = decomp(concavePolygon)

// ==> [  [[-1,1],[-1,0],[1,0],[0.5,0.5]],  [[1,0],[1,1],[0.5,0.5]]  ]

Advanced usage

import { isSimple, makeCCW, quickDecomp } from 'poly-decomp-es'

// Get user input as an array of points.
const polygon = getUserInput()

// Check if the polygon self-intersects
if (isSimple(polygon)) {
  // Reverse the polygon to make sure it uses counter-clockwise winding
  makeCCW(polygon)

  // Decompose into convex pieces
  const convexPolygons = quickDecomp(polygon)

  // Draw each point on an HTML5 Canvas context
  for (let i = 0; i < convexPolygons.length; i++) {
    const convexPolygon = convexPolygons[i]

    ctx.beginPath()
    const firstPoint = convexPolygon[0]
    ctx.moveTo(firstPoint[0], firstPoint[1])

    for (let j = 1; j < convexPolygon.length; j++) {
      const point = convexPolygon[j]
      const x = point[0]
      const y = point[1]
      c.lineTo(x, y)
    }
    ctx.closePath()
    ctx.fill()
  }
}

Documentation

type Point = [number, number]

type Polygon = Point[]

quickDecomp(polygon: Polygon): Polygon[]

import { quickDecomp } from 'poly-decomp-es'

const convexPolygons = 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.

If the polygon is not simple, the decomposition will produce unexpected results.

decomp(polygon: Polygon): Polygon[] | false

import { decomp } from 'poly-decomp-es'

const convexPolygons = decomp(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.

Returns false if the decomposition fails.

isSimple(polygon: Polygon): boolean

import { isSimple, quickDecomp } from 'poly-decomp-es'

if (isSimple(polygon)) {
  // Polygon does not self-intersect - it's safe to decompose.
  const convexPolygons = quickDecomp(polygon)
}

Returns true if the polygon does not self-intersect. Use this to check if the input polygon is OK to decompose.

makeCCW(polygon: Polygon): void

import { makeCCW } from 'poly-decomp-es'

console.log('Polygon with clockwise winding:', polygon)
makeCCW(polygon)
console.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: Polygon, thresholdAngle = 0): void

import { removeCollinearPoints } from 'poly-decomp-es'

const before = polygon.length
removeCollinearPoints(polygon, 0.1)
const numRemoved = before - polygon.length
console.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: Polygon, precision = 0): void

import { removeDuplicatePoints } from 'poly-decomp-es'

const polygon = [
  [0, 0],
  [1, 1],
  [2, 2],
  [0, 0],
]
removeDuplicatePoints(polygon, 0.01)

// polygon is now [[1,1],[2,2],[0,0]]

poly-decomp-es's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar isaac-mason avatar schteppe avatar stefanstarstable avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

poly-decomp-es's Issues

QuickDecomp method extension?

Hello, I have a special requirement on my end that when calling the quickDecomp method, the segmented polygon should contain at least 4 vertices, so I don't want triangles to appear. What should I do?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.