An algorithm for inflating/deflating (offsetting, buffering) polygons

后端 未结 12 2150
醉话见心
醉话见心 2020-11-22 12:58

How would I "inflate" a polygon? That is, I want to do something similar to this:

\"alt

<
12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 13:02

    For these types of things I usually use JTS. For demonstration purposes I created this jsFiddle that uses JSTS (JavaScript port of JTS). You just need to convert the coordinates you have to JSTS coordinates:

    function vectorCoordinates2JTS (polygon) {
      var coordinates = [];
      for (var i = 0; i < polygon.length; i++) {
        coordinates.push(new jsts.geom.Coordinate(polygon[i].x, polygon[i].y));
      }
      return coordinates;
    }
    

    The result is something like this:

    Additional info: I usually use this type of inflating/deflating (a little modified for my purposes) for setting boundaries with radius on polygons that are drawn on a map (with Leaflet or Google maps). You just convert (lat,lng) pairs to JSTS coordinates and everything else is the same. Example:

提交回复
热议问题