ACCURACY: The simulation must be scientifically/mathematically accurate for youngs double slit experiment
FUNCTIONAL INTERACTIVITY: All sliders and buttons MUST actually control and modify the simulation behavior in real-time
EDUCATIONAL: Users should learn about youngs double slit experiment through experimentation and parameter manipulation
VISUAL PRECISION: Animations should be smooth, realistic, and visually accurate to real-world physics
MANDATORY INTERACTIVE ELEMENTS THAT MUST WORK:
At least 4-6 sliders controlling key parameters with realistic ranges - these MUST change simulation behavior
At least 3-4 buttons for actions (reset, pause/play, presets, etc.) - these MUST perform actual functions
Real-time parameter display showing current values that update as sliders move
Visual feedback that responds immediately and visibly to control changes
Mouse interactions where physically meaningful
CRITICAL: CONTROLS MUST BE FUNCTIONAL, NOT DECORATIVE
Every slider must control a variable that actively affects the simulation
Every button must perform a meaningful action that changes the simulation state
Parameter changes must be immediately visible in the simulation output
Use the actual slider values in mathematical calculations and physics equations
ANIMATION AND TIMING REQUIREMENTS:
Use realistic time scaling - avoid animations that are too fast or unrealistic
Implement proper frame rate control using deltaTime or fixed timesteps
Use realistic physical constants and scaling factors
Ensure smooth, stable animations at 60fps
Avoid conflicting visual elements or overlapping controls
Make animations visually appealing and scientifically accurate
CONTROL IMPLEMENTATION REQUIREMENTS:
Sliders: Use createSlider(min, max, default, step) with appropriate ranges that make physical sense
Buttons: Use createButton('Label') with meaningful functions that actually execute
Position controls below canvas using .position(x, y) - ensure no overlap
Show parameter values with text() in the main canvas that update in real-time
Use function declarations for event handlers: function resetSim() {}
ALWAYS use slider.value() in your calculations - never ignore slider inputs
CODE FORMAT REQUIREMENTS:
Use GLOBAL MODE p5.js (NOT instance mode)
Assistant
Define setup() and draw() as regular functions
All variables should be global (use let/var at top level)
Use standard p5.js function names: createCanvas, background, fill, etc.
NO sketch wrapper function - write direct p5.js code
SIMULATION ACCURACY FOR YOUNGS DOUBLE SLIT EXPERIMENT:
Use correct physics equations, mathematical relationships, or algorithmic principles
Realistic parameter ranges and default values that make physical sense
Smooth, continuous animations with proper time scaling
Proper scaling and units where applicable
Include scientific constants or realistic coefficients
ENSURE every parameter controlled by sliders affects the simulation visibly
Use appropriate time steps and scaling factors to avoid too-fast animations
TEXT AND DISPLAY REQUIREMENTS:
Show ONLY the simulation title at the top
Display ONLY key physics parameters (velocity, acceleration, force, etc.) - no instructions or explanations
Keep text minimal and clean - maximum 4-5 parameter displays
NO instruction text like "Click to...", "Drag to...", etc.
Use concise parameter labels like "Velocity: 5.2 m/s" not "Current velocity value is 5.2"
Position parameter text neatly in corners or edges, not cluttering the center
VISUAL REQUIREMENTS:
Clear, educational visualization of the concept
Multiple visual elements (trails, vectors, measurements, etc.) that don't conflict
Color coding to distinguish different aspects
Grid lines, axes, or reference markers where helpful
Immediate and obvious visual response to parameter changes
Smooth, realistic motion that matches real-world physics
Proper visual hierarchy - important elements should stand out
Clean, uncluttered display with minimal text
EXAMPLE OF FUNCTIONAL CONTROLS WITH PROPER TIMING:
javascript
// Global variables that ACTUALLY affect simulation
let gravitySlider, dampingSlider;
let gravity = 0.5; // This gets updated by slider
let damping = 0.99; // This gets updated by slider
let resetBtn, pauseBtn;
let isPaused = false;
let timeScale = 0.02; // Control animation speed
function setup() {
createCanvas(400, 400);
// Create sliders that CONTROL the simulation with realistic ranges
gravitySlider = createSlider(0.1, 1.0, 0.4, 0.05);
gravitySlider.position(10, height + 10);
dampingSlider = createSlider(0.9, 1.0, 0.995, 0.001);
dampingSlider.position(10, height + 40);
// Create buttons that DO something - positioned to avoid overlap
resetBtn = createButton('Reset');
resetBtn.position(10, height + 40);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(80, height + 40);
}
function draw() {
background(220);
// ACTUALLY USE the slider values - this is critical!
gravity = gravitySlider.value();
damping = dampingSlider.value();
if (!isPaused) {
// Use gravity and damping in actual physics calculations with proper time scaling
velocity += gravity * timeScale;
velocity *= damping;
position += velocity;
}
// Show ONLY title and key parameters - keep it minimal
fill(0);
textAlign(CENTER);
text('Youngs Double Slit Experiment', width/2, 20);
textAlign(LEFT);
text('Gravity: ' + gravity.toFixed(2), 10, 40);
text('Velocity: ' + velocity.toFixed(2), 10, 60);
// Draw simulation that responds to parameters with realistic motion
ellipse(position, height/2, 20, 20);
}
// Functions that actually work
function resetSimulation() {
position = width/2;
velocity = 0;
}
function togglePause() {
isPaused = !isPaused;
}
IMPORTANT:
Focus on the specific physics, mathematics, or principles of youngs double slit experiment
Make controls that ACTUALLY matter for understanding the concept
Every slider must control a variable used in the simulation
Every button must perform a visible action
Ensure the simulation behaves realistically and responds to all controls
Use proper time scaling to avoid unrealistically fast animations
Position controls carefully to avoid visual conflicts
Make animations smooth and scientifically accurate
Keep text displays MINIMAL - only title and key parameters
NO instruction text or explanations in the simulation
Only output JavaScript code, no explanations
Use global mode p5.js format (no sketch wrapper)
Position controls below the 400x400 canvas with proper spacing
Generate an accurate, interactive simulation of: youngs double slit experiment with WORKING controls, REALISTIC timing, and MINIMAL text displays
javascript
// Young's Double Slit Experiment Simulation - p5.js Global Mode
let canvasW = 800;
let canvasH = 400;
let wavelengthSlider, slitDistanceSlider, screenDistanceSlider, slitWidthSlider, intensitySlider, timeScaleSlider;
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let wavelength = 500; // nm (visible light)
let slitDistance = 0.25; // mm (distance between slits)
let screenDistance = 1.0; // m (distance from slits to screen)
let slitWidth = 0.05; // mm (width of each slit)
let intensity = 1.0; // relative intensity (0.1 to 1.5)
let timeScale = 1.0; // animation speed multiplier
let isPaused = false;
let time = 0;
let canvas, ctx;
function setup() {
canvas = createCanvas(canvasW, canvasH);
canvas.parent(document.body);
frameRate(60);
pixelDensity(1);
noStroke();
// Sliders with realistic ranges and steps
wavelengthSlider = createSlider(380, 750, 500, 1); // nm, visible spectrum
wavelengthSlider.position(10, height + 10);
wavelengthSlider.style('width', '180px');
slitDistanceSlider = createSlider(0.05, 1.0, 0.25, 0.01); // mm
slitDistanceSlider.position(210, height + 10);
slitDistanceSlider.style('width', '180px');
screenDistanceSlider = createSlider(0.2, 3.0, 1.0, 0.01); // m
screenDistanceSlider.position(410, height + 10);
screenDistanceSlider.style('width', '180px');
slitWidthSlider = createSlider(0.01, 0.2, 0.05, 0.005); // mm
slitWidthSlider.position(610, height + 10);
slitWidthSlider.style('width', '180px');
intensitySlider = createSlider(0.1, 1.5, 1.0, 0.01);
intensitySlider.position(10, height + 50);
intensitySlider.style('width', '180px');
timeScaleSlider = createSlider(0.1, 3.0, 1.0, 0.01);
timeScaleSlider.position(210, height + 50);
timeScaleSlider.style('width', '180px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(410, height + 50);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(480, height + 50);
pauseBtn.mousePressed(togglePause);
presetBtn1 = createButton('Preset: Red Laser');
presetBtn1.position(600, height + 50);
presetBtn1.mousePressed(() => {
wavelengthSlider.value(650);
slitDistanceSlider.value(0.3);
screenDistanceSlider.value(1.2);
slitWidthSlider.value(0.05);
intensitySlider.value(1.0);
timeScaleSlider.value(1.0);
});
presetBtn2 = createButton('Preset: Blue LED');
presetBtn2.position(720, height + 50);
presetBtn2.mousePressed(() => {
wavelengthSlider.value(450);
slitDistanceSlider.value(0.15);
screenDistanceSlider.value(0.8);
slitWidthSlider.value(0.03);
intensitySlider.value(1.2);
timeScaleSlider.value(1.0);
});
resetSim();
}
function draw() {
background(30);
// Update parameters from sliders
wavelength = wavelengthSlider.value(); // nm
slitDistance = slitDistanceSlider.value(); // mm
screenDistance = screenDistanceSlider.value(); // m
slitWidth = slitWidthSlider.value(); // mm
intensity = intensitySlider.value(); // relative
timeScale = timeScaleSlider.value();
if (!isPaused) {
time += deltaTime * 0.001 * timeScale; // seconds scaled by timeScale
}
// Constants and conversions
// Convert units to meters for calculation
let wl = wavelength * 1e-9; // wavelength in meters
let d = slitDistance * 1e-3; // slit distance in meters
let D = screenDistance; // screen distance in meters
let a = slitWidth * 1e-3; // slit width in meters
// Calculate screen pixel scale
// We'll map screen width to physical width of interference pattern
// Max fringe position approx: y_max = (lambda * D) / d * N fringes
// We want to show ~10 fringes on screen width
let fringeSpacing = (wl * D) / d; // meters between fringes on screen
let fringesToShow = 10;
let physicalWidth = fringeSpacing * fringesToShow; // meters
// Map physicalWidth to canvas width
let scaleX = canvasW / physicalWidth;
// Center vertical axis for screen
let centerY = canvasH / 2;
// Draw grid lines for reference (fringes)
stroke(80);
strokeWeight(1);
for (let i = -fringesToShow / 2; i <= fringesToShow / 2; i++) {
let x = canvasW / 2 + i * fringeSpacing * scaleX;
line(x, 0, x, canvasH);
}
noStroke();
// Draw slits on left side
let slitX = 80;
fill(200);
let slitHeight = 60;
let slitGapPx = d * scaleX * 1e3; // scale slit distance to pixels (scaled up for visibility)
if (slitGapPx < 20) slitGapPx = d * 1e3 * 10; // fallback scaling for visibility
// Draw slit rectangles (scaled for visibility)
let slitWidthPx = max(2, a * scaleX * 1e3);
rectMode(CENTER);
fill(220);
rect(slitX, centerY - slitGapPx / 2, slitWidthPx, slitHeight);
rect(slitX, centerY + slitGapPx / 2, slitWidthPx, slitHeight);
// Draw screen line on right side
stroke(150);
strokeWeight(2);
line(canvasW - 50, 0, canvasW - 50, canvasH);
noStroke();
// Calculate intensity pattern on screen
// Intensity I(y) = I0 * [cos^2( (pi * d * y) / (lambda * D) )] * [ (sin(pi * a * y) / (pi * a * y))^2 ]
// y = position on screen relative to center (meters)
// We'll compute intensity for each vertical pixel column on screen
loadPixels();
let I0 = intensity; // max intensity scaling
// Precompute constants
let k = TWO_PI / wl; // wave number
// For animation: add a phase shift to simulate wave oscillation
let omega = 2 * PI * 3e14; // angular freq ~ visible light freq (approx 3e14 Hz)
// We can't animate at optical frequency, so simulate a slow phase shift for visualization
let phaseShift = time * 2 * PI * 0.5; // 0.5 Hz slow oscillation for visible effect
// We'll draw the interference pattern on the right side screen area (last 100 px)
let screenStartX = canvasW - 150;
let screenWidthPx = 100;
// For performance, sample every 2 pixels horizontally
for (let px = 0; px < screenWidthPx; px++) {
let x = screenStartX + px;
// Map px to physical y position on screen (centered)
let y_m = ((px - screenWidthPx / 2) / scaleX); // meters
// Avoid division by zero in diffraction term
let beta = (PI * a * y_m) / (wl * D);
let betaSafe = beta === 0 ? 1e-6 : beta;
// Interference term
let interference = cos((PI * d * y_m) / (wl * D) + phaseShift);
interference = interference * interference; // cos^2
// Diffraction term (single slit envelope)
let diffraction = (sin(betaSafe) / betaSafe);
diffraction = diffraction * diffraction;
// Total intensity normalized
let I = I0 * interference * diffraction;
// Map intensity to color brightness (0-255)
let brightness = constrain(I * 255, 0, 255);
// Draw vertical line with brightness
for (let py = 0; py < canvasH; py++) {
let idx = 4 * (py * width + x);
// Color: use wavelength to get approximate RGB color
let col = wavelengthToRGB(wavelength);
pixels[idx] = (col[0] * brightness) / 255;
pixels[idx + 1] = (col[1] * brightness) / 255;
pixels[idx + 2] = (col[2] * brightness) / 255;
pixels[idx + 3] = 255;
}
}
updatePixels();
// Draw axis line for screen center
stroke(100);
strokeWeight(1);
line(screenStartX + screenWidthPx / 2, 0, screenStartX + screenWidthPx / 2, canvasH);
noStroke();
// Draw wavefront animation at slits (simplified)
let waveRadiusMax = 150;
let waveCount = 4;
let waveSpeed = 50 * timeScale; // pixels per second
push();
translate(slitX, centerY);
noFill();
stroke(180, 180, 255, 120);
strokeWeight(1.5);
for (let i = 0; i < waveCount; i++) {
let r = (time * waveSpeed * 1000 + i * (waveRadiusMax / waveCount)) % waveRadiusMax;
ellipse(0, -slitGapPx / 2, r * 2);
ellipse(0, slitGapPx / 2, r * 2);
}
pop();
// Title
fill(255);
textAlign(CENTER, TOP);
textSize(20);
text('Young\'s Double Slit Experiment', width / 2, 10);
// Parameter displays (top-left corner)
textSize(14);
textAlign(LEFT, TOP);
fill(200);
text(`Wavelength: ${wavelength} nm`, 10, 40);
text(`Slit Distance: ${slitDistance.toFixed(3)} mm`, 10, 60);
text(`Screen Distance: ${screenDistance.toFixed(2)} m`, 10, 80);
text(`Slit Width: ${slitWidth.toFixed(3)} mm`, 10, 100);
text(`Intensity: ${intensity.toFixed(2)}`, 10, 120);
text(`Time Scale: ${timeScale.toFixed(2)}x`, 10, 140);
}
// Reset simulation to default values
function resetSim() {
wavelengthSlider.value(500);
slitDistanceSlider.value(0.25);
screenDistanceSlider.value(1.0);
slitWidthSlider.value(0.05);
intensitySlider.value(1.0);
timeScaleSlider.value(1.0);
time = 0;
isPaused = false;
}
// Toggle pause/play
function togglePause() {
isPaused = !isPaused;
}
// Convert wavelength (nm) to approximate RGB color
// Approximate visible spectrum 380-750 nm
function wavelengthToRGB(wl) {
let R = 0, G = 0, B = 0;
if (wl >= 380 && wl < 440) {
R = -(wl - 440) / (440 - 380);
G = 0;
B = 1;
} else if (wl >= 440 && wl < 490) {
R = 0;
G = (wl - 440) / (490 - 440);
B = 1;
} else if (wl >= 490 && wl < 510) {
R = 0;
G = 1;
B = -(wl - 510) / (510 - 490);
} else if (wl >= 510 && wl < 580) {
R = (wl - 510) / (580 - 510);
G = 1;
B = 0;
} else if (wl >= 580 && wl < 645) {
R = 1;
G = -(wl - 645) / (645 - 580);
B = 0;
} else if (wl >= 645 && wl <= 750) {
R = 1;
G = 0;
B = 0;
}
// Intensity correction near vision limits
let factor = 1.0;
if (wl > 700) {
factor = 0.3 + 0.7 * (750 - wl) / (750 - 700);
} else if (wl < 420) {
factor = 0.3 + 0.7 * (wl - 380) / (420 - 380);
}
R = Math.round(R * factor * 255);
G = Math.round(G * factor * 255);
B = Math.round(B * factor * 255);
return [R, G, B];
}