mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
124 lines
3.6 KiB
JavaScript
124 lines
3.6 KiB
JavaScript
/* Copyright (c) 2012 the authors listed at the following URL, and/or
|
|
the authors of referenced articles or incorporated external code:
|
|
http://en.literateprograms.org/Quickhull_(Javascript)?action=history&offset=20120410175256
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
a copy of this software and associated documentation files (the
|
|
"Software"), to deal in the Software without restriction, including
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=18434
|
|
*/
|
|
|
|
(function () {
|
|
L.QuickHull = {
|
|
getDistant: function (cpt, bl) {
|
|
var vY = bl[1].lat - bl[0].lat,
|
|
vX = bl[0].lng - bl[1].lng;
|
|
return (vX * (cpt.lat - bl[0].lat) + vY * (cpt.lng - bl[0].lng));
|
|
},
|
|
|
|
|
|
findMostDistantPointFromBaseLine: function (baseLine, latLngs) {
|
|
var maxD = 0,
|
|
maxPt = null,
|
|
newPoints = [],
|
|
i, pt, d;
|
|
|
|
for (i = latLngs.length - 1; i >= 0; i--) {
|
|
pt = latLngs[i];
|
|
d = this.getDistant(pt, baseLine);
|
|
|
|
if (d > 0) {
|
|
newPoints.push(pt);
|
|
} else {
|
|
continue;
|
|
}
|
|
|
|
if (d > maxD) {
|
|
maxD = d;
|
|
maxPt = pt;
|
|
}
|
|
|
|
}
|
|
return { 'maxPoint': maxPt, 'newPoints': newPoints };
|
|
},
|
|
|
|
buildConvexHull: function (baseLine, latLngs) {
|
|
var convexHullBaseLines = [],
|
|
t = this.findMostDistantPointFromBaseLine(baseLine, latLngs);
|
|
|
|
if (t.maxPoint) { // if there is still a point "outside" the base line
|
|
convexHullBaseLines =
|
|
convexHullBaseLines.concat(
|
|
this.buildConvexHull([baseLine[0], t.maxPoint], t.newPoints)
|
|
);
|
|
convexHullBaseLines =
|
|
convexHullBaseLines.concat(
|
|
this.buildConvexHull([t.maxPoint, baseLine[1]], t.newPoints)
|
|
);
|
|
return convexHullBaseLines;
|
|
} else { // if there is no more point "outside" the base line, the current base line is part of the convex hull
|
|
return [baseLine];
|
|
}
|
|
},
|
|
|
|
getConvexHull: function (latLngs) {
|
|
//find first baseline
|
|
var maxLat = false, minLat = false,
|
|
maxPt = null, minPt = null,
|
|
i;
|
|
|
|
for (i = latLngs.length - 1; i >= 0; i--) {
|
|
var pt = latLngs[i];
|
|
if (maxLat === false || pt.lat > maxLat) {
|
|
maxPt = pt;
|
|
maxLat = pt.lat;
|
|
}
|
|
if (minLat === false || pt.lat < minLat) {
|
|
minPt = pt;
|
|
minLat = pt.lat;
|
|
}
|
|
}
|
|
var ch = [].concat(this.buildConvexHull([minPt, maxPt], latLngs),
|
|
this.buildConvexHull([maxPt, minPt], latLngs));
|
|
return ch;
|
|
}
|
|
};
|
|
}());
|
|
|
|
L.MarkerCluster.include({
|
|
getConvexHull: function () {
|
|
var childMarkers = this.getAllChildMarkers(),
|
|
points = [],
|
|
hullLatLng = [],
|
|
hull, p, i;
|
|
|
|
for (i = childMarkers.length - 1; i >= 0; i--) {
|
|
p = childMarkers[i].getLatLng();
|
|
points.push(p);
|
|
}
|
|
|
|
hull = L.QuickHull.getConvexHull(points);
|
|
|
|
for (i = hull.length - 1; i >= 0; i--) {
|
|
hullLatLng.push(hull[i][0]);
|
|
}
|
|
|
|
return hullLatLng;
|
|
}
|
|
}); |