-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordinate.js
94 lines (79 loc) · 1.62 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
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
/**
* Created by jonathan on 28/09/16.
*/
"use strict";
/**
* Coordinate of a "Demineur" cell
* @param x
* @param y
* @param maxX
* @param maxY
* @constructor
*/
function Coordinate(x,y, maxX, maxY) {
this.x = x;
this.y = y;
/**
* Ensure that x and y are valid value
*/
this.x = this.x < 0 ? 0 : this.x;
this.x = this.x > maxX ? maxX : this.x;
this.y = this.y < 0 ? 0 : this.y;
this.y = this.y > maxY ? maxY : this.y;
/**
* north coordinate
*/
this.north = () => {
return new Coordinate(this.x,this.y-1, maxX, maxY);
};
/**
* south coordinate
*/
this.south = () => {
return new Coordinate(this.x,this.y+1, maxX, maxY);
};
/**
* east coordinate
*/
this.east = () => {
return new Coordinate(this.x+1,this.y, maxX, maxY);
};
/**
* west coordinate
*/
this.west = () => {
return new Coordinate(this.x-1,this.y, maxX, maxY);
};
this.getId = () => {
return `${this.x}.${this.y}`;
}
}
/**
* Get a coordinate system
* [
* 0:[0,1,2,3,4]
* 1:[0,1,2,3,4]
* 2:[0,1,2,3,4]
* 3:[0,1,2,3,4]
* 4:[0,1,2,3,4]
* ]
*/
Coordinate.helperGridCoordinate = function(size) {
let coordinates = [];
for (let i = 0; i < size; i++) {
coordinates.push([]);
for (let j = 0; j < size; j++) {
coordinates[i].push({x:j,y:i});
}
}
return coordinates;
};
//for test purpose.
//do not care about this, newbie
try {
//stop watching, I said DO NOT CARE
module.exports = Coordinate;
}
catch (e) {
//nothing to do here
}