-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixelGrid.js
94 lines (83 loc) · 1.96 KB
/
PixelGrid.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
class PixelGrid
{
constructor(w, h)
{
this.grid = Tools.Arr2d(w, h, Constants.COLOR_CLEAR);
this.width = w;
this.height = h;
}
IsEmpty()
{
for(var y = 0; y < this.grid.length; y++)
for(var x = 0; x < this.grid[y].length; x++)
if(this.grid[y][x] !== Constants.COLOR_CLEAR)
return false;
return true;
}
FlipX()
{
this.grid.forEach(row =>
{
for(var x = 0; x < row.length / 2; x++)
{
const temp = row[x];
row[x] = row[row.length - 1 - x];
row[row.length - 1 - x] = temp;
}
});
}
FlipY()
{
for(var y = 0; y < this.grid.length / 2; y++)
{
const temp = this.grid[y];
this.grid[y] = this.grid[this.grid.length - 1 - y];
this.grid[this.grid.length - 1 - y] = temp;
}
}
ShiftX(dx)
{
if(dx < 0)
this.grid.forEach(row => { row.shift(); row.push(Constants.COLOR_CLEAR); });
else
this.grid.forEach(row => { row.pop(); row.unshift(Constants.COLOR_CLEAR); });
}
ShiftY(dy)
{
const temp = new Array(this.grid[0].length).fill(Constants.COLOR_CLEAR);
if(dy < 0)
{
this.grid.shift();
this.grid.push(temp);
}
else
{
this.grid.pop();
this.grid.unshift(temp);
}
}
InBounds(x, y)
{
return (y > 0 && y < this.height && x > 0 && x < this.width);
}
Clear()
{
this.grid = Tools.Arr2d(w, h, Constants.COLOR_CLEAR);
}
Resize(w, h)
{
let temp = Tools.Arr2d(w, h, Constants.COLOR_CLEAR);
for(var y = 0; y < Math.min(h, this.height); y++)
for(var x = 0; x < Math.min(w, this.width); x++)
temp[y][x] = this.grid[y][x];
this.width = w;
this.height = h;
this.grid = temp;
}
Copy()
{
let temp = new PixelGrid(this.width, this.height);
this.grid.forEach((r, y) => r.forEach((c, x) => temp.grid[y][x] = c));
return temp;
}
}