add test p5 sketch

This commit is contained in:
Tom Hodson 2020-11-09 14:17:17 +01:00
parent ab73cbbdd5
commit 7755fae2e8
5 changed files with 93170 additions and 0 deletions

12
MCMCwalk/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html><html lang="en"><head>
<script src="p5.js"></script>
<script src="p5.sound.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
</head>
<body>
<script src="sketch.js"></script>
</body></html>

93039
MCMCwalk/p5.js Normal file

File diff suppressed because one or more lines are too long

3
MCMCwalk/p5.sound.min.js vendored Normal file

File diff suppressed because one or more lines are too long

109
MCMCwalk/sketch.js Normal file
View File

@ -0,0 +1,109 @@
let w = 200;
let stepsize = 10;
let Nwalkers = 1;
let fr = 1;
let cw = 400;
let canvas, src, pg;
function proposal(pos) {
}
class Walker {
constructor() {
this.pos = createVector(w/2, w/2);
}
//draw the walker
draw(ctx) {
ctx.circle(this.pos.x, this.pos.y, 1);
ctx.line(this.newpos.x, this.newpos.y, this.pos.x, this.pos.y);
}
//calculate dBH
get_dBH(landscape, prop) {
let l = lightness(landscape.get(this.newpos.x, this.newpos.y)) - lightness(landscape.get(this.pos.x, this.pos.y));
if (mouseIsPressed) {
let tomouse = createVector(this.pos.x - mouseX, this.pos.y - mouseY);
let mouse = p5.Vector.dot(tomouse, prop) / prop.mag() / tomouse.mag()
console.log('l, mouse:', l, mouse);
return l + mouse;
}
console.log('l:', l);
return l;
}
//move the walker
step(landscape) {
this.prop = p5.Vector.random2D();
this.newpos = p5.Vector.add(this.pos, this.prop);
this.prop.mult(stepsize);
let dBH = this.get_dBH(landscape, this.prop);
let n = this.newpos;
let withinBounds = (0 <= n.x) && (n.x <= w) && (0 <= n.y) && (n.y < w);
if(withinBounds && (dBH <= 0 || exp(-dBH) > random(1))) {
this.pos = n;
}
}
//leave behing a trail in the landscape
leaveFootstep(landscape) {
landscape.loadPixels();
let o = landscape.get(this.pos.x, this.pos.y);
let n = color(hue(o), saturation(o), lightness(o) * 0.9);
landscape.set(this.pos.x, this.pos.y, n);
landscape.updatePixels();
}
}
function setup() {
canvas = createCanvas(cw, cw);
pixelDensity(1);
let d = pixelDensity();
frameRate(fr);
src = createGraphics(w, w);
src.pixelDensity(1);
src.textAlign(CENTER, CENTER);
src.background(255);
src.textSize(6.5 * w/50);
src.text("Happy Birthday\n Sophie!", w/2, w/2);
src.loadPixels();
dest = createGraphics(w, w);
dest.pixelDensity(1);
dest.background(255);
pg = createGraphics(w, w);
pg.pixelDensity(1);
pg.background(255);
colorMode(HSL);
image(pg, 0, 0, cw, cw);
walkers = [];
for(let i = 0; i < Nwalkers; i += 1) {
append(walkers, new Walker());
}
}
function draw() {
pg.reset();
pg.background(255);
walkers.forEach(function(w) {
w.step(src);
w.leaveFootstep(dest);
w.draw(pg);
});
image(src, 0, 0, cw, cw)
image(dest, 0, 0, cw, cw);
image(pg, 0, 0, cw, cw);
}

7
MCMCwalk/style.css Normal file
View File

@ -0,0 +1,7 @@
html, body {
margin: 0;
padding: 0;
}
canvas {
display: block;
}