Left-click to pause, any other mouse button to reset.
A fork of Bouncing Circular Random Walker by Vamoss
xxxxxxxxxx
/**
* Bouncing Circular Random Walker (v1.0.1) [Java/Pjs]
*
* p5js original version by Vamoss (2018/Nov/09)
* https://OpenProcessing.org/sketch/624877
*
* Java Mode conversion by GoToLoop (2018/Dec/11)
* https://OpenProcessing.org/sketch/643550
*
* https://Discourse.Processing.org/t/translate-p5js-code-to-processing-code/6505/7
*/
static final color BG = -1, COLORS = 45;
static final float BOLD = 1.0, FPS = 60.0;
static final int RAD_MIN_STEP = 1, RAD_MAX_STEP = 20, LIM = 50;
final PVector prevPos = new PVector(), currPos = prevPos.get();
float ang, rad, dir;
boolean paused;
void setup() {
size(600, 600);
smooth(3);
frameRate(FPS);
colorMode(HSB, COLORS, 1, 1);
strokeCap(ROUND);
strokeJoin(ROUND);
strokeWeight(BOLD);
restart();
}
void draw() {
ang += dir/rad;
currPos.add(cos(ang) * rad, sin(ang) * rad);
int x = round(currPos.x), y = round(currPos.y);
if (x < 0 || x >= width || y < 0 || y >= height) {
bounce();
x = constrain(x, -LIM, width + LIM);
y = constrain(y, -LIM, height + LIM);
currPos.set(x, y);
} else if (get(x, y) != BG) bounce();
line(prevPos.x, prevPos.y, currPos.x, currPos.y);
prevPos.set(currPos);
}
void mousePressed() {
if (mouseButton == LEFT)
if (paused ^= true) noLoop();
else loop();
else restart();
}
void bounce() {
rad = random(RAD_MIN_STEP, RAD_MAX_STEP);
ang += PI * (dir *= -1);
}
void restart() {
background(BG);
stroke(random(COLORS), 1, 1);
prevPos.set(width>>1, height>>1);
currPos.set(prevPos);
rad = dir = 1;
}