Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created June 25, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/3a0c109baf6b6fd15282bae94182c465 to your computer and use it in GitHub Desktop.
Save KrabCode/3a0c109baf6b6fd15282bae94182c465 to your computer and use it in GitHub Desktop.
void showToast(final String message) {
//println(message);
runOnUiThread(new Runnable() {public void run() {android.widget.Toast.makeText(getContext(), message, android.widget.Toast.LENGTH_SHORT).show();}});
}
float[][] pile;
int scl = 10;
int offTop = 0;
int toEach = 0;
float offTopFreq = 0;
float toEachFreq = 0;
float offTopMin = 0;
float offTopMax = 0;
float toEachMin = 0;
float toEachMax = 0;
float startClr = 0;
float clrRange = 0;
int w = 0;
int h = 0;
public void setup() {
fullScreen();
noSmooth();
w = width/scl;
h = height/scl;
reset();
colorMode(HSB);
frameRate(30);
showToast("double tap to reset");
}
public void draw() {
offTop=round(map(sin(radians(frameCount*offTopFreq)), -1, 1, offTopMin, offTopMax));
toEach=round(map(cos(radians(frameCount*toEachFreq)), -1, 1, toEachMin, toEachMax));
update();
render();
// save("apache-" + frameCount+".png");
}
void render() {
fill(0);
noStroke();
rect(0, 0, width, height);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
float num = pile[x][y];
if (num > 0) {
int clr = color(
(startClr+(.5f+.5f*sin(radians(num)))*clrRange)%255
, 255
, 255
);
fill(clr);
noStroke();
rect(x*scl, y*scl, scl, scl);
}
}
}
}
void update() {
float[][] next = new float[w][h];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
float num = pile[x][y];
if (num < 4) {
next[x][y] = pile[x][y];
}
}
}
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
float num = pile[x][y];
if (num >= offTop) {
next[x][y] += (num - offTop);
if (x+1<w) next[x+1][y]+=toEach;
if (y+1<h) next[x][y+1]+=toEach;
if (x-1>=0) next[x-1][y]+=toEach;
if (y-1>=0) next[x][y-1]+=toEach;
}
}
}
pile = next;
}
long lastClicked = 0;
public void mousePressed() {
if (lastClicked > millis() - 500) {
reset();
}
lastClicked = millis();
}
void reset() {
pile = new float[width/scl][height/scl];
pile[w/2][h/2] = 1500000000;
setRandom();
showToast("reset");
println("----------");
println("emin: " + toEachMin);
println("emax: " + toEachMax);
println("efrq: " + toEachFreq);
println("omin: " + offTopMin);
println("omax: " + offTopMax);
println("ofrq: " + offTopFreq);
println("clrr: " + clrRange);
println("sclr: " + startClr);
}
private void setRandom() {
toEachMin = random(1f, 2f);
toEachMax = random(2f, 5f);
toEachFreq = random(3f, 15f);
offTopMin = random(2f, 4f);
offTopMax = random(15f, 25f);
offTopFreq = random( 3f, 12f);
clrRange = random( 64f,255f);
startClr = random(255f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment