-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.js
103 lines (84 loc) · 2.52 KB
/
grid.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
95
96
97
98
99
100
101
102
103
/**
* Object to encapsulate double dimension array and accessors
*
* @param {int} width Number of columns
* @param {int} height Number of rows
*/
function Grid(width, height) {
this.width = width;
this.height = height;
this.space = new Array(height);
for (var i = 0; i < this.height; i++)
this.space[i] = new Array(width);
}
/**
* Set cell value
* @param {Vector} vector X,Y coordinate object
* @param {value} value New value for cell
*/
Grid.prototype.set = function set(vector, value) {
if (! this.isInside(vector))
return null;
this.space[vector.y][vector.x] = value;
};
/**
* Get cell value
* @param {Vector} vector X,Y coordinate object
* @return {boolean} Value of cell
*/
Grid.prototype.get = function get(vector) {
if (! this.isInside(vector))
return null;
return this.space[vector.y][vector.x];
};
/**
* Aggregate neighboring values
* @param {Vector} vector Coordinates
* @param {int} radius Depth of tessellation
* @return {Array} List of neighboring values
*/
Grid.prototype.getNeighbors = function getNeighbors(vector, radius) {
var neighbors = [];
for (var yOffset = -radius; yOffset <= radius; yOffset++) {
for (var xOffset = -radius; xOffset <= radius; xOffset++) {
if (xOffset == 0 && yOffset == 0)
continue;
var value = this.get( new Vector(vector.x + xOffset, vector.y + yOffset) );
if (value != undefined)
neighbors.push(value);
}
}
return neighbors;
}
/**
* Iterates through each cell assigning given function's return value
*
* @param {Function} fn Function to set cells' value
* @param {Object} ctx Context for 'this'
*/
Grid.prototype.fill = function fill(fn, ctx) {
for (var y = 0; y < this.height; y++)
for (var x = 0; x < this.width; x++)
this.space[y][x] = fn.call(ctx);
};
/**
* Iterates through each cell and calls a given function with cell value and
* coordinate vector as arguments
*
* @param {Function} fn Function to call on each cell
* @param {Object} ctx Context for 'this'
*/
Grid.prototype.forEach = function forEach(fn, ctx) {
for (var y = 0; y < this.height; y++)
for (var x = 0; x < this.width; x++)
fn.call(ctx, this.space[y][x], new Vector(x, y));
};
/**
* Checks if coordinates are within grid bounds
* @param {Vector} vector X,Y coordinate object
* @return {boolean} true or false
*/
Grid.prototype.isInside = function(vector) {
return (vector.x >= 0 && vector.x < this.width &&
vector.y >= 0 && vector.y < this.height);
};