Created
December 21, 2016 03:43
-
-
Save fogleman/d767c6ab36ed6c3a724aedcdafdb0485 to your computer and use it in GitHub Desktop.
Random Walkers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<style>body {padding: 0; margin: 0;}</style> | |
</head> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/p5.min.js"></script> | |
<script> | |
function Point() { | |
this.x = width / 2; | |
this.y = height / 2; | |
} | |
Point.prototype.copy = function() { | |
var point = new Point(); | |
point.x = this.x; | |
point.y = this.y; | |
return point; | |
} | |
Point.prototype.step = function() { | |
var angle = random(-PI, PI); | |
this.x += cos(angle) * 1; | |
this.y += sin(angle) * 1; | |
} | |
Point.prototype.draw = function() { | |
ellipse(this.x, this.y, 2); | |
} | |
var points = []; | |
function restart() { | |
points = []; | |
for (var i = 0; i < 1; i++) { | |
points.push(new Point()); | |
} | |
} | |
function setup() { | |
background(255); | |
createCanvas(windowWidth, windowHeight); | |
restart(); | |
} | |
function windowResized() { | |
background(255); | |
resizeCanvas(windowWidth, windowHeight); | |
restart(); | |
} | |
function draw() { | |
noStroke(); | |
fill(0, 0, 0, 8); | |
for (var t = 0; t < 50; t++) { | |
for (var i = 0; i < points.length; i++) { | |
points[i].step(); | |
points[i].draw(); | |
} | |
if (points.length < 100 && random() < 0.001) { | |
points.push(points[Math.floor(random(points.length))].copy()); | |
} | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment