Skip to content

Instantly share code, notes, and snippets.

@kunalb
Created November 15, 2012 06:37
Show Gist options
  • Save kunalb/4077044 to your computer and use it in GitHub Desktop.
Save kunalb/4077044 to your computer and use it in GitHub Desktop.
// Copyright 2012-present Facebook. All Rights Reserved.
class Life extends LXPattern {
final float hBase = hue(fbBlue);
private int board[][] = new int[lx.height][lx.width];
private Click tick = new Click(500);
public Life(HeronLX lx) {
super(lx);
addModulator(tick).trigger();
PImage logo = loadImage("/Users/kunalb/life.png");
logo.loadPixels();
for (int i = 0; i < lx.total; i++) {
board[lx.row(i)][lx.column(i)] = (logo.pixels[i] != #000000) ? 1 : 0;
}
}
private int x(int a) {
return (a + lx.height) % lx.height;
}
private int y(int a) {
return (a + lx.width) % lx.width;
}
private void transition() {
int next[][] = new int[lx.height][lx.width];
for (int i = 0; i < lx.height; i++) {
for (int j = 0; j < lx.width; j++) {
int neighbours = 0;
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if (a !=0 || b != 0) {
neighbours += (board[x(i+a)][y(j+b)] > 0) ? 1 : 0;
}
}
}
if (board[i][j] > 0 && (neighbours == 2 || neighbours == 3)) {
next[i][j] = board[i][j]+1;
} else if (neighbours == 3) {
next[i][j] = 1;
} else {
next[i][j] = 0;
}
}
}
board = next;
}
public void run(int deltaMs) {
if (tick.getValue() == 1) {
transition();
}
for (int i = 0; i < lx.total; ++i) {
colors[i] = (board[lx.row(i)][lx.column(i)] > 0) ? #ff0000 : #000000;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment