Skip to content

Instantly share code, notes, and snippets.

@obedrios
Created April 17, 2023 00:54
Show Gist options
  • Save obedrios/55b024b7e78011abe1621e13cbd190c6 to your computer and use it in GitHub Desktop.
Save obedrios/55b024b7e78011abe1621e13cbd190c6 to your computer and use it in GitHub Desktop.
Simple 1D Wave Interference in Processing
float amplitude1 = 100; // Amplitude of the first wave
float amplitude2 = 50; // Amplitude of the second wave
float wavelength1 = 200; // Wavelength of the first wave
float wavelength2 = 400; // Wavelength of the second wave
float frequency1 = 2*PI/wavelength1; // Frequency of the first wave
float frequency2 = 2*PI/wavelength2; // Frequency of the second wave
float phase1 = 0; // Phase of the first wave
float phase2 = 0; // Phase of the second wave
void setup() {
size(800, 400);
background(255);
}
void draw() {
float[] y1 = new float[width];
float[] y2 = new float[width];
float[] interference = new float[width];
// Calculate the waves at each x value, with time-dependent phase for the second wave
for (int x = 0; x < width; x++) {
y1[x] = amplitude1 * sin(frequency1 * x + phase1);
y2[x] = amplitude2 * sin(frequency2 * x + phase2);
interference[x] = y1[x] + y2[x];
}
// Draw the waves and interference
noFill();
background(255);
stroke(255, 0, 0);
beginShape();
for (int x = 0; x < width; x++) {
vertex(x, height/2 + y1[x]);
}
endShape();
// Wave 2
stroke(0, 0, 255);
beginShape();
for (int x = 0; x < width; x++) {
vertex(x, height/2 + y2[x]);
}
endShape();
// Interference Wave
stroke(0, 255, 0);
beginShape();
for (int x = 0; x < width; x++) {
vertex(x, height/2 + interference[x]);
}
endShape();
// Increment the phase of the second wave over time to animate the interference
phase2 += 0.01;
}
float amplitude1 = 100; // Amplitude of the first wave
float amplitude2 = 50; // Amplitude of the second wave
float wavelength1 = 200; // Wavelength of the first wave
float wavelength2 = 400; // Wavelength of the second wave
float frequency1 = 2*PI/wavelength1; // Frequency of the first wave
float frequency2 = 2*PI/wavelength2; // Frequency of the second wave
float phase1 = 0; // Phase of the first wave
float phase2 = PI/2; // Phase of the second wave
void setup() {
size(800, 400);
background(255);
float[] y1 = new float[width];
float[] y2 = new float[width];
float[] interference = new float[width];
// Calculate the waves at each x value
for (int x = 0; x < width; x++) {
y1[x] = amplitude1 * sin(frequency1 * x + phase1);
y2[x] = amplitude2 * sin(frequency2 * x + phase2);
interference[x] = y1[x] + y2[x];
}
// Draw the waves and interference
noFill();
stroke(255, 0, 0);
beginShape();
for (int x = 0; x < width; x++) {
vertex(x, height/2 + y1[x]);
}
endShape();
stroke(0, 0, 255);
beginShape();
for (int x = 0; x < width; x++) {
vertex(x, height/2 + y2[x]);
}
endShape();
stroke(0, 255, 0);
beginShape();
for (int x = 0; x < width; x++) {
vertex(x, height/2 + interference[x]);
}
endShape();
}
void draw() {
//Do Nothing
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment