-
Notifications
You must be signed in to change notification settings - Fork 6
/
sample.html
298 lines (266 loc) · 10.8 KB
/
sample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<html>
<head>
<title>Leaflet</title>
<link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.css">
<script type='text/javascript' src="leaflet-src.js"></script>
<script type='text/javascript' src="distance.js"></script>
<style type="text/css">
.label {
margin-top: 0px !important;
font-size: 10px !important;
}
</style>
</head>
<body>
<div style="width:100%; height:100%" id="map"></div>
<script type='text/javascript'>
var map = new L.Map('map', {zoom: 10});
//var map = new L.Map('map', {center: points[100].coord, zoom: 12});
map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));
var DEBUG = false;
var markers = [];
var poly = {};
function clearMarks(){
markers.forEach(function(m){map.removeLayer(m)});
markers = [];
}
function mark(point, radius, color)
{
var m = L.circleMarker(point,{color: color, radius: radius}).addTo(map);
markers.push(m);
}
function drawHull(hull, map){
map.removeLayer(poly);
poly = L.polyline(hull, {color: 'red'}).addTo(map);
}
// concave hull
Array.prototype.getUnique = function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i].toString())) {
continue;
}
a.push(this[i]);
u[this[i].toString()] = 1;
}
return a;
}
Array.prototype.findMinYPoint = function(){
var minI = this.length;
var min = 360;
for(i = 0; i < this.length; i++) {
if(min > this[i].lat){
min = this[i].lat;
minI = i;
}
}
return this[minI];
}
Array.prototype.remove = function(obj){
this.splice(this.indexOf(obj), 1);
}
var angles = [];
var prevAngle = 0;
function fixAngle(angle){
var delta = 0.001;
if(angle < 0)
angle = 2*Math.PI + angle;
if(angle >= 2*Math.PI)
angle = angle - 2*Math.PI;
if((angle > Math.PI/2 && angle - Math.PI/2 < delta ) || (Math.PI/2 > angle && Math.PI/2 - angle < delta))
angle = Math.PI/2;
if((angle > Math.PI && angle - Math.PI < delta ) || (Math.PI > angle && Math.PI - angle < delta))
angle = Math.PI;
if((angle > Math.PI*3/4 && angle - Math.PI*3/4 < delta ) || (Math.PI*3/4 > angle && Math.PI*3/4 - angle < delta))
angle = Math.PI*3/4;
return angle;
}
L.LatLng.prototype.angleTo = function(p) {
var p1 = L.CRS.EPSG3395.latLngToPoint(this, 18);
var p2 = L.CRS.EPSG3395.latLngToPoint(p, 18);
var angle = 2*Math.PI - Math.atan2(p2.x - p1.x, p2.y - p1.y) - Math.PI/2;
return fixAngle(angle)
}
Array.prototype.sortByAngle = function (point){
angles = [];
this.sort(function(a,b){
return fixAngle((Math.PI - prevAngle) + point.angleTo(b)) - fixAngle((Math.PI - prevAngle) + point.angleTo(a));
});
for(i = 0; i < this.length; i++)
angles.push(this[i].name + ': '+ point.angleTo(this[i])*180/Math.PI+'°')
}
L.LatLng.prototype.nearestPoints = function(points, count){
var /* L.LatLng */ source = this;
points.sort(function(a, b){
return source.distanceTo(a) - source.distanceTo(b)
});
return points.slice(0, count);
}
Array.prototype.pointInPolygon = function (point) {
// (c) https://github.com/substack/point-in-polygon
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
var x = point.lng, y = point.lat;
var inside = false;
for (var i = 0, j = this.length - 1; i < this.length; j = i++) {
var xi = this[i].lng, yi = this[i].lat;
var xj = this[j].lng, yj = this[j].lat;
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
function ccw(A, B, C){
var cccw = (C.y-A.y)*(B.x-A.x) - (B.y-A.y)*(C.x-A.x);
return cccw > 0.0 ? 1 : cccw < 0.0 ? -1 : 0;
}
function intersectQ(/* LatLng */ a1, a2, b1, b2){
var A = L.CRS.EPSG3395.latLngToPoint(a1, 18);
var B = L.CRS.EPSG3395.latLngToPoint(a2, 18);
var C = L.CRS.EPSG3395.latLngToPoint(b1, 18);
var D = L.CRS.EPSG3395.latLngToPoint(b2, 18);
return (ccw(A,C,D) != ccw(B,C,D) && ccw(A,B,C) != ccw(A,B,D));
}
Array.prototype.concaveHull = function(k, map){
if(k < 3)
return [];
var dataset = this.getUnique();
if(this.length <= 3)
return this;
var kk = Math.min(k,dataset.length-1);
var firstPoint = dataset.findMinYPoint();
var hull = [];
hull.push(firstPoint);
var currentPoint = firstPoint;
//prevPoint = currentPoint;
prevAngle = 0;
//if(DEBUG) L.circleMarker(currentPoint,{color:"#F00", radius:3}).addTo(map);
dataset.remove(firstPoint);
var step = 1;
while (((currentPoint != firstPoint) || (step == 1)) && (dataset.length > 0)){
if (step == 4){
dataset.push(firstPoint)
}
var cPoints = currentPoint.nearestPoints(dataset,kk);
cPoints.sortByAngle(currentPoint);
//if(DEBUG){
// clearMarks();
// for(xx=0; xx < cPoints.length; xx++)mark(cPoints[xx], 2*(xx+1), "#F0F");
//}
var its = true;
//if(DEBUG) console.log('step:'+step);
for(i = 0; its && i < cPoints.length; i++){//check all candidates if their segments cross any prev segment in hull
its = false;
if(cPoints[i] == firstPoint){
i++;
break;
}
for(j = step - 2; !its && j > 0; j--){ //check with all prev hull segments, except the last
its = intersectQ(hull[step - 1], cPoints[i], hull[j], hull[j - 1]);
//if(DEBUG){ console.log('i:'+i+',j:'+j+') '+hull[step - 1].name + '-'+cPoints[i].name
// +' x '+hull[j].name+'-'+hull[j - 1].name + ': '+its);
}
}
if(its)//since all candidates intersect at least one edge, try again with a higher number of neighbours
return this.concaveHull(kk+1, map);
currentPoint = cPoints[i - 1];
hull.push(currentPoint);//a valid candidate was found
if(DEBUG) drawHull(hull, map);
prevAngle = hull[step - 1].angleTo(hull[step]);
dataset.remove(currentPoint);
step++;
i++;
}
var allInside = true;
i = dataset.length;
while (allInside && i > 0) {// check if all the given points are inside the computed polygon
i--;
allInside = hull.pointInPolygon(dataset[i]);
//if(DEBUG) console.log(dataset[i].name + " inside: "+allInside);
}
if(!allInside)
return this.concaveHull(kk+1, map); //since at least one point is out of the computed polygon, try again with a higher number of neighbours
return hull;
}
// convex hull
Array.prototype.findLeftMostLowestPoint = function(){
var idx = 0;
for (var i = 1; i < this.length; ++i)
{
var a = this[i];
var b = this[idx];
if(a.lng < b.lng || (a.lng == b.lng && a.lat < b.lat))
idx = i;
}
return idx;
}
L.LatLng.prototype.isToLeft = function(b, c){
var u1 = b.lng - this.lng;
var v1 = b.lat - this.lat;
var u2 = c.lng - this.lng;
var v2 = c.lat - this.lat;
return u1 * v2 - v1 * u2 < 0;
}
Array.prototype.convexHull = function()
{
// Gift wrap method
var out = [];
var startIdx = this.findLeftMostLowestPoint();
var hull = startIdx;
var npts = this.length;
var endpt = 0;
do
{
out.push(this[hull]);
//if(DEBUG) drawHull(out, map);
endpt = 0;
for (var j = 1; j < npts; ++j)
{
if (hull == endpt || this[hull].isToLeft(this[endpt], this[j]))
endpt = j;
}
hull = endpt;
} while (endpt != startIdx);
out.push(this[endpt]); // close the poly
return out;
}
// end convex hull
var pts = [];
for(var i = 0; i < points.length; i++){
var p = new L.latLng(points[i].coord);
var p1 = new L.latLng(points[i].road);
p.name = i;
p1.name = 'r'+i;
pts.push(p);
pts.push(p1);
}
var pt = pts.getUnique();
for(i = 0; i < pt.length; i++){
var rr = L.circleMarker(pt[i],{title:i,color:"#000", radius:1})
.addTo(map);
var myIcon = L.divIcon({className:'label', html:''+i});
//L.marker(pt[i], {icon: myIcon}).addTo(map);
//var tt = new L.Marker.Text(pts[i], i).addTo(map);
}
map.fitBounds(L.polygon(pt).getBounds());
L.polygon(pt.concaveHull(3, map), {color: "#00F", weight:1}).addTo(map);
//L.polygon(pts.convexHull(map), {color: "#00F", weight:1}).addTo(map);
//console.log('0-2 = 90: ' + pt[0].angleTo(pt[2])*180/Math.PI);
//console.log('4-9 = 45: ' + pt[4].angleTo(pt[9])*180/Math.PI);
//console.log('12-19 = 135: ' + pt[12].angleTo(pt[19])*180/Math.PI);
//console.log('4-5 x 9-8 (false): ' + intersectQ(pt[4], pt[5], pt[9], pt[8]));
//console.log('5-9 x 8-4 (false): ' + intersectQ(pt[5], pt[9], pt[8], pt[4]));
//console.log('4-9 x 5-8 (true ): ' + intersectQ(pt[4], pt[9], pt[5], pt[8]));
//console.log('8-5 x 9-4 (true ): ' + intersectQ(pt[8], pt[5], pt[9], pt[4]));
//console.log('0 - 14 x 21-3 (true ): ' + intersectQ(pt[0], pt[14], pt[21], pt[3]));
//console.log('0 - 14 x 21-1 (true ): ' + intersectQ(pt[0], pt[14], pt[21], pt[1]));
//console.log('176-194 x 209-177 (true ): ' + intersectQ(pt[176], pt[194], pt[209], pt[177]));
//console.log('194-176 x 209-177 (true ): ' + intersectQ(pt[194], pt[176], pt[209], pt[177]));
//console.log('0-2 x 2-3 (touch ): ' + intersectQ(pt[0], pt[2], pt[2], pt[3]));
//console.log('5-3 x 4-8 (touch ): ' + intersectQ(pt[5], pt[3], pt[4], pt[8]));
//console.log('141-176 x 125-110 (false): ' + intersectQ(pt[141], pt[176], pt[125], pt[110]));
//console.log('39-52 x 28-19 (false): ' + intersectQ(pt[39], pt[52], pt[28], pt[19]));
</script>
</body>
</html>