From 3055dbd0a20efc949da1623f12f8d8551edd9442 Mon Sep 17 00:00:00 2001 From: Yannick Francois Date: Sun, 3 Sep 2017 16:31:41 +0200 Subject: [PATCH] Add random walk that follow mouse --- .../random_walk_follow_mouse.pde | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 random_walk_follow_mouse/random_walk_follow_mouse.pde diff --git a/random_walk_follow_mouse/random_walk_follow_mouse.pde b/random_walk_follow_mouse/random_walk_follow_mouse.pde new file mode 100644 index 0000000..23afae5 --- /dev/null +++ b/random_walk_follow_mouse/random_walk_follow_mouse.pde @@ -0,0 +1,60 @@ + +int EQUILIBRE = 3; +int SUD_EST = 4; +int BALADE = SUD_EST; + +class Walker { + int x; + int y; + + Walker() { + x = width / 2; + y = height / 2; + } + + void display() { + stroke(0); + point(x, y); + } + + boolean follow_the_mouse(float random) { + return random < 0.5; + } + + void step() { + float random = random(1); + if (follow_the_mouse(random)) { + int x_direction = (mouseX - x); + int y_direction = (mouseY - y); + + if (x_direction != 0) { + x_direction /= abs(x_direction); + } + if (y_direction != 0) { + y_direction /= abs(y_direction); + } + + x += x_direction; + y += y_direction; + } else { + x += int(random(-2, 2)); + y += int(random(-2, 2)); + } + + x = constrain(x, 0, width-1); + y = constrain(y, 0, height-1); + } +} + +Walker w; + +void setup(){ + size(640, 360); + w = new Walker(); + background(255); +} + +void draw() { + w.step(); + w.display(); +} \ No newline at end of file