[Pkg-javascript-commits] [leaflet-markercluster] 30/479: 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

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:05 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet-markercluster.

commit e32c4de14cd65fab7a385e135da6641498997d6c
Author: danzel <danzel at localhost.geek.nz>
Date:   Wed Jul 18 10:52:47 2012 +1200

    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
---
 example/marker-clustering-convexhull.html | 110 +++++++++++++++++++++++++++
 src/MarkerCluster.QuickHull.js            | 120 ++++++++++++++++++++++++++++++
 2 files changed, 230 insertions(+)

diff --git a/example/marker-clustering-convexhull.html b/example/marker-clustering-convexhull.html
new file mode 100644
index 0000000..6e4531b
--- /dev/null
+++ b/example/marker-clustering-convexhull.html
@@ -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>
diff --git a/src/MarkerCluster.QuickHull.js b/src/MarkerCluster.QuickHull.js
new file mode 100644
index 0000000..b2afe46
--- /dev/null
+++ b/src/MarkerCluster.QuickHull.js
@@ -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;
+	}
+});
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-markercluster.git



More information about the Pkg-javascript-commits mailing list