-
Notifications
You must be signed in to change notification settings - Fork 55
/
coordinate.js
67 lines (62 loc) · 2.28 KB
/
coordinate.js
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
class Coordinate{
constructor(longitude, latitude){
this.set(longitude, latitude);
}
set(long, lat){
this.longitude = ((long + 180) % 360 + 360) % 360 - 180;
this.latitude = constrain(lat, -90, 90);
}
add(long, lat){
if(long instanceof Coordinate){
lat = long.latitude;
long = long.longitude;
}
this.set(this.longitude + long, this.latitude + lat);
}
// for simplicity, returns the equirectangular map projection distance in "Pythagorean degrees" rather than true spherical distance, which is good enough for tropical cyclones far from the poles
dist(long, lat){
if(long instanceof Coordinate){
lat = long.latitude;
long = long.longitude;
}
let long_dist = abs(this.longitude - long);
long_dist = min(long_dist, 360 - long_dist);
let lat_dist = abs(this.latitude - lat);
return Math.hypot(long_dist, lat_dist);
}
static convertFromXY(mapType, x, y){
if(x instanceof p5.Vector)
({x, y} = x);
let west, east, north, south;
if(MAP_TYPES[mapType].form === 'earth')
({west, east, north, south} = MAP_TYPES[mapType]);
else
({west, east, north, south} = MAP_TYPES[6]); // default to Atlantic
if(east < west)
east += 360;
let long = map(x, 0, WIDTH, west, east, true);
let lat = map(y, 0, HEIGHT, north, south, true);
return new Coordinate(long, lat);
}
static convertToXY(mapType, long, lat){
if(long instanceof Coordinate){
lat = long.latitude;
long = long.longitude;
}
let west, east, north, south;
if(MAP_TYPES[mapType].form === 'earth')
({west, east, north, south} = MAP_TYPES[mapType]);
else
({west, east, north, south} = MAP_TYPES[6]); // default to Atlantic
let x, y;
if(east < west){
if(long > west)
x = map(long, west, east + 360, 0, WIDTH, true);
else
x = map(long, west - 360, east, 0, WIDTH, true);
}else
x = map(long, west, east, 0, WIDTH, true);
y = map(lat, north, south, 0, HEIGHT, true);
return createVector(x, y);
}
}