petak, 11. travnja 2014.

Intersection of two 2D polygons in Javascript

If you are dealing with 2D graphics, you must have dealt with polygons. Polygons such as:



Polygons under a are called convex polygons and those under b are called concave. There is also another kind of classification: simple and complex polygons. All of the above polygons are simple. If you draw a following polygon using 5 straight lines, then it is a complex polygon.



The same shape can also be drawn with more lines. In that case, this shape can represent three triangles, that is, three separate polygons that are merely positioned together.

If you wish to calculate intersection of two polygons, this can be relatively simple for convex polygons but a little bit trickier for concave and especially for complex polygons.

Convex polygon intersections:















Concave polygon intersection:
















I created a javascript library that take 2 polygons and return a third polygon that represents an intersection of the given 2 polygons. It works for both convex and concave polygon, but probably not as expected for complex polygons. Polygons are javascript arrays of Points. 'Point' is any javascript object that has both x and y numeric properties. The order of points in an array that defines a polygon is very important. The same set of n points can produce n! different polygons. Most all of them will be complex.

The .js file can be downloaded here:

Polygon.js

Library depends on jQuery for creating shallow copies of javascript objects - $.extend({}, object). Also for various utility functions like $.grep()


Broj komentara: 12:

  1. I had similar requirement where polygon intersection is required. some points were not clear to me about your code polygon.js. In mean time I was also evaluating some other libs also for polygon boolean functions. I have put my requirement in detail on stackoverflow(http://stackoverflow.com/questions/24085049/javascript-module-for-polygon-boolean-functions-union-intersection-difference) , please see if you can help.

    OdgovoriIzbriši
    Odgovori
    1. This lib is a result of one week efforts. I decided to go for my custom solution because my requirements were simple.

      I optimized it for performace as much as I could while keeping the code not too large. Since there are not many simple JS solutions out there, I decided to put it online.

      With little effort, the existing Intersect functionality can be adapted to include Union functionality.
      For Xor, I would need a little bit more time.

      I haven't done as much research as you.
      I saw you commented on the use of jQuery. If you need something high-end, this lib is not recommended.

      My requirements were that I can get an intersection of 2 polygons of under 100 points each, within a second. And I believe that this lib accomplishes that just fine.

      Izbriši
  2. This worked great for me, thanks.

    OdgovoriIzbriši
  3. Can you provide the license information?

    OdgovoriIzbriši
  4. Must be a newbie question, but what is the input format of the polygon ?
    I tried to create a JSON polygon, but no luck.
    Can you just provide an exemple ?

    OdgovoriIzbriši
  5. I believe a polygon is an array of points.
    Point is a javascript object {x: __, y: __}

    OdgovoriIzbriši
  6. I think I have found a little bug :-( with a concave polygon

    this call is right : intersectionPolygons([{x:0.5,y:0.5},{x:0,y:0},{x:1,y:0},{x:0.5,y:0.5}],[{x:0,y:0},{x:.5,y:0},{x:.5,y:.5},{x:0.25,y:.5},{x:0.25,y:1},{x:1,y:1},{x:1,y:2},{x:0,y:2},{x:0,y:0}])

    but swap the two polygons, this call returns the concave polygon : intersectionPolygons([{x:0,y:0},{x:.5,y:0},{x:.5,y:.5},{x:0.25,y:.5},{x:0.25,y:1},{x:1,y:1},{x:1,y:2},{x:0,y:2},{x:0,y:0}],[{x:0.5,y:0.5},{x:0,y:0},{x:1,y:0},{x:0.5,y:0.5}])

    thanks for your work and for sharing!

    Christophe

    OdgovoriIzbriši
  7. Thank you for posting your library. It is so simple to use and efficient !
    Dan

    OdgovoriIzbriši
  8. Any chance of adding one of the Open Source licences to this?

    OdgovoriIzbriši
  9. I once got stuck with the coding thing in my laptop. I’m not a tech geek so I took help with my cousin. He was searching for the plus size ugly Christmas sweater for himself. For the grateful gesture I gifted him those plus size pajamas.

    OdgovoriIzbriši
  10. we'll explore what makes these elite pelle pelle leather coats for sale a coveted choice for fashion enthusiasts and whether they are truly worth the investment.

    OdgovoriIzbriši