-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.pde
110 lines (100 loc) · 3.24 KB
/
Functions.pde
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
// Heuristic function
float dist(Spot a, Spot b) {
//float d = abs(a.i - b.i) + abs(a.j - b.j); // Taxicab distance
float d = sqrt(pow(a.i - b.i, 2) + pow(a.j - b.j, 2)); // Euclidean distance
return d;
}
// Search for lowest fscore in OpenSet
int getwinner(ArrayList<Spot> array) {
Spot winner = array.get(0);
int index = 0;
for (int i = 0; i < array.size(); i++) {
if (array.get(i).fscore < winner.fscore) { // Reverse inequality to get the worst path :D
winner = array.get(i);
index = i;
}
}
return index;
}
boolean isinarraylist(ArrayList<Spot> array, Spot treasure) {
for (int i = 0; i < array.size(); i++) {
if (array.get(i).i == treasure.i && array.get(i).j == treasure.j) return true;
}
return false;
}
void recreatepath(Spot spot) {
color col = #F262E9;
spot.show(col);
if (spot.i == start.i && spot.j == start.j) {
//print("\nCost: ", cost);
//print("\nStopping");
return;
}
else {
int i = int(spot.camefrom_i);
int j = int(spot.camefrom_j);
Spot previous = grid[i][j];
cost += dist(spot, previous);
recreatepath(previous);
}
}
ArrayList<Spot> getneighbours(Spot curr) {
ArrayList<Spot> neighbours = new ArrayList<Spot>();
int i = int(curr.i);
int j = int(curr.j);
// i = 0 means first column
if (curr.i == 0 && curr.j < ncols-1) { // First column upto last row
print("\nCase 1");
if (curr.j != 0) {
neighbours.add(grid[i][j-1]); // One above
neighbours.add(grid[i+1][j+1]); // Top right
}
neighbours.add(grid[i][j+1]); // One below
neighbours.add(grid[i+1][j]); // One right
neighbours.add(grid[i+1][j+1]); // Bottom right
}
else if (curr.i == nrows-1 && curr.j < ncols-1) { // Last column upto last row
print("\nCase 2");
if (curr.j != 0) {
neighbours.add(grid[i][j-1]); // One above
neighbours.add(grid[i-1][j-1]); // Top left
}
neighbours.add(grid[i][j+1]); // One below
neighbours.add(grid[i-1][j]); // One left
neighbours.add(grid[i-1][j+1]); // Bottom left
}
else if (curr.i < nrows-1 && curr.j == 0) { // First row upto last column
print("\nCase 3");
if (curr.i != 0) {
neighbours.add(grid[i-1][j]); // One left
neighbours.add(grid[i-1][j+1]); // Bottom left
}
neighbours.add(grid[i][j+1]); // One below
neighbours.add(grid[i+1][j]); // One right
neighbours.add(grid[i+1][j+1]); // Bottom right
}
else if (curr.i <= nrows-1 && curr.j == ncols-1) { // Last row upto last column
print("\nCase 4");
if (curr.i != 0) {
neighbours.add(grid[i-1][j]); // One left
neighbours.add(grid[i-1][j-1]); // Top left
}
if (curr.i != nrows-1) {
neighbours.add(grid[i+1][j]); // One right
neighbours.add(grid[i+1][j-1]); // Top right
}
neighbours.add(grid[i][j-1]); // One above
}
else {
print("\nCase 5");
neighbours.add(grid[i][j+1]); // One below
neighbours.add(grid[i][j-1]); // One above
neighbours.add(grid[i+1][j]); // One right
neighbours.add(grid[i-1][j]); // One left
neighbours.add(grid[i+1][j+1]); // Bottom right
neighbours.add(grid[i+1][j-1]); // Top right
neighbours.add(grid[i-1][j+1]); // Top left
neighbours.add(grid[i-1][j-1]); // Bottom left
}
return neighbours;
}