Skip to content

Instantly share code, notes, and snippets.

@volfegan
Last active October 1, 2019 02:12
Show Gist options
  • Save volfegan/be2e3a1f87f63b5a659fb17132cf58d4 to your computer and use it in GitHub Desktop.
Save volfegan/be2e3a1f87f63b5a659fb17132cf58d4 to your computer and use it in GitHub Desktop.
6 different Agents modify only one specific RGB unit either by +1 or -1. Basically creates random colour noise to the canvas
//@author u/thudly
//https://www.reddit.com/r/processing/comments/da6l10/munchers_and_poopers_a_very_inefficient_3d_noise/
//modified by Volfegan
int n = 12;
int wCount = 6 * n; // number of walkers. should be a multiple of 6
Walker[] walkers = new Walker[wCount];
boolean showFrameRate = true;
void setup() {
size(960, 720);
background(128);
for (int i = 0; i < wCount; i++) {
walkers[i] = new Walker(i % 6);
}
}
void draw() {
//show frame rate, frameCount % number is to reduce the rate of println
if (frameCount % 120 == 0) {
println(String.format("%.2f", frameRate) + " frameRate ");
}
int repeatProcess = 5000;
for (int a = 0; a < repeatProcess; a++) {
for (Walker w : walkers) {
w.move();
w.eat(1); // pass the desired strength as an argument, 1 by default
}
}
}
class Walker {
int x;
int y;
int type;
Walker(int t) { // the constructor receives an int for type (i % 6)
type = t;
x = int(random(width));
y = int(random(height));
}
void move() {
x += round(random(-1, 1)); // random x adjustment (-1, 0 , or +1)
if (x < 0) x += width; // wrap to right side
if (x > width - 1) x -= width; // wrap to left side
y += round(random(-1, 1)); // random y adjustment (-1, 0 , or +1)
if (y < 0) y += height; // wrap to bottom
if (y > height - 1) y -= height; // wrap to top
}
void eat(int p) {
float r, g, b;
color c1 = get(x, y); // the current color
// get current R, G, B values
r = red(c1);
g = green(c1);
b = blue(c1);
// adjust R, G, or B depending on walker type
// also, make sure values don't go below zero or above 255.
switch(type) {
case 0:
if (r > p) r -= p; // munch red
break;
case 1:
if (r < 255 - p) r += p; // poop
break;
case 2:
if (g > p) g -= p; // munch green
break;
case 3:
if (g < 255 - p) g += p; // poop green
break;
case 4:
if (b > p) b -= p; // munch blue
break;
case 5:
if (b < 255 - p) b += p; // poop blue
break;
}
// set the current pixel to the adjusted color.
color c2 = color(r, g, b);
set(x, y, c2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment