-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixelGrid.js
160 lines (145 loc) · 3.45 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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;
}
}
ShiftRow(r, dx)
{
const step = Math.sign(dx);
for(var i = 0; i < Math.abs(dx); i++)
{
if(step < 0)
{
const left = this.grid[r][0];
this.grid[r].shift();
this.grid[r].push(left);
}
else {
const right = this.grid[r][this.grid[r].length - 1];
this.grid[r].pop();
this.grid[r].unshift(right);
}
}
}
ShiftCol(c, dy)
{
const step = Math.sign(dy);
for(var i = 0; i < Math.abs(dy); i++)
{
if(step < 0)
{
const top = this.grid[0][c];
for(var j = 0; j < this.grid.length - 1; j++)
this.grid[j][c] = this.grid[j + 1][c];
this.grid[this.grid.length - 1][c] = top;
}
else {
const bot = this.grid[this.grid.length - 1][c];
for(var j = this.grid.length - 1; j > 0; j--)
this.grid[j][c] = this.grid[j - 1][c];
this.grid[0][c] = bot;
}
}
}
WrapX(dx)
{
for(var i = 0; i < Math.abs(dx); i++)
{
for(var j = 0; j < this.grid.length; j++)
{
if(Math.sign(dx) < 0)
this.grid[j].push(this.grid[j].shift());
else
this.grid[j].unshift(this.grid[j].pop());
}
}
}
WrapY(dy)
{
console.log(Math.abs(dy))
for(var i = 0; i < Math.abs(dy); i++)
{
if(Math.sign(dy) < 0)
this.grid.push(this.grid.shift());
else
this.grid.unshift(this.grid.pop());
}
}
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;
}
}