-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrailmap.pde
92 lines (78 loc) · 1.8 KB
/
trailmap.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
class trailmap{
float[][] grid;
float[][] buffer;
float decayRate;
trailmap(){
grid = new float[width][height];
buffer = new float[width][height];
decayRate = 0.59;
}
void draw(){
loadPixels();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int i = y * width + x;
pixels[i] = color(t.grid[x][y]);
}
}
updatePixels();
}
void diffuse(){
loadPixels();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
float sum = 0;
float avg = 0;
float total = 0;
for(int kx = -1; kx < 2; kx++){
for(int ky = -1; ky < 2; ky++){
int currentX, currentY;
//wrap x;
if(x==width-1){
currentX = 0;
} else if(x==0){
currentX = width-1;
} else {
currentX = kx+x;
}
//wrap y
if(y==height-1){
currentY = 0;
} else if(y==0){
currentY = height-1;
} else {
currentY = ky+y;
}
float intensity = grid[currentX][currentY];
sum += intensity;
total ++;
}
}
avg = sum / 9.0;
//write average to a buffer grid
buffer[x][y] = avg;
}
}
//write buffer grid onto draw grid
for(int x = 0; x < width; x++){
for(int y = 0; y< height; y++){
grid[x][y] = buffer[x][y];
}
}
clearBuffer();
}
void decay(){
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
grid[x][y] *= decayRate;
}
}
}
void clearBuffer(){
for(int x = 0; x<width;x++){
for(int y = 0; y< height; y++){
buffer[x][y] = 0;
}
}
}
}