implement bee-like movement into ecosystem project
This commit is contained in:
parent
97c2a74712
commit
5dfb825315
15
Ecosystem Project/index.html
Normal file
15
Ecosystem Project/index.html
Normal file
@ -0,0 +1,15 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="https://cdn.jsdelivr.net/npm/p5@1.9.0/lib/p5.js"></script>
|
||||
<script src="sketch.js"></script>
|
||||
<style>
|
||||
body {
|
||||
background: #212121;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
60
Ecosystem Project/sketch.js
Normal file
60
Ecosystem Project/sketch.js
Normal file
@ -0,0 +1,60 @@
|
||||
// An algorithm for picking a random number based on monte carlo method
|
||||
function acceptreject(formula) {
|
||||
// We do this “forever” until we find a qualifying random value.
|
||||
while (true) {
|
||||
// Pick a random value.
|
||||
let r1 = random(1);
|
||||
// Assign a probability.
|
||||
let probability = formula(r1);
|
||||
// Pick a second random value.
|
||||
let r2 = random(1);
|
||||
|
||||
//{!3} Does it qualify? If so, we’re done!
|
||||
if (r2 < probability) {
|
||||
return r1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Walker {
|
||||
constructor(x = 0, y = 0) {
|
||||
this.t = 0
|
||||
this.x = x
|
||||
this.y = y
|
||||
}
|
||||
|
||||
step() {
|
||||
let dx, dy
|
||||
if (noise(this.t+20000) < 0.5) {
|
||||
let step = -1.5
|
||||
dx = random(-step, step)
|
||||
dy = random(-step, step)
|
||||
} else {
|
||||
let step = 0.5;
|
||||
dx = map(noise(this.t), 0, 1, -1, 1)*step;
|
||||
dy = map(noise(this.t+10000), 0, 1, -1, 1)*step;
|
||||
}
|
||||
|
||||
this.x = (this.x + dx) % width
|
||||
this.y = (this.y + dy) % height
|
||||
this.t += 0.01
|
||||
}
|
||||
|
||||
draw() {
|
||||
stroke(0)
|
||||
point(this.x, this.y)
|
||||
}
|
||||
}
|
||||
|
||||
let walker
|
||||
|
||||
function setup() {
|
||||
createCanvas(640, 240)
|
||||
background(255)
|
||||
walker = new Walker(320, 120)
|
||||
}
|
||||
|
||||
function draw() {
|
||||
walker.step();
|
||||
walker.draw();
|
||||
}
|
Loading…
Reference in New Issue
Block a user