Skip to content

Instantly share code, notes, and snippets.

@kunalb
Created November 15, 2012 05:43
Show Gist options
  • Save kunalb/4076846 to your computer and use it in GitHub Desktop.
Save kunalb/4076846 to your computer and use it in GitHub Desktop.
// Copyright 2012-present Facebook. All Rights Reserved.
class Rangoli extends LXPattern {
private boolean board[][];
private int rule = 182;
private int leftCounter = 10;
private int seedDistance = 17;
final SinLFO yoffset = new SinLFO(1, 100, 5000);
final SinLFO xoffset = new SinLFO(1, 100, 5000);
final SawLFO ruleCount = new SawLFO(0, 3, 5000);
final SawLFO leftCounters = new SawLFO(0, 17, 1000);
public Rangoli(HeronLX lx) {
super(lx);
addModulator(xoffset).trigger();
addModulator(yoffset).trigger();
addModulator(ruleCount).trigger();
addModulator(leftCounters).trigger();
setTransition(new IrisTransition(lx).setDuration(5000));
board = new boolean[lx.height][lx.width];
// Seeding
for (int i = seedDistance; i < lx.width; i+=seedDistance) {
board[0][i] = true;
board[lx.height-1][i] = true;
}
updateBoard(rule);
}
private void updateBoard(int r) {
for (int i = 1; i < lx.height/2; ++i) {
for (int j = 0; j < lx.width; ++j) {
boolean top = board[i-1][j];
boolean topLeft = (j > 0)? board[i-1][j-1] : false;
boolean topRight = (j < lx.width - 1)? board[i-1][j+1] : false;
int bit = int(topRight) + 2 * int(top) + 4 * int(topLeft);
int test = 1 << bit;
if (boolean(test & r)) {
board[i][j] = true;
} else {
board[i][j] = false;
}
}
}
for (int i = lx.height-2; i >= lx.height/2; --i) {
for (int j = 0; j < lx.width; ++j) {
boolean bottom = board[i+1][j];
boolean bottomLeft = (j > 0)? board[i+1][j-1] : false;
boolean bottomRight = (j < lx.width - 1)? board[i+1][j+1] : false;
int bit = int(bottomRight) + 2 * int(bottom) + 4 * int(bottomLeft);
int test = 1 << bit;
if (boolean(test & r)) {
board[i][j] = true;
} else {
board[i][j] = false;
}
}
}
}
private void shiftLeft() {
for (int j = 0; j < lx.width; j++) {
board[0][j] = false;
board[lx.height-1][j] = false;
}
for (int i = (int)leftCounters.getValue(); i < lx.width; i+=seedDistance) {
board[0][i] = true;
board[lx.height-1][i] = true;
}
leftCounter = (leftCounter == 0)? seedDistance : leftCounter - 1;
}
public void run(int deltaMs) {
for (int i = 0; i < lx.total; ++i) {
if (board[lx.row(i)][lx.column(i)]) {
colors[i] = color(yoffset.getValuef(), lx.column(i)*10, lx.column(i) * lx.column(i));
} else {
colors[i] = #000000;
}
}
shiftLeft();
int rules[] = {182, 54, 190};
updateBoard(rules[(int)ruleCount.getValue()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment