Waving Cell
https://gyazo.com/0ce95c60033b7490a2249fc595b26b6b
code:java
Cell[][] _cellArray;
int _cellSize = 10;
int _numx, _numy;
int picnum = 0;
void setup() {
size(500, 300);
_numx = floor(width/_cellSize);
_numy = floor(height/_cellSize);
restart();
}
void restart() {
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
Cell newCell = new Cell(x, y);
}
}
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
int above = y-1;
int below = y+1;
int left = x-1;
int right = x+1;
if (above < 0) { above = _numy-1; }
if (below == _numy) { below = 0; }
if (left < 0) { left = _numx-1; }
if (right == _numx) { right = 0; }
_cellArrayxy.addNeighbour(_cellArraylefty); _cellArrayxy.addNeighbour(_cellArrayxabove); _cellArrayxy.addNeighbour(_cellArrayxbelow); _cellArrayxy.addNeighbour(_cellArrayrighty); }
}
}
void draw() {
background(200);
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
_cellArrayxy.calcNextState(); }
}
translate(_cellSize/2, _cellSize/2);
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
}
}
if (picnum++ < 64) {
saveFrame("###.jpg");
}
}
void mousePressed() {
restart();
}
class Cell {
float x, y;
float state;
float nextState;
float lastState = 0;
Cell[] neighbors;
Cell(float ex, float why) {
x = ex * _cellSize;
y = why * _cellSize;
nextState = ((x/500) + (y/300)) * 14;
state = nextState;
}
void addNeighbour(Cell cell) {
neighbors = (Cell[])append(neighbors, cell);
}
void calcNextState() {
float total = 0;
for (int i = 0; i < neighbors.length; i++) {
total += neighborsi.state; }
float average = int(total/8);
if (average == 255) {
nextState = 0;
} else if (average == 0) {
nextState = 255;
} else {
nextState = state + average;
if (lastState > 0) {
nextState -= lastState;
}
if (nextState > 255) {
nextState = 255;
} else if (nextState < 0) {
nextState = 0;
}
}
lastState = state;
}
void drawMe() {
state = nextState;
stroke(0);
fill(state);
ellipse(x, y, _cellSize, _cellSize);
}
}