How about instead of running a check from every square, run a check close to the mouse. This gives you a square area to check. Then you can ask if those points are within the circular distance to the mouse pointer, giving you the round check zone. The function I've written looks pretty exhaustive but it's dealing with a lot of stuff that you could initialise before hand, and I'm making sure the checking square doesn't step outside the array.
Code:
int _width = 500; // stagewidth
int _height = 500; // stageheight
int cellSize = 10; // resolution
int _w = _width/cellSize;
int _h = _height/cellSize;
int MAX = int(sq(_w)); // calculate number of cells
int near = 30; // affected area from mousepointer
float as = 15; // ageing speed
Cell [] cells = new Cell[MAX];
void setup() {
size(500,500);
background(255);
noStroke();
for (int i = 0; i < MAX; i++)
cells[i] = new Cell((i%_w)*cellSize, (i/_w)*cellSize, cellSize);
rectMode(CENTER);
}
void draw() {
background(255);
for (int i = 0; i < MAX; i++) {
cells[i].draw();
}
burn(int(mouseX/cellSize),int(mouseY/cellSize),near/cellSize);
}
void burn(int x, int y, int radius){
//cast square radius
int topLeftX = x - radius;
int topLeftY = y - radius;
int bottomRightX = x + radius;
int bottomRightY = y + radius;
if(topLeftX < 0) topLeftX = 0;
if(topLeftY < 0) topLeftY = 0;
if(bottomRightX >= _w) bottomRightX = _w - 1;
if(bottomRightY >= _h) bottomRightY = _h - 1;
int wide = bottomRightX - topLeftX;
int high = bottomRightY - topLeftY;
int cellsInside = wide * high;
int [] burnCells = new int [cellsInside];
int i = 0;
for(int row = topLeftX + topLeftY * _w; row < topLeftX + (topLeftY + high) * _w; row += _w){
for(int col = 0; col < wide; col++){
burnCells[i++] = row + col;
}
}
for(i = 0; i < burnCells.length; i++){
cells[burnCells[i]].stoke();
}
}
class Cell {
float xpos;
float ypos;
int xsize;
int ysize;
float r = 255;
float g = 255;
float b = 255;
Cell(float xp, float yp, int _size) {
xpos = xp;
ypos = yp;
xsize = _size;
ysize = xsize;
}
void draw () {
fill(r,g,b);
rect(xpos,ypos,xsize,ysize);
}
void stoke(){
if ((dist(xpos, ypos, mouseX, mouseY) < near) && (b >= 0)) {
r-=as;
b-=as;
}
if ((dist(xpos, ypos, mouseX, mouseY) < near) && (b < 0)) {
r+=as;
}
if ((dist(xpos, ypos, mouseX, mouseY) < near) && (b < 0) && (r >= 255) ) {
g-=as;
}
}
}