Created
April 17, 2023 01:16
-
-
Save obedrios/f6f82c9b8f00629da005e2afe2ad2b8d to your computer and use it in GitHub Desktop.
Two Dimensional with Two Source Wave Interference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parameters | |
int N = 500; // Grid size | |
float lambda = 10; // Wavelength | |
float k = 2 * PI / lambda; // Wave number | |
float A = 1; // Amplitude | |
float speed = 0.1; // Speed of the animation | |
// Source positions | |
int[] source1 = {N/4, N/2}; | |
int[] source2 = {3 * N/4, N/2}; | |
// Grid and wave arrays | |
float[][] R1 = new float[N][N]; | |
float[][] R2 = new float[N][N]; | |
float[][] wave = new float[N][N]; | |
float t = 0; | |
void setup() { | |
size(500, 500); | |
calculateDistance(); | |
} | |
void draw() { | |
background(255); | |
calculateWave(); | |
drawWave(); | |
t += speed; | |
} | |
void calculateDistance() { | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
R1[i][j] = sqrt(pow(i - source1[0], 2) + pow(j - source1[1], 2)); | |
R2[i][j] = sqrt(pow(i - source2[0], 2) + pow(j - source2[1], 2)); | |
} | |
} | |
} | |
void calculateWave() { | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
wave[i][j] = A * (sin(k * R1[i][j] - k * t) + sin(k * R2[i][j] - k * t)); | |
} | |
} | |
} | |
void drawWave() { | |
loadPixels(); | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
float brightness = map(wave[i][j], -2 * A, 2 * A, 0, 255); | |
color c = color(brightness); | |
int index = i + j * width; | |
if (index < pixels.length) { | |
pixels[index] = c; | |
} | |
} | |
} | |
updatePixels(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parameters | |
int N = 100; // Grid size (reduced for better performance) | |
float lambda = 10; // Wavelength | |
float k = 2 * PI / lambda; // Wave number | |
float A = 50; // Amplitude | |
float speed = 0.1; // Speed of the animation | |
// Source positions | |
int[] source1 = {N/4, N/2}; | |
int[] source2 = {3 * N/4, N/2}; | |
// Grid and wave arrays | |
float[][] R1 = new float[N][N]; | |
float[][] R2 = new float[N][N]; | |
float[][] wave = new float[N][N]; | |
float t = 0; | |
void setup() { | |
size(800, 800, P3D); | |
calculateDistance(); | |
} | |
void draw() { | |
background(255); | |
translate(width/2, height/2); | |
rotateX(radians(60)); | |
rotateZ(PI/4); | |
calculateWave(); | |
drawWave(); | |
t += speed; | |
} | |
void calculateDistance() { | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
R1[i][j] = sqrt(pow(i - source1[0], 2) + pow(j - source1[1], 2)); | |
R2[i][j] = sqrt(pow(i - source2[0], 2) + pow(j - source2[1], 2)); | |
} | |
} | |
} | |
void calculateWave() { | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
wave[i][j] = A * (sin(k * R1[i][j] - k * t) + sin(k * R2[i][j] - k * t)); | |
} | |
} | |
} | |
void drawWave() { | |
float scaleFactor = 4; | |
float zFactor = 1; | |
for (int i = 0; i < N-1; i++) { | |
beginShape(TRIANGLE_STRIP); | |
for (int j = 0; j < N; j++) { | |
float x1 = (i - N/2) * scaleFactor; | |
float y1 = (j - N/2) * scaleFactor; | |
float z1 = wave[i][j] * zFactor; | |
float x2 = (i + 1 - N/2) * scaleFactor; | |
float y2 = (j - N/2) * scaleFactor; | |
float z2 = wave[i + 1][j] * zFactor; | |
// Color mapping | |
float hue = map(z1, -2 * A, 2 * A, 0, 255); | |
fill(hue, 255, 255); | |
vertex(x1, y1, z1); | |
vertex(x2, y2, z2); | |
} | |
endShape(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment