mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-28 00:24:21 +02:00
Add QuickHull and use it for calculating convex hulls. (From http://en.literateprograms.org/Quickhull_(Javascript)?oldid=18434 - Referenced in code) Needs tidying up to use LatLng objects rather than having to change back and forth
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Leaflet debug page</title>
|
||||
|
||||
<link rel="stylesheet" href="../lib/leaflet-dist/leaflet.css" />
|
||||
<!--[if lte IE 8]><link rel="stylesheet" href="../lib/leaflet-dist/leaflet.ie.css" /><![endif]-->
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="screen.css" />
|
||||
<script src="../lib/leaflet-dist/leaflet-src.js"></script>
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../src/MarkerCluster.css" />
|
||||
<link rel="stylesheet" href="../src/MarkerCluster.Default.css" />
|
||||
<script src="../src/MarkerClusterGroup.js"></script>
|
||||
<script src="../src/MarkerCluster.js"></script>
|
||||
<script src="../src/MarkerCluster.QuickHull.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="map"></div>
|
||||
<button id="populate">Populate 1 marker</button>
|
||||
<button id="remove">Remove 1 marker</button>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
|
||||
cloudmadeAttribution = 'Map data © 2011 OpenStreetMap contributors, Imagery © 2011 CloudMade',
|
||||
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution}),
|
||||
latlng = new L.LatLng(50.5, 30.51);
|
||||
|
||||
var map = new L.Map('map', {center: latlng, zoom: 15, layers: [cloudmade]});
|
||||
|
||||
var markers = new L.MarkerClusterGroup();
|
||||
var markersList = [];
|
||||
|
||||
function populate() {
|
||||
for (var i = 0; i < 100; i++) {
|
||||
var m = new L.Marker(getRandomLatLng(map));
|
||||
markersList.push(m);
|
||||
markers.addLayer(m);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function populateRandomVector() {
|
||||
for (var i = 0, latlngs = [], len = 20; i < len; i++) {
|
||||
latlngs.push(getRandomLatLng(map));
|
||||
}
|
||||
var path = new L.Polyline(latlngs);
|
||||
map.addLayer(path);
|
||||
}
|
||||
function getRandomLatLng(map) {
|
||||
var bounds = map.getBounds(),
|
||||
southWest = bounds.getSouthWest(),
|
||||
northEast = bounds.getNorthEast(),
|
||||
lngSpan = northEast.lng - southWest.lng,
|
||||
latSpan = northEast.lat - southWest.lat;
|
||||
|
||||
return new L.LatLng(
|
||||
southWest.lat + latSpan * Math.random(),
|
||||
southWest.lng + lngSpan * Math.random());
|
||||
}
|
||||
|
||||
markers.on('click', function (a) {
|
||||
if (a.layer instanceof L.MarkerCluster) {
|
||||
console.log('cluster ' + a.layer.getAllChildMarkers().length);
|
||||
//a.layer.zoomToBounds();
|
||||
var ms = a.layer.getAllChildMarkers();
|
||||
var p = [];
|
||||
for (var i = 0; i < ms.length; i++) {
|
||||
var ll = ms[i].getLatLng();
|
||||
p.push([ll.lat, ll.lng]);
|
||||
}
|
||||
var hull = L.QuickHull.getConvexHull(p);
|
||||
var hullll = [];
|
||||
for (var i = 0; i < hull.length; i++) {
|
||||
var p = hull[i];
|
||||
var ll = new L.LatLng(p[0][0], p[0][1]);
|
||||
hullll.push(ll);
|
||||
}
|
||||
|
||||
var path = new L.Polygon(hullll);
|
||||
map.addLayer(path);
|
||||
} else {
|
||||
console.log('marker ' + a.layer);
|
||||
}
|
||||
});
|
||||
|
||||
populate();
|
||||
//populateRandomVector();
|
||||
map.addLayer(markers);
|
||||
|
||||
L.DomUtil.get('populate').onclick = function () {
|
||||
var bounds = map.getBounds(),
|
||||
southWest = bounds.getSouthWest(),
|
||||
northEast = bounds.getNorthEast(),
|
||||
lngSpan = northEast.lng - southWest.lng,
|
||||
latSpan = northEast.lat - southWest.lat;
|
||||
var m = new L.Marker(new L.LatLng(
|
||||
southWest.lat + latSpan * 0.5,
|
||||
southWest.lng + lngSpan * 0.5));
|
||||
markersList.push(m);
|
||||
markers.addLayer(m);
|
||||
};
|
||||
L.DomUtil.get('remove').onclick = function () {
|
||||
markers.removeLayer(markersList.pop());
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,120 @@
|
||||
/* 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][0] - bl[0][0];
|
||||
var Vx = bl[0][1] - bl[1][1];
|
||||
return (Vx * (cpt[0] - bl[0][0]) + Vy * (cpt[1] - bl[0][1]))
|
||||
},
|
||||
|
||||
|
||||
findMostDistantPointFromBaseLine: function (baseLine, points) {
|
||||
var maxD = 0;
|
||||
var maxPt = new Array();
|
||||
var newPoints = new Array();
|
||||
for (var idx in points) {
|
||||
var pt = points[idx];
|
||||
var 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, points) {
|
||||
var convexHullBaseLines = new Array();
|
||||
var t = this.findMostDistantPointFromBaseLine(baseLine, points);
|
||||
if (t.maxPoint.length) { // 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 (points) {
|
||||
//find first baseline
|
||||
var maxX, minX;
|
||||
var maxPt, minPt;
|
||||
for (var idx in points) {
|
||||
var pt = points[idx];
|
||||
if (pt[0] > maxX || !maxX) {
|
||||
maxPt = pt;
|
||||
maxX = pt[0];
|
||||
}
|
||||
if (pt[0] < minX || !minX) {
|
||||
minPt = pt;
|
||||
minX = pt[0];
|
||||
}
|
||||
}
|
||||
var ch = [].concat(this.buildConvexHull([minPt, maxPt], points),
|
||||
this.buildConvexHull([maxPt, minPt], points))
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
}());
|
||||
|
||||
L.MarkerCluster.include({
|
||||
getConvexHull: function () {
|
||||
var childMarkers = this.getAllChildMarkers(),
|
||||
points = [],
|
||||
hullLatLng = [],
|
||||
hull, p, i;
|
||||
|
||||
for (var i = ms.lenght; i >= 0; i--) {
|
||||
ll = ms[i].getLatLng();
|
||||
points.push([ll.lat, ll.lng]);
|
||||
}
|
||||
|
||||
hull = L.QuickHull.getConvexHull(points);
|
||||
|
||||
for (var i = 0; i < hull.length; i++) {
|
||||
p = hull[i];
|
||||
hullLatLng.push(new L.LatLng(p[0][0], p[0][1]));
|
||||
}
|
||||
|
||||
return hullLatLng;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user