Skip to content

Commit

Permalink
Replace frequency with wavelength
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrustyPwo committed Mar 30, 2024
1 parent 95396a1 commit 0bfbca0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions sim2.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<canvas width="1000px" height="500px"></canvas>

<div class="frequency">
<input type="range" min="0.2" max="1" step="0.1" value="1" class="slider" id="frequencyInput">
Frequency: <span id="frequencyValue">1.0</span> Hz
<input type="range" min="50" max="200" step="10" value="120" class="slider" id="wavelengthInput">
Wavelength: <span id="wavelengthValue">120</span> px
</div>
<div class="amplitude">
<input type="range" min="0.1" max="0.3" step="0.05" value="0.2" class="slider" id="amplitudeInput">
Expand Down
17 changes: 10 additions & 7 deletions static/js/shared/waves.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class WaveVectorDisplay {
this.c = c;

this.t = 0;
this.dt = 1 / 15
this.frequency = 1.0;
this.dt = 1 / 60;
this.wavelength = 150;
this.amplitude = 0.2;
this.velocity = 60;
this.spacing = 10;

this.vectors = [];
Expand Down Expand Up @@ -89,8 +90,8 @@ class WaveVectorDisplay {
return distance(this.x1, this.y1, this.x2, this.y2);
}

get wavelength() {
return 120 / this.frequency;
get waveNumber() {
return 2 * Math.PI / this.wavelength;
}
}

Expand All @@ -101,8 +102,9 @@ class WaveVector {

this.x = this.display.x1 + this.id * this.display.spacing;
this.base = this.display.gradient * this.x + this.display.verticalOffset;
const dist = distance(this.x, this.base, this.display.x1, this.display.y1);
this.y = 0.5 * this.display.amplitude * this.display.cvs.height
* Math.sin(0.05 * this.display.frequency * distance(this.x, this.base, this.display.x1, this.display.y1)) + this.base;
* Math.cos(this.display.waveNumber * (dist - this.display.velocity * this.display.t)) + this.base;
}

draw = () => {
Expand All @@ -117,8 +119,9 @@ class WaveVector {
}

update = () => {
const dist = distance(this.x, this.base, this.display.x1, this.display.y1);
this.y = 0.5 * this.display.amplitude * this.display.cvs.height
* Math.sin(0.05 * this.display.frequency * distance(this.x, this.base, this.display.x1, this.display.y1) - this.display.t) + this.base;
* Math.cos(this.display.waveNumber * (dist - this.display.velocity * this.display.t)) + this.base;
}
}

Expand Down Expand Up @@ -183,7 +186,7 @@ class WaveSource {
}

update = () => {
this.internal = this.offset + this.amplitude * Math.sin(this.display.t);
this.internal = this.offset + this.amplitude * Math.cos(this.display.waveNumber * this.display.velocity * this.display.t);
}

draw = () => {
Expand Down
10 changes: 5 additions & 5 deletions static/js/sim2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const fps = 60;

const cvs = document.querySelector('canvas');
const c = cvs.getContext('2d');
const frequencyInput = document.getElementById("frequencyInput");
const wavelengthInput = document.getElementById("wavelengthInput");
const amplitudeInput = document.getElementById("amplitudeInput");
const pathDifference = document.getElementById("pathDifference");
const phaseDifference = document.getElementById("phaseDifference");
const interference = document.getElementById("interference");

const simulation = new InterferenceSimulation(cvs, c, frequencyInput.value, amplitudeInput.value);
const simulation = new InterferenceSimulation(cvs, c, wavelengthInput.value, amplitudeInput.value);
simulation.wave1.waveTopColor = "#e1503c";
simulation.wave1.waveBottomColor = "#ea887b";
simulation.wave2.waveTopColor = "#eea71f";
Expand All @@ -35,9 +35,9 @@ const animate = () => {

}

frequencyInput.oninput = () => {
document.getElementById("frequencyValue").innerText = frequencyInput.value;
simulation.setFrequency(frequencyInput.value);
wavelengthInput.oninput = () => {
document.getElementById("wavelengthValue").innerText = wavelengthInput.value;
simulation.setWavelength(wavelengthInput.value);
}

amplitudeInput.oninput = () => {
Expand Down
14 changes: 7 additions & 7 deletions static/js/simulations/interference.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { Screen } from "../shared/screen.js";

// Double Source Interference Simulation
class InterferenceSimulation extends Simulation {
constructor(cvs, c, frequency = 1.0, amplitude = 0.2) {
constructor(cvs, c, wavelength = 120, amplitude = 0.2) {
super(cvs, c);
this.frequency = frequency;
this.wavelength = wavelength;
this.amplitude = amplitude;

this.wave1 = new WaveVectorDisplay(cvs, c);
this.wave2 = new WaveVectorDisplay(cvs, c);
this.setFrequency(frequency);
this.setWavelength(wavelength);
this.setAmplitude(amplitude);

this.pointer = new Pointer(cvs, c, cvs.width - 50, cvs.height / 2);
Expand All @@ -36,10 +36,10 @@ class InterferenceSimulation extends Simulation {
this.screen.draw();
}

setFrequency = (freq) => {
this.frequency = freq;
this.wave1.frequency = freq;
this.wave2.frequency = freq;
setWavelength = (wavelength) => {
this.wavelength = wavelength;
this.wave1.wavelength = wavelength;
this.wave2.wavelength = wavelength;
}

setAmplitude = (amplitude) => {
Expand Down

0 comments on commit 0bfbca0

Please sign in to comment.