forked from tejado/telegram-nearby-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnailShellMatrix.js
63 lines (51 loc) · 1.42 KB
/
SnailShellMatrix.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
class SnailShellMatrix {
constructor(gap = 1, x = 0, y = 0) {
this.gap = gap;
// overall position; 1 for first X, Y point
this.position = 1;
// level = amount of points at this line (starting with 2)
// increments every two lines
this.level = 2;
// levelStep = 0 for horizontal lines and 1 for vertical
// increments every line
this.levelStep = 1;
// levelPosition = current point at step
this.stepPosition = 1;
this.x = x;
this.y = y;
}
setGap(gap) {
if (gap > 0) {
this.gap = gap;
}
}
getNext() {
this.position++;
this.stepPosition++;
if (this.level % 2 === 0) {
if (this.levelStep === 1) {
this.x += this.gap;
} else {
this.y += this.gap;
}
} else {
if (this.levelStep === 1) {
this.x -= this.gap;
} else {
this.y -= this.gap;
}
}
let ret = [this.position, [this.x, this.y]];
if (this.stepPosition === this.level) {
if (this.levelStep === 1) {
this.levelStep++;
} else {
this.levelStep = 1;
this.level++;
}
this.stepPosition = 1;
}
return ret;
}
}
module.exports = SnailShellMatrix;