-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.kn
78 lines (72 loc) · 1.5 KB
/
geo3x3.kn
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
+func encode(lat: float, lng: float, level: int): []char
var result: []char
if(level < 1)
do result :: ""
ret result
end if
if(lng >= 0.0)
do result :: "E"
else
do result :: "W"
do lng :+ 180.0
end if
do lat :+ 90.0 {180:the North Pole, 0:the South Pole}
var unit: float :: 180.0
var i: int :: 1
var array: []char
while(i < level)
do unit :/ 3.0
var x: int :: (lng / unit) $ int
var y: int :: (lat / unit) $ int
do array :: "\{x + y * 3 + 1}"
do result :: result ~ array
do lng :- x $ float * unit
do lat :- y $ float * unit
do i :+ 1
end while
ret result
end func
+func decode(code: []char): []float
var clen: int :: ^code
if(clen = 0)
ret null
end if
var begin: int :: 0
var flg: bool :: false
var c: char :: code[0]
if(c = '-' | c = 'W')
do flg :: true
do begin :: 1
else
if(c = '+' | c = 'E')
do begin :: 1
end if
end if
var unit: float :: 180.0
var lat: float :: 0.0
var lng: float :: 0.0
var level: int :: 1
var i: int :: begin
var array: []char :: "0123456789"
var n: int
while a(i < clen)
do n :: array.find(code[i], -1)
if(n <= 0)
break a
end if
do unit :/ 3.0
do n :- 1
do lng :+ n $ float % 3.0 * unit
do lat :+ ((n / 3) $ int) $ float * unit
do level :+ 1
do i :+ 1
end while
do lat :+ unit / 2.0
do lng :+ unit / 2.0
do lat :- 90.0
if(flg)
do lng :- 180.0
end if
var return: []float :: [lat, lng, level $ float, unit]
ret return
end func