Skip to content

Commit

Permalink
let it snow
Browse files Browse the repository at this point in the history
  • Loading branch information
pedersen committed Dec 5, 2011
1 parent 81b7d54 commit d56ddca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions sketches/LetItSnow/LetItSnow.pde
@@ -1,4 +1,4 @@
final int CNT = 1000;
final int CNT = 500;
final color BACKGROUND = color(0);
final color FLAKECOLOR = color(255, 255, 255);
Particle[] flakes;
Expand All @@ -10,7 +10,7 @@ PImage img;

void setup() {
size(600, 400, P2D);
img = loadImage("osaa2.png");
img = loadImage("osaa3.png");
snow_init();
}

Expand Down Expand Up @@ -79,39 +79,66 @@ boolean isEmpty(int x, int y) {
return bg[y * width + x] == BACKGROUND;
}

void setBG(int idx, color c) {
bg[idx] = c;
updates[idx] = frameCount;
}

void smooth_snow() {

for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
int idx = y * width + x;
if (FLAKECOLOR != bg[idx])
continue;
if (updates[idx] == frameCount)
if (FLAKECOLOR != bg[idx]
|| updates[idx] == frameCount)
continue;

int next_idx = idx;
float p = random(0,1);
//fall if possible
if (isEmpty(x, y+1)) {
bg[idx] = BACKGROUND;
bg[idx+width] = FLAKECOLOR;
updates[idx+width] = frameCount;
setBG(idx, BACKGROUND);
//perhaps move sideways
if ( p > .8) {
if (p > .9 && isEmpty(x+1, y+1))
next_idx++;
else if (isEmpty(x-1, y+1))
next_idx--;
}
next_idx += width;
setBG(next_idx, FLAKECOLOR);
continue;
}

//roll if possible
boolean leftEmpty = isEmpty(x-1,y+1);
boolean rightEmpty = isEmpty(x+1,y+1);
if (leftEmpty || rightEmpty) {
bg[idx] = BACKGROUND;
idx = idx + width;
setBG(idx, BACKGROUND);
next_idx += width;
if (leftEmpty ^ rightEmpty) {
idx += (rightEmpty ? 1 : -1);
next_idx += (rightEmpty ? 1 : -1);
} else {
idx += (random(0,1)>.5 ? 1 : -1);
next_idx += (random(0,1)>.5 ? 1 : -1);
}
updates[idx] = frameCount;
bg[idx] = FLAKECOLOR;
}

setBG(next_idx, FLAKECOLOR);
continue;
}

//avoid pyramids
//by perhaps move sideways
if ( p > .8) {
if (p > .9 && isEmpty(x+1, y)) {
next_idx++;
} else if (isEmpty(x-1, y)) {
next_idx--;
} else
continue;
setBG(idx, BACKGROUND);
setBG(next_idx, FLAKECOLOR);

}

}
}
}
Expand Down
Binary file added sketches/LetItSnow/data/osaa3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d56ddca

Please sign in to comment.