-
Notifications
You must be signed in to change notification settings - Fork 15
/
particle_system.pde
94 lines (83 loc) · 2.13 KB
/
particle_system.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
public class ParticleSystem {
Cell[][] cells;
PImage img;
int cellWidth;
int cellHeight;
ParticleSystem(PApplet app, PImage img) {
this.cellWidth = width / (int)CELL_SIZE;
this.cellHeight = height / (int)CELL_SIZE;
this.img = img;
this.cells = new Cell[this.cellWidth][this.cellHeight];
for (int x = 0; x < this.cellWidth; x++) {
for (int y = 0; y < this.cellHeight; y++) {
this.cells[x][y] = new Cell(this, x, y);
}
}
for (int x = 0; x < this.cellWidth; x++) {
for (int y = 0; y < this.cellHeight; y++) {
this.cells[x][y].neighbors = this.neighbors(x, y);
}
}
}
public void plan() {
for (Cell[] row : this.cells) {
for (Cell cell : row) {
cell.plan();
}
}
}
public void move() {
for (Cell[] row : this.cells) {
for (Cell cell : row) {
cell.move();
}
}
}
public void draw() {
for (Cell[] row : this.cells) {
for (Cell cell : row) {
cell.draw();
}
}
}
void add(float x, float y) {
Particle p = new Particle(x, y);
this.place(p);
}
void place(Particle p) {
int cell_x = floor(p.origin.x / CELL_SIZE);
int cell_y = floor(p.origin.y / CELL_SIZE);
if (cell_x < 0 || cell_x >= this.cellWidth) {
return;
}
if (cell_y < 0 || cell_y >= this.cellHeight) {
return;
}
Cell cell = this.cells[cell_x][cell_y];
cell.add(p);
}
Cell[] neighbors(int x, int y) {
Cell[] cells = {
this.getCell(x-1, y-1),
this.getCell(x , y-1),
this.getCell(x+1, y-1),
this.getCell(x-1, y ),
this.getCell(x+1, y ),
this.getCell(x-1, y+1),
this.getCell(x , y+1),
this.getCell(x+1, y+1)
};
return cells;
}
Cell getCell(int x, int y) {
if (x < 0 || x >= this.cellWidth || y < 0 || y >= this.cellWidth) {
return null;
}
return this.cells[x][y];
}
color getColor(float x, float y) {
int _x = floor(map(x, 0, width, 0, this.img.width));
int _y = floor(map(y, 0, height, 0, this.img.height));
return this.img.pixels[_y * this.img.width + _x];
}
}