Skip to content

Commit

Permalink
Optimize single slit sim
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrustyPwo committed May 11, 2024
1 parent ed0e287 commit 5b9234b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion static/js/shared/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Screen {
this.y = y;
this.h = h;

this.minX = 0.5 * cvs.width;
this.minX = 0.3 * cvs.width;
this.maxX = 0.85 * cvs.width;
}

Expand Down
1 change: 0 additions & 1 deletion static/js/sim4.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const slitWidthInput = document.getElementById("slitWidthInput");

const simulation = new SingleSlitSimulation(cvs, c, wavelengthInput.value, slitWidthInput.value * 1000);
const animate = () => {
c.clearRect(0, 0, cvs.width, cvs.height);
simulation.update();

setTimeout(() => {
Expand Down
22 changes: 15 additions & 7 deletions static/js/simulations/singleSlit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class SingleSlitSimulation extends Simulation {

this.t = 0;
this.dt = 1 / 60;

this.cache = {}
this.cache = {};
this.redraw = true;
}

evaluate = (theta) => {
Expand All @@ -29,13 +29,18 @@ class SingleSlitSimulation extends Simulation {

update = () => {
this.t += this.dt;
this.screen.draw();
this.slit.draw();
this.plotIntensity();
this.displayMeasurements();

if (this.redraw) {
this.c.clearRect(0, 0, this.cvs.width, this.cvs.height);
this.screen.draw();
this.slit.draw();
this.plotIntensity();
this.redraw = false;
} else this.c.clearRect(this.slit.x + 2.5, 0, this.screen.x - this.slit.x - 5, this.cvs.height);

this.c.save();
for (let x = 0; x < this.screen.x; x += 5) {
this.displayMeasurements();
for (let x = 0; x < this.screen.x - 3; x += 5) {
for (let y = 0; y <= this.cvs.height; y += 5) {
this.c.globalAlpha = this.intensityAt(x, y);
this.c.fillStyle = this.colorAt(x, y);
Expand Down Expand Up @@ -75,11 +80,13 @@ class SingleSlitSimulation extends Simulation {

setWavelength = (wavelength) => {
this.wavelength = wavelength;
this.redraw = true;
this.cache = {};
}

setSlitWidth = (slitWidth) => {
this.slit.width = slitWidth / this.ypx2nm;
this.redraw = true;
this.cache = {};
}

Expand All @@ -89,6 +96,7 @@ class SingleSlitSimulation extends Simulation {

mouseMove = (event, x, y) => {
this.screen.x = Math.max(Math.min(x, this.screen.maxX), this.screen.minX);
this.redraw = true;
}

intensityAt = (x, y) => {
Expand Down

0 comments on commit 5b9234b

Please sign in to comment.