-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathareautil.ts
64 lines (61 loc) · 1.72 KB
/
areautil.ts
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
export interface Location {
latitude: number;
longitude: number;
}
export function inArea(bounds: Array<Location>, point: Location): boolean {
let lonA: number;
let latA: number;
let lonB: number;
let latB: number;
let inside = 0;
for(let i = 0; i < bounds.length; i++) {
lonA = bounds[i].longitude - point.longitude;
latA = bounds[i].latitude - point.latitude;
lonB = bounds[(i + 1) % bounds.length].longitude - point.longitude;
latB = bounds[(i + 1) % bounds.length].latitude - point.latitude;
const check = polygonPointCheck(lonA, latA, lonB, latB);
if(check === 4) {
return true;
}
inside += check;
}
return (inside !== 0);
}
export function polygonPointCheck(lonA: number,
latA: number,
lonB: number,
latB: number): number {
if(latA * latB > 0) {
return 0;
}
if((lonA * latB !== lonB * latA) || (lonA * lonB > 0)) {
if(latA * latB < 0) {
if(latA > 0) {
if(latA * lonB >= lonA * latB) {
return 0;
}
return -2;
}
if(lonA * latB >= latA * lonB) {
return 0;
}
return 2;
}
if(latB === 0) {
if(latA === 0) {
return 0;
} else if(lonB > 0) {
return 0;
} else if(latA > 0) {
return -1;
}
return 1;
} else if(lonA > 0) {
return 0;
} else if(latB > 0) {
return 1;
}
return -1;
}
return 4;
}