-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
148 lines (125 loc) · 3.3 KB
/
sketch.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
function setupLayout() {
canvasHeight = windowHeight - 100;
canvasWidth = windowHeight - 100;
createCanvas(canvasWidth, canvasHeight).parent('view');
select("#doStep").mousePressed(doStep);
select("#doRun").mousePressed(toggleRun);
select("#doRestart").mousePressed(resetProgress);
select("#doShuffle").mousePressed(setupVariables);
}
function setupVariables() {
cellWidth = Math.floor(canvasWidth / gridColumns);
cellHeight = Math.floor(canvasHeight / gridRows);
for (let y = 0; y < gridRows; y++) {
let columns = [];
for (let x = 0; x < gridColumns; x++) {
columns.push(new Cell(y, x));
}
grid[y] = columns;
}
resetProgress();
}
function resetProgress() {
openSet = [];
closedSet = [];
path = [];
currentCell = undefined;
startCell = grid[0][0];
targetCell = grid[gridRows - 1][gridColumns - 1];
startCell.isObstacle = false;
targetCell.isObstacle = false;
path.push(startCell);
openSet.push(startCell);
drawLayout();
}
function setup() {
frameRate(10);
setupLayout();
setupVariables();
}
function draw() {
if (run) {
doStep();
}
}
function doStep() {
if (currentCell === targetCell || openSet.length === 0) {
toggleRun(false);
return;
}
currentCell = findLowestMovementCost();
removeElementFromArray(openSet, currentCell);
closedSet.push(currentCell);
for (let neighbor of currentCell.findNeighbors()) {
if (neighbor.isObstacle || closedSet.includes(neighbor)) {
continue;
}
let movementCostToNeighbor = currentCell.gCost + currentCell.calculateDistance(neighbor);
if (movementCostToNeighbor >= neighbor.gCost && openSet.includes(neighbor)) {
continue;
}
neighbor.gCost = movementCostToNeighbor;
neighbor.hCost = neighbor.calculateDistance(targetCell);
neighbor.parent = currentCell;
if (openSet.includes(neighbor)) {
continue;
}
openSet.push(neighbor);
}
traceLatestPath(currentCell);
drawLayout();
}
function traceLatestPath(currentCell) {
path = [];
let cell = currentCell;
path.push(cell);
while (cell.parent) {
path.push(cell);
cell = cell.parent;
}
path.push(cell);
}
function drawLayout() {
background(0, 0, 0);
stroke(0);
for (let y = 0; y < gridRows; y++) {
for (let x = 0; x < gridColumns; x++) {
grid[y][x].draw();
}
}
drawPath();
}
function drawPath() {
noFill();
stroke(255);
strokeWeight(cellWidth / 4);
beginShape();
for (let i = 0; i < path.length; i++) {
vertex(path[i].y * cellWidth + cellWidth / 2, path[i].x * cellHeight + cellHeight / 2);
}
endShape();
}
function toggleRun(isRunning) {
run = isRunning ? isRunning : !run;
if (run) {
select("#doRun").html('Stop').addClass('active');
} else {
select("#doRun").html('Run').removeAttribute('class');
}
}
function removeElementFromArray(array, element) {
array.splice(array.indexOf(element), 1);
}
function findLowestMovementCost() {
let lowest = openSet[0];
for (let i = 1; i < openSet.length; i++) {
let current = openSet[i];
if (current.fCost() < lowest.fCost()) {
lowest = current;
}
if (current.fCost() === lowest.fCost() && current.hCost < lowest.hCost) {
lowest = current;
}
}
return lowest;
}