xxxxxxxxxx
35
function setup() {
createCanvas(800, 600);
background(255);
}
function draw() {
background(255);
let lightSource1 = createVector(width / 3, height / 2);
let lightSource2 = createVector(2 * width / 3, height / 2);
let objectPos = createVector(width / 2, height / 3);
stroke(0);
line(lightSource1.x, lightSource1.y, objectPos.x, objectPos.y);
line(lightSource2.x, lightSource2.y, objectPos.x, objectPos.y);
fill(255, 0, 0);
ellipse(lightSource1.x, lightSource1.y, 10, 10);
ellipse(lightSource2.x, lightSource2.y, 10, 10);
fill(0, 255, 0);
ellipse(objectPos.x, objectPos.y, 10, 10);
// Calculate and display interference pattern
let plateY = 2 * height / 3;
for (let x = 0; x < width; x++) {
let d1 = dist(lightSource1.x, lightSource1.y, x, plateY);
let d2 = dist(lightSource2.x, lightSource2.y, x, plateY);
let interference = sin(d1 / 10) + sin(d2 / 10);
let brightness = map(interference, -2, 2, 0, 255);
stroke(brightness);
point(x, plateY);
}
}