-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.pde
103 lines (89 loc) · 3.02 KB
/
Grid.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
/*
Implemented from https://github.com/mangecoeur/optboid/blob/master/simulation.py
*/
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.HashMap;
public class Grid {
int cellSize;
int divisions;
ArrayDeque<Boid> boids;
HashMap<PVector, ArrayDeque<Boid>> cells;
public Grid(float _width, float cellWidth) {
cellSize = (int)cellWidth;
boids = new ArrayDeque<Boid>();
divisions = (int)floor(_width / cellWidth);
cells = new HashMap<PVector, ArrayDeque<Boid>>();
for (int i = 0; i < divisions; i++) {
for (int j = 0; j < divisions; j++) {
cells.put(new PVector(i, j), new ArrayDeque<Boid>());
}
}
}
// Bind cells to edges of screen
private PVector getCellNum(int i, int j) {
int x = (int)(floor(i / cellSize));
int y = (int)(floor(j / cellSize));
x = constrain(x, 0, divisions - 1);
y = constrain(y, 0, divisions - 1);
return new PVector(x, y);
}
public ArrayDeque<Boid> getCellAt(int x, int y) {
return cells.get(getCellNum(x, y));
}
public ArrayDeque<Boid> getNeighbourCells(int x, int y) {
return cells.get(getCellNum(x, y));
}
public ArrayDeque<Boid> getNearCells(int x, int y, float influence) {
ArrayDeque<Boid> nearestGroup;
if (influence == 0) {
nearestGroup = getCellAt(x, y);
} else if (influence <= cellSize) {
nearestGroup = getNeighbourCells(x, y);
} else {
nearestGroup = getFarCells(x, y, ceil(influence / cellSize));
}
return nearestGroup;
}
public ArrayDeque<Boid> getFarCells(int x, int y, float influence) {
PVector cell = getCellNum(x, y);
int infl = (int)influence;
ArrayDeque<Boid> group = new ArrayDeque<Boid>();
for (int i = (int)cell.x - infl; i < (int)cell.x + infl; i++) {
for (int j = (int)cell.y - infl; j < (int)cell.y + infl; j++) {
PVector c = new PVector(i, j);
if (cells.containsKey(c)) {
group.addAll(cells.get(c));
}
}
}
return group;
}
// public void process() {
// for (ArrayDeque<Boid> boids : cells.values()) {
// for (Boid b : boids) {
// }
// }
// }
public void refreshCells() {
for (ArrayDeque<Boid> b : cells.values()) {
b.clear();
}
boids.removeIf(b -> b.isCaught);
for (Boid b : boids) {
getCellAt((int)b.pos.x,(int)b.pos.y).add(b);
}
}
public void drawGrid() {
for (PVector p : cells.keySet()) {
fill(255, 100, 50, 20 * cells.get(new PVector(p.x, p.y)).size());
rect(p.x * cellSize, p.y * cellSize, cellSize, cellSize);
}
}
float roundToN(float x, int n) {
return ceil(round(x / n)) * n;
}
public void reset() {
boids.clear();
}
}