piptester

1.0.1 • Public • Published

PIPTester

a point in polygon tester

Install

npm install piptester --save

Code Examples

import PIPTester from 'piptester'

// create a 3 sides polygon(triangle) tester, using webgl test by default
const tester = new PIPTester({
  // polygon points
  points: [{x: 0, y: 0}, {x: 5, y: 5}, {x: 10, y: 0}]
})

expect(tester.test({x: 5, y: 3})).to.equal(true)
expect(tester.test({x: 0, y: 1})).to.equal(false)
import PIPTester from 'piptester'

// create a 5 sides polygon tester, using Ray Casting test
const tester = new PIPTester({
  points: [{x: 0, y: 0}, {x: 2, y: 0}, {x: 3, y: 1}, {x: 2, y: 2}, {x: 1, y: 2}],
  mode: 'ray'
})

expect(tester.test({x: 1, y: 1})).to.equal(true)
expect(tester.test({x: 0, y: 1})).to.equal(false)
expect(tester.test({x: 2, y: 0})).to.equal(true)
expect(tester.test({x: 1.5, y: 2})).to.equal(true)

Features

List of features ready:

  • Ray Casting test
  • WebGL test

TODOs for future development:

  • winding number test

Inspiration

This tool is based on Mecki's post about how to determine whether a 2D point is within a polygon in stackoverflow.

Tanks for Mecki's fantastic answer!

API Documentation

PIPTester Class

Param Type Description
points array the polygon's vertex.
mode string the mode used to test. options: 'webgl' or 'ray'. default: webgl.

test(point) => boolean

test if a point lies inside or outside the polygon.

License

MIT

Copyright (c) 2018-present Chunlin He

Package Sidebar

Install

npm i piptester

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

42 kB

Total Files

18

Last publish

Collaborators

  • hcl1687