polygon-offset
Version:
Polygon offsetting algorithm, aimed for use with leaflet
45 lines (39 loc) • 1.13 kB
JavaScript
L.Polygon.prototype._projectLatlngs = function (latlngs, result, projectedBounds, isHole) {
var flat = latlngs[0] instanceof L.LatLng,
len = latlngs.length,
i, ring, area;
if (flat) {
area = 0;
ring = [];
for (i = 0; i < len; i++) {
ring[i] = this._map.latLngToLayerPoint(latlngs[i]);
projectedBounds.extend(ring[i]);
if (i) {
area += ring[i - 1].x * ring[i].y;
area -= ring[i].x * ring[i - 1].y;
}
}
area += ring[len - 1].x * ring[0].y;
area -= ring[0].x * ring[len - 1].y;
if ((!isHole && area > 0) || (isHole && area < 0)) {
ring.reverse();
}
result.push(ring);
} else {
for (i = 0; i < len; i++) {
this._projectLatlngs(latlngs[i], result, projectedBounds, i !== 0);
}
}
};
L.Polygon.prototype._project = function() {
L.Polyline.prototype._project.call(this);
if ((this._latlngs.length > 1) &&
!L.Polyline._flat(this._latlngs) &&
!(this._latlngs[0][0] instanceof L.LatLng)) {
if (this.options.fillRule !== 'nonzero') {
this.setStyle({
fillRule: 'nonzero'
});
}
}
};