-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterference-space.html
More file actions
290 lines (234 loc) · 11.6 KB
/
interference-space.html
File metadata and controls
290 lines (234 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Wave Field Visualization</title>
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.min.js"></script>
<style>
#controls {
position: absolute;
right: 10px;
top: 10px;
background-color: white;
border: 1px solid #ccc;
padding: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
z-index: 10;
}
#controls textarea {
width: 100%;
margin-top: 20px;
font-family: monospace;
font-size: 13px;
pointer-events: none;
}
#explanation {
padding: 30px;
max-width: 1000px;
margin: 20px auto 0 auto;
background-color: #f4f4f4;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
line-height: 1.6;
}
#explanation h2 {
margin-top: 0;
color: #333;
}
#explanation ul {
padding-left: 20px;
}
</style>
</head>
<body>
<div id="controls">
<label for="wavelengthSlider">Wavelength:</label>
<input type="range" id="wavelengthSlider" min="20" max="300" value="100">
<span id="wavelengthValue">100</span>
<br>
<label for="distanceSlider">Source Distance:</label>
<input type="range" id="distanceSlider" min="50" max="600" value="240">
<span id="distanceValue">240</span>
<br>
<label for="phaseShiftSlider">Phase Shift (radians):</label>
<input type="range" id="phaseShiftSlider" min="-3.1416" max="3.1416" step="0.01" value="0">
<span id="phaseShiftValue">0</span>
<br>
<label for="amplitudeRatioSlider">Amplitude Ratio (0.1 to 10):</label>
<input type="range" id="amplitudeRatioSlider" min="-1" max="1" step="0.01" value="0">
<span id="amplitudeRatioValue">1.0</span>
<br>
<label for="attenuationSlider">Distance Attenuation (0-2):</label>
<input type="range" id="attenuationSlider" min="0" max="2" step="0.1" value="1">
<span id="attenuationValue">1</span>
<br>
<label for="distanceScaleSlider">Distance Scale (0.01 to 5):</label>
<input type="range" id="distanceScaleSlider" min="0.01" max="5" step="0.01" value="1">
<span id="distanceScaleValue">1.00</span>
<br><br>
<textarea id="fieldValues" rows="4" cols="50" readonly></textarea>
</div>
<div id="canvas-container" style="position: relative; height: 800px;"></div>
<div id="explanation">
<h2>Wave Field Visualization</h2>
<p>
This simulation visualizes the wave fields generated by two coherent sources. Each control allows you to adjust key parameters:
</p>
<ul>
<li><strong>Wavelength:</strong> Determines the distance between wave crests. Longer wavelengths create broader patterns, while shorter wavelengths result in tighter, more defined wave interactions.</li>
<li><strong>Source Distance:</strong> Adjusts the spacing between the two wave sources. Wider spacing can lead to distinct interference patterns, while closer sources may yield overlapping waves.</li>
<li><strong>Phase Shift:</strong> Alters the phase at which each source emits waves. Changes in phase can enhance or diminish interference effects, affecting the visibility of wave patterns.</li>
<li><strong>Amplitude Ratio:</strong> Modifies the relative strength of the wave amplitudes from each source, affecting how they combine. A higher amplitude ratio alters the overall intensity of the resulting wave field.</li>
<li><strong>Distance Attenuation:</strong> Controls how wave intensity decreases over distance. This simulates real-world conditions where waves lose energy as they propagate.</li>
<li><strong>Distance Scale:</strong> Scales the calculated distances to make the visualization more comprehensible, especially when dealing with larger distances.</li>
</ul>
<p>
Move your mouse over the visualization to see details on the wave properties at that point. The information displayed includes the amplitude and phase for each source, as well as the combined amplitude and phase.
</p>
<p>
By adjusting the parameters and observing their effects on the wave fields, you gain insight into wave behavior, interference, and the complex interactions between different wave sources.
</p>
</div>
<script>
let source1, source2;
let wavelength = 100;
let distanceBetweenSources = 240;
let phaseShift = 0;
let amplitudeRatio = 1;
let attenuationPower = 0;
let distanceScale = 1;
let sourceDiameter = 20;
let maxAmplitude = 2;
function setup() {
let cnv = createCanvas(1200, 800);
cnv.parent('canvas-container');
colorMode(HSB, 360, 100, 100);
noLoop();
source1 = createVector(width / 2 - distanceBetweenSources / 2, height * 0.5);
source2 = createVector(width / 2 + distanceBetweenSources / 2, height * 0.5);
redrawField();
document.getElementById('wavelengthSlider').addEventListener('input', function() {
document.getElementById('wavelengthValue').innerText = this.value;
redrawField();
});
document.getElementById('distanceSlider').addEventListener('input', function() {
document.getElementById('distanceValue').innerText = this.value;
redrawField();
});
document.getElementById('phaseShiftSlider').addEventListener('input', function() {
document.getElementById('phaseShiftValue').innerText = this.value;
redrawField();
});
document.getElementById('amplitudeRatioSlider').addEventListener('input', function() {
let expValue = Math.pow(10, parseFloat(this.value));
document.getElementById('amplitudeRatioValue').innerText = expValue.toFixed(2);
redrawField();
});
document.getElementById('attenuationSlider').addEventListener('input', function() {
document.getElementById('attenuationValue').innerText = this.value;
redrawField();
});
document.getElementById('distanceScaleSlider').addEventListener('input', function() {
document.getElementById('distanceScaleValue').innerText = parseFloat(this.value).toFixed(2);
redrawField();
});
}
function dist2phase(d, shift) {
return TWO_PI * d / wavelength + shift;
}
function redrawField() {
wavelength = parseFloat(document.getElementById('wavelengthSlider').value);
distanceBetweenSources = parseFloat(document.getElementById('distanceSlider').value);
phaseShift = parseFloat(document.getElementById('phaseShiftSlider').value);
amplitudeRatio = Math.pow(10, parseFloat(document.getElementById('amplitudeRatioSlider').value));
attenuationPower = parseFloat(document.getElementById('attenuationSlider').value);
distanceScale = parseFloat(document.getElementById('distanceScaleSlider').value);
maxAmplitude =
Math.pow(0.5 * sourceDiameter * distanceScale, -attenuationPower) +
Math.pow(0.5 * (distanceBetweenSources - sourceDiameter) * distanceScale, -attenuationPower);
source1 = createVector(width / 2 - distanceBetweenSources / 2, height * 0.5);
source2 = createVector(width / 2 + distanceBetweenSources / 2, height * 0.5);
background(0, 0, 100);
loadPixels();
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let d1 = dist(x, y, source1.x, source1.y) * distanceScale;
let d2 = dist(x, y, source2.x, source2.y) * distanceScale;
let k = TWO_PI / wavelength;
let phase1 = dist2phase(d1, -phaseShift / 2)
let phase2 = dist2phase(d2, phaseShift / 2);
let attenuation1 = (d1 !== 0) ? Math.pow(d1, -attenuationPower) : 1;
let attenuation2 = (d2 !== 0) ? Math.pow(d2, -attenuationPower) : 1;
let sumReal = attenuation1 * cos(phase1) + amplitudeRatio * attenuation2 * cos(phase2);
let sumImag = attenuation1 * sin(phase1) + amplitudeRatio * attenuation2 * sin(phase2);
let amplitude = sqrt(sumReal * sumReal + sumImag * sumImag);
let phase = atan2(sumImag, sumReal);
let hue = degrees(phase);
if (hue < 0) hue += 360;
let saturation = amplitude / maxAmplitude * 100;
let c = color(hue, saturation, 100);
let index = 4 * (x + y * width);
pixels[index] = red(c);
pixels[index + 1] = green(c);
pixels[index + 2] = blue(c);
pixels[index + 3] = 255;
}
}
updatePixels();
drawSources();
}
function drawSources() {
let k = TWO_PI / wavelength;
let phase1 = dist2phase(0.5 * sourceDiameter, -0.5 * phaseShift);
let hue1 = degrees(phase1);
if (hue1 < 0) hue1 += 360;
let c1 = color(hue1, 100, 100);
let phase2 = dist2phase(0.5 * sourceDiameter, +0.5 * phaseShift);
let hue2 = degrees(phase2);
if (hue2 < 0) hue2 += 360;
let c2 = color(hue2, 100, 100);
noStroke();
fill(c1);
ellipse(source1.x, source1.y, sourceDiameter, sourceDiameter);
stroke(0);
noFill();
ellipse(source1.x, source1.y, sourceDiameter, sourceDiameter);
noStroke();
fill(c2);
ellipse(source2.x, source2.y, sourceDiameter, sourceDiameter);
stroke(0);
noFill();
ellipse(source2.x, source2.y, sourceDiameter, sourceDiameter);
}
function nicePhase(phase) {
phase = phase % (2 * PI);
if (phase > PI) {
phase -= 2 * PI;
} else if (phase <= -PI) {
phase += 2 * PI;
}
return Math.round(phase * (180 / PI) * 10) / 10;
}
function niceAmplitude(amplitude) {
return Math.round(amplitude * 1000) / 1000;
}
function mouseMoved() {
if (mouseX >= 0 && mouseX < width && mouseY >= 0 && mouseY < height) {
let d1 = dist(mouseX, mouseY, source1.x, source1.y) * distanceScale;
let d2 = dist(mouseX, mouseY, source2.x, source2.y) * distanceScale;
let phase1 = dist2phase(d1, -0.5 * phaseShift);
let phase2 = dist2phase(d2, +0.5 * phaseShift);
let amplitude1 = ((d1 !== 0) ? Math.pow(d1, -attenuationPower) : 1) / maxAmplitude;
let amplitude2 = ((d2 !== 0) ? Math.pow(d2, -attenuationPower) : 1) / maxAmplitude;
let sumReal = amplitude1 * cos(phase1) + amplitudeRatio * amplitude2 * cos(phase2);
let sumImag = amplitude1 * sin(phase1) + amplitudeRatio * amplitude2 * sin(phase2);
let amplitude = sqrt(sumReal * sumReal + sumImag * sumImag);
let phase = atan2(sumImag, sumReal);
document.getElementById('fieldValues').value =
`x: ${mouseX}, y: ${mouseY}\nSource 1 amplitude: ${niceAmplitude(amplitude1)}, phase: ${nicePhase(phase1)}\nSource 2 amplitude: ${niceAmplitude(amplitude2)}, phase: ${nicePhase(phase2)}\nCombined amplitude: ${niceAmplitude(amplitude)}, phase: ${nicePhase(phase)}`;
}
}
</script>
</body>
</html>