forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray2DTracer.js
65 lines (53 loc) · 1.27 KB
/
Array2DTracer.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
import { Tracer } from 'core/tracers';
import { Array2DRenderer } from 'core/renderers';
class Element {
constructor(value) {
this.value = value;
this.patched = false;
this.selected = false;
}
}
class Array2DTracer extends Tracer {
getRendererClass() {
return Array2DRenderer;
}
set(array2d = []) {
this.data = array2d.map(array1d => [...array1d].map(value => new Element(value)));
super.set();
}
patch(x, y, v = this.data[x][y].value) {
if (!this.data[x][y]) this.data[x][y] = new Element();
this.data[x][y].value = v;
this.data[x][y].patched = true;
}
depatch(x, y) {
this.data[x][y].patched = false;
}
select(sx, sy, ex = sx, ey = sy) {
for (let x = sx; x <= ex; x++) {
for (let y = sy; y <= ey; y++) {
this.data[x][y].selected = true;
}
}
}
selectRow(x, sy, ey) {
this.select(x, sy, x, ey);
}
selectCol(y, sx, ex) {
this.select(sx, y, ex, y);
}
deselect(sx, sy, ex = sx, ey = sy) {
for (let x = sx; x <= ex; x++) {
for (let y = sy; y <= ey; y++) {
this.data[x][y].selected = false;
}
}
}
deselectRow(x, sy, ey) {
this.deselect(x, sy, x, ey);
}
deselectCol(y, sx, ex) {
this.deselect(sx, y, ex, y);
}
}
export default Array2DTracer;