1.0.6 • Published 3 years ago

intersecting-polygon-detector v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Intersection polygon detector

This package validate 2D polygon array for self intersecting lines.
Returns true if polygon has intersecting lines and false if does not.
Does not have any dependencies.
Use case example -> coordinates array from geoJSON object, validation before saving to DB e.g. mongodb will fail on geoJSON query if polygon has self intersecting line.

Valid polygon example

Valid polygon

Self intersecting polygon example

Valid polygon

How to use this package

installation

npm install intersecting-polygon-detector

use

const isPolygonIntersecting = require('intersecting-polygon-detector');
// or
import *  as isPolygonIntersecting from 'intersecting-polygon-detector';

const   validPolygon = [
    [1, 1.5],
    [2, 3],
    [1, -1],
    [-1, -1],
    [-1, 1],
    [-1, 3],
    [1, 1.5],
  ];

  const intersectingPolygon = [
    [1, 1],
    [1, -1],
    [-1, 1],
    [-1, -1],
    [1, 1],
  ];

  isPolygonIntersecting(validPolygon)  // returns false

  isPolygonIntersecting(intersectingPolygon) // returns true

practical use example

import *  as isPolygonIntersecting from 'intersecting-polygon-detector';

function saveUsersPolygon(polygon){
  if(sPolygonIntersecting(polygon)){
    throw new Error('Polygon is self intersecting!')
  }

  // carry on with saving to DB

}

additional behaviour

  • validates the polygon if it is an array
  • validates if array has minimum length of 4
  • validates if each point contains only 2 coordinates and thay are of type numbers
  • validates if the polygon is closing properly (start point equals end point)