-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmatrix.js
More file actions
109 lines (86 loc) · 3.47 KB
/
matrix.js
File metadata and controls
109 lines (86 loc) · 3.47 KB
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
/**
* Created by oliveira on 23/08/16.
*/
/**
* Created by oliveira on 09/08/16.
*/
var cell= require("./cell");
var location=require("./location");
var colors = require('colors');
var matrix= function (step) {
var begin_lat=-90;
var begin_lon=-180;
this.m={};
var counter=-90.01800;
for(i=0;i<180/step;i++){
//console.log("lat: "+begin_lat+" - "+((begin_lat+step)));
for(j=0;j<360/step;j++){
//console.log(" lon: "+begin_lon+" - "+(begin_lon+step));
this.m[truncateDecimals(counter,4)]=new cell(truncateDecimals(counter,4),new location(begin_lat,begin_lon),step);
step1=step;
//l=this.m[counter].locin;
//console.log(JSON.stringify(l));
begin_lon=this.m[truncateDecimals(counter,4)].locout.point.longitude();
if(begin_lon>180)
begin_lon=180;
counter+=0.0001;
}
begin_lat=this.m[truncateDecimals(counter-0.0001,4)].locout.point.latitude();
if(begin_lat>90)
begin_lat=90;
begin_lon=-180;
counter+=1;
counter=Number((counter).toFixed(0));
counter-=0.01800;
}
console.log("matrix incited correctly");
};
matrix.prototype.getCells=function (cid) {
//console.log(colors.red("not tested , definely will not work"));
return [this.m[cid-1.0001],this.m[cid-1],this.m[cid+0.0009],this.m[cid-0.0001],this.m[cid],this.m[cid+0.0001],this.m[cid+1.0001],this.m[cid+1],this.m[cid+0.0009]]
};
matrix.prototype.disToPoints=function (cells, point,max) {
dists={};
//console.log(JSON.stringify(cells));
for(i=0;i<cells.length;i++) {
if(cells[i] != null) {
//console.log("---------> "+i+" : "+cells[i].cell_id+" : "+cells[i].users.length);
for (j=0;j<cells[i].points.length;j++) {
//console.log(" |---------> uid:"+cells[i].users[j].uid+" dist:" + user.location.point.distanceTo(cells[i].users[j].location.point, true));
if (point.location.point.distanceTo(cells[i].points[j].location.point, true) < max)
dists[cells[i].points[j].uid] = point.location.point.distanceTo(cells[i].points[j].location.point, true);
}
}
}
return dists;
};
matrix.prototype.disToPoint=function (points, point, max) {
dists={};
for (j=0;j<points.length;j++) {
if (point.location.point.distanceTo(points[j].location.point, true) < max)
dists[points[j].uid] = point.location.point.distanceTo(points[j].location.point, true);
}
return dists;
};
matrix.prototype.getCellOfPoint=function (point) {
lat = point.location.point.latitude();
lon = point.location.point.longitude();
cell_id = (parseFloat(Number(lat).toFixed(0))+Number(lat).toFixed(0)/10000)
return cell_id;
};
matrix.prototype.getCellOfLoc=function (loc) {
//console.log("nao funciona ate se corrigir o erro de truncatura");
lat = loc.point.latitude();
lon = loc.point.longitude();
//console.log(Number(lat).toFixed(0)+" "+(Number(lat).toFixed(0)/10000))
cell_id = (parseFloat(Number(lat).toFixed(0))+Number(lat).toFixed(0)/10000)
//console.log(cell_id);
return this.m[cell_id];
};
truncateDecimals = function (number, digits) {
var multiplier = Math.pow(10, digits),
adjustedNum = number * multiplier,
truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);
return truncatedNum / multiplier;
};
module.exports=matrix;