-
Notifications
You must be signed in to change notification settings - Fork 16
/
Geo3x3.bancha
98 lines (91 loc) · 2.05 KB
/
Geo3x3.bancha
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
function log(x) {
native "console.log(x)";
}
operator ++ prefix 1 = "++{0}";
operator -- prefix 1 = "--{0}";
operator ++ postfix 1 = "{0}++";
operator -- postfix 1 = "{0}--";
operator - prefix 1 = "-{0}";
operator + prefix 1 = "+{0}";
operator * infixl 2 = "({0}*{1})";
operator / infixl 2 = "({0}/{1})";
operator % infixl 2 = "({0}%{1})";
operator + infixl 3 = "({0}+{1})";
operator - infixl 3 = "({0}-{1})";
operator < infixl 5 = "({0}<{1})";
operator > infixl 5 = "({0}>{1})";
operator <= infixl 5 = "({0}<={1})";
operator >= infixl 5 = "({0}>={1})";
operator == infixl 5 = "({0}=={1})";
operator != infixl 5 = "({0}!={1})";
operator := infixl 14 = "{0}={1}";
function floor(n) {
return native "Math.floor(n)";
}
function charAt(s, n) {
return native "s[n]";
}
function indexOf(s, key) {
return native "s.indexOf(key)";
}
function length(s) {
return native "s.length";
}
function encode(lat, lng, level) {
var res = "E";
if (lng < 0.0) {
res := "W";
lng := lng + 180.0;
}
lat := lat + 90; // 180:the North Pole, 0:the South Pole
var unit = 180.0;
for (var i = 1; i < level; i++) {
unit := unit / 3.0;
var x = floor(lng / unit);
var y = floor(lat / unit);
res := res + (x + y * 3 + 1);
lng := lng - x * unit;
lat := lat - y * unit;
}
return res;
}
function decode(code) {
var flg = false;
var begin = 0;
var c = charAt(code, 0);
if (c == "W") {
flg := true;
begin := 1;
} else {
if (c == "E") {
begin := 1;
} else {
return null;
}
}
var unit = 180.0;
var lat = 0.0;
var lng = 0.0;
var level = 1;
for (var i = begin; i < length(code); i++) {
var n = indexOf("0123456789", charAt(code, i));
if (n == 0) {
break;
}
if (n < 0) {
return null; // err
}
unit := unit / 3;
n--;
lng := lng + n % 3 * unit;
lat := lat + floor(n / 3) * unit;
level++;
}
lat := lat + unit / 2;
lng := lng + unit / 2;
lat := lat - 90.0;
if (flg) {
lng := lng - 180.0;
}
return [lat, lng, level, unit];
}