Last active
December 21, 2015 10:09
-
-
Save sczizzo/6290005 to your computer and use it in GitHub Desktop.
Electric rain / static shock / thunder mountain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int COARSENESS = 4; | |
ArrayList vertices; | |
float bump(float val) { | |
return val + random(-1.0, 1.0); | |
} | |
class Vertex { | |
int x, y; | |
float val, target; | |
Vertex(int _x, int _y, float _val) { | |
x = _x; y = _y; val = _val; | |
} | |
int off = COARSENESS / 2; | |
void draw() { | |
noStroke(); | |
if (val < 0.25) fill(0, abs(val * 255.0) % 256, 0); | |
else if (val < 0.75) fill(0, 0, abs(val * 255.0) % 256); | |
else fill(abs(val * 255.0) % 256, 0, 0); | |
rect(x - off, y - off, COARSENESS, COARSENESS); | |
} | |
void mutate(Vertex north, Vertex south, Vertex east, Vertex west) { | |
if (abs(target - val) < 0.5) { | |
float vNorth = bump(north.val); | |
float vSouth = bump(south.val); | |
float vEast = bump(east.val); | |
float vWest = bump(west.val); | |
target = (vNorth + vSouth + vEast + vWest) / 4.0; | |
val = target; | |
} else { | |
val += (target - val) / 2; | |
} | |
} | |
} | |
void setup() { | |
frameRate(60); | |
size(displayWidth, displayHeight); | |
vertices = new ArrayList<Vertex>(); | |
int off = COARSENESS / 2; | |
for (int x = 0; x < width; x += COARSENESS) { | |
for (int y = 0; y < height; y += COARSENESS) { | |
vertices.add(new Vertex(x+off, y+off, 1.0)); | |
} | |
} | |
strokeWeight(off); | |
} | |
void draw() { | |
background(0); | |
int nVertices = vertices.size(); | |
int rowSize = width / COARSENESS; | |
int colSize = height / COARSENESS; | |
for (int i = 0; i < nVertices; ++i) { | |
Vertex v = (Vertex) vertices.get(i); | |
int iNorth = i - rowSize; iNorth = iNorth < 0 ? i : iNorth; | |
int iSouth = i + rowSize; iSouth = iSouth >= nVertices ? i : iSouth; | |
int iEast = i; | |
if ((i + 1) % rowSize != 0) iEast = i + 1; | |
if (i + 1 > nVertices) iEast = i; | |
int iWest = i; | |
if ((i - 1) % rowSize != rowSize - 1) iWest = i - 1; | |
if (i - 1 < 0) iWest = i; | |
Vertex north = (Vertex) vertices.get(iNorth); | |
Vertex south = (Vertex) vertices.get(iSouth); | |
Vertex east = (Vertex) vertices.get(iEast); | |
Vertex west = (Vertex) vertices.get(iWest); | |
v.mutate(north, south, east, west); | |
// print(String.format("%d => N: %d, S: %d, E: %d, W: %d [%.2f, %.2f]\n", i, iNorth, iSouth, iEast, iWest, v.val, v.target)); | |
v.draw(); | |
} | |
println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment