PolyUtil.js (Leaflet-1.8.0) | : | PolyUtil.js (Leaflet-1.9.0) | ||
---|---|---|---|---|
import * as LineUtil from './LineUtil'; | import * as LineUtil from './LineUtil'; | |||
import {toLatLng} from '../geo/LatLng'; | ||||
import {toPoint} from './Point'; | ||||
/* | /* | |||
* @namespace PolyUtil | * @namespace PolyUtil | |||
* Various utility functions for polygon geometries. | * Various utility functions for polygon geometries. | |||
*/ | */ | |||
/* @function clipPolygon(points: Point[], bounds: Bounds, round?: Boolean): Poin t[] | /* @function clipPolygon(points: Point[], bounds: Bounds, round?: Boolean): Poin t[] | |||
* Clips the polygon geometry defined by the given `points` by the given bounds (using the [Sutherland-Hodgman algorithm](https://en.wikipedia.org/wiki/Sutherla nd%E2%80%93Hodgman_algorithm)). | * Clips the polygon geometry defined by the given `points` by the given bounds (using the [Sutherland-Hodgman algorithm](https://en.wikipedia.org/wiki/Sutherla nd%E2%80%93Hodgman_algorithm)). | |||
* Used by Leaflet to only show polygon points that are on the screen or near, i ncreasing | * Used by Leaflet to only show polygon points that are on the screen or near, i ncreasing | |||
* performance. Note that polygon points needs different algorithm for clipping | * performance. Note that polygon points needs different algorithm for clipping | |||
* than polyline, so there's a separate method for it. | * than polyline, so there's a separate method for it. | |||
skipping to change at line 56 | skipping to change at line 57 | |||
p = LineUtil._getEdgeIntersection(b, a, edge, bou nds, round); | p = LineUtil._getEdgeIntersection(b, a, edge, bou nds, round); | |||
p._code = LineUtil._getBitCode(p, bounds); | p._code = LineUtil._getBitCode(p, bounds); | |||
clippedPoints.push(p); | clippedPoints.push(p); | |||
} | } | |||
} | } | |||
points = clippedPoints; | points = clippedPoints; | |||
} | } | |||
return points; | return points; | |||
} | } | |||
/* @function polygonCenter(latlngs: LatLng[] crs: CRS): LatLng | ||||
* Returns the center ([centroid](http://en.wikipedia.org/wiki/Centroid)) of the | ||||
passed LatLngs (first ring) from a polygon. | ||||
*/ | ||||
export function polygonCenter(latlngs, crs) { | ||||
var i, j, p1, p2, f, area, x, y, center; | ||||
if (!latlngs || latlngs.length === 0) { | ||||
throw new Error('latlngs not passed'); | ||||
} | ||||
if (!LineUtil.isFlat(latlngs)) { | ||||
console.warn('latlngs are not flat! Only the first ring will be u | ||||
sed'); | ||||
latlngs = latlngs[0]; | ||||
} | ||||
var points = []; | ||||
for (var k in latlngs) { | ||||
points.push(crs.project(toLatLng(latlngs[k]))); | ||||
} | ||||
var len = points.length; | ||||
area = x = y = 0; | ||||
// polygon centroid algorithm; | ||||
for (i = 0, 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; | ||||
} | ||||
if (area === 0) { | ||||
// Polygon is so small that all points are on same pixel. | ||||
center = points[0]; | ||||
} else { | ||||
center = [x / area, y / area]; | ||||
} | ||||
return crs.unproject(toPoint(center)); | ||||
} | ||||
End of changes. 2 change blocks. | ||||
1 lines changed or deleted | 2 lines changed or added |