Javascript module for polygon logic functions (union, intersection, difference and XOR)

I have a polygon. In this set, some of them are external polygons, and some may (or cannot) be holes. At this stage, I do not know what polygonal holes are. I want to calculate a finite polygon that combines all the polygons, including holes.

I thought about this:

//'SomeLib' that has polygon boolean fucntions var polygonSet = [poly1,poly2,...polyn]; var union, intersection; var combinedPoly = SomeLib.XOR(polygonSet[0], polygonSet[1]); for( var i=2; i<polygonSet.length ; i++) { combinedPoly = SomeLib.XOR(combinedPoly, polygonSet[i]); //or if XOR is not available union = SomeLib.union(combinedPoly, polygonSet[i]); intersection = SomeLib.intersection(combinedPoly, polygonSet[i]); combinedPoly = union - intersection; } 

So my requirement for the module

  • only four logical functions of the polygon
  • If the npm module is available, its good
  • lightweight as a combined polygon will be one of many features. I mean, the size of the application is already bigger, so you are looking for a small library.
  • Efficiency: in my case, the number of polygons in the set may not be high, but the points in the polygon in large quantities, so the search for O (k.log (n)) is not O (kn)

The list of libraries that I came across, and some points that I realized:

  • Is JSCLipper efficient, without the npm module, is a github project in sync with soureforge JSCLipper ?
  • kld-intersections there is a polygon intersection, but there is no connection (or I can not find), it has many other functions (except for a Boolean polygon). This is the javscript port of this project.
  • polygon.js depends on jQuery, efficiency ?, there is no npm module, do not confuse with polygon.js
  • boolean in paper.js its complement to paper.js lib, not an independent module
  • raphael-boolean its complement to raphael lib, not an independent module
  • tess2.js The GLU tessellator ported to Javascript contains many functions for tessellation - is tessellation necessary for the logical functions of the polygon? I could not find the logical functions, no documentation, there is some error on the html test page, without the npm module
  • turf-donuts depends on another large JSTS library
  • JSTS Topology Suite large library not modular

In some cases, these functions are part of large libraries that are overloaded with other functions or libraries working with SVG or Geo-spatial environement, or many of them are added to an existing library.

Please suggest for my requirement which library is suitable? Are there any javscript modular libraries for the logical functions of the polygon?

+6
source share
2 answers

I started to implement the Vatti polygon clipping algorithm , here is the code store. But I found the Greiner Hormann polygon clipping algorithm much better. Therefore, I do not support the code .

I recommend the Greiner Hormann implementation in JavaScript for clipping polygons or other logical operations.

0
source

I use my own methods that work with convex SVG polygons:

  • Set convex polygons counterclockwise

  • Polygons - Fix for Convex / CCW

  • Point inside a convex polygon: Jordan Curve Theorem

  • Polygon intersection line - vector analysis

  • Polygon Intersection - Vector Analysis

  • Compound intersecting polygon theorem with Jordan curve

  • Convex polygons - crop, uses the Sutherland-Hodgman clipping algorithm

Here are shown here .

You can also check this out .

+2
source

Source: https://habr.com/ru/post/970460/


All Articles