/* * L.Polygon implements polygon vector layer (closed polyline with a fill inside). */ L.Polygon = L.Polyline.extend({ options: { fill: true }, getCenter: function () { var i, j, len, p1, p2, f, area, x, y, points = this._rings[0]; // polygon centroid algorithm; only uses the first ring if there are multiple area = x = y = 0; for (i = 0, len = points.length, j = len - 1; i < len; j = i++) { p1 = points[i]; p2 = points[j]; f = p1.y * p2.x - p2.y * p1.x; x += (p1.x + p2.x) * f; y += (p1.y + p2.y) * f; area += f * 3; } return this._map.layerPointToLatLng([x / area, y / area]); }, _convertLatLngs: function (latlngs) { var result = L.Polyline.prototype._convertLatLngs.call(this, latlngs), len = result.length; // remove last point if it equals first one if (len >= 2 && result[0] instanceof L.LatLng && result[0].equals(result[len - 1])) { result.pop(); } return result; }, _clipPoints: function () { if (this.options.noClip) { this._parts = this._rings; return; } // polygons need a different clipping algorithm so we redefine that var bounds = this._renderer._bounds, w = this.options.weight, p = new L.Point(w, w); // increase clip padding by stroke width to avoid stroke on clip edges bounds = new L.Bounds(bounds.min.subtract(p), bounds.max.add(p)); this._parts = []; for (var i = 0, len = this._rings.length, clipped; i < len; i++) { clipped = L.PolyUtil.clipPolygon(this._rings[i], bounds); if (clipped.length) { this._parts.push(clipped); } } }, _updatePath: function () { this._renderer._updatePoly(this, true); } }); L.polygon = function (latlngs, options) { return new L.Polygon(latlngs, options); };