Nature of code – week 1 – Random Walker

This week we expanded on the random walker concept. We were introduced to different ideas and had a brief introduction to vectors. I didn’t really have any specific random walk sketch in mind for this weeks assignment – so I just played around with different basic ideas and variations. These are some of the sketches I ended up with.

This sketch is using the heading function and a random P5.vector2D()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Walker {

constructor (x,y) {

this.location = createVector(x,y);
this.velocity = createVector(0,0);
this.acceleration = createVector(0,0);

}

display() {
var angle = this.velocity.heading();
push();
rectMode(CENTER);
translate(this.location.x,this.location.y)
rotate(angle)
noStroke();
fill(160,200,200)
rect(0,0,50,10)
triangle(25,10,35,0,25,-10);
pop();

}

update() {
var v = p5.Vector.random2D()
v.limit(0.1);
this.location.add(this.velocity);
this.velocity.add(v);

}

edges() {
if (this.location.x > width) {
this.location.x = 0;
}
if (this.location.x < 0) {
this.location.x = width;
}
if (this.location.y > height) {
this.location.y = 0;
}
if (this.location.y < 0) {
this.location.y = height;
}

}

}

 

This sketch is using the createShape() to combine the random walkers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
let walkers = [];
let totalWalkers = 40;

function setup() {
createCanvas(400, 400);
for (var i = 0; i < totalWalkers; i++) {
walkers.push(new Walker(random(width/3,width/3*2),random(height/3,height/3*2)));
}
}

function draw() {
background(220);
//ellipse(width/2,height/2,10,10);
for (var i = 0; i < totalWalkers-1; i++) {
walkers[i].display();
walkers[i].update();

push();
beginShape(QUADS);
fill(255,255,255)
vertex(walkers[i+1].x+5,walkers[i+1].y)
vertex(walkers[i+1].x-5,walkers[i+1].y)
vertex(walkers[i].x-5,walkers[i].y)
vertex(walkers[i].x+5,walkers[i].y)
endShape(CLOSE);
pop();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *