Generate interactive benzene and its structure simulation with working controls and realistic physics. Output only JavaScript code.
javascript
// Benzene simulation with physics-based vibrating ring and sliders/buttons controlling parameters
// Global mode p5.js
let canvasSize = 400;
// Sliders and buttons
let bondLengthSlider, vibrationAmpSlider, vibrationFreqSlider, dampingSlider;
let resetBtn, pauseBtn;
let paused = false;
let time = 0;
let dt = 0.016; // ~60fps time step in seconds
// Benzene parameters
const N = 6; // 6 carbon atoms in benzene ring
let atoms = []; // positions of atoms (vectors)
let velocities = []; // velocities of atoms (vectors)
let baseBondLength = 100; // default bond length in pixels (slider controls scale)
let vibrationAmplitude = 5; // max radial vibration amplitude (slider)
let vibrationFrequency = 2; // Hz (slider)
let damping = 0.05; // damping coefficient (slider)
// Center of benzene ring
let center;
function setup() {
createCanvas(canvasSize, canvasSize);
frameRate(60);
textFont('monospace', 14);
center = createVector(width / 2, height / 2);
// Create sliders with realistic ranges
// Bond length: 50 to 150 px, default 100 px, step 1
bondLengthSlider = createSlider(50, 150, 100, 1);
bondLengthSlider.position(10, 420);
// Vibration amplitude: 0 to 20 px, default 5 px, step 0.1
vibrationAmpSlider = createSlider(0, 20, 5, 0.1);
vibrationAmpSlider.position(200, 420);
// Vibration frequency: 0 to 10 Hz, default 2 Hz, step 0.1
vibrationFreqSlider = createSlider(0, 10, 2, 0.1);
vibrationFreqSlider.position(10, 450);
// Damping: 0 to 0.2, default 0.05, step 0.005
dampingSlider = createSlider(0, 0.2, 0.05, 0.005);
dampingSlider.position(200, 450);
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(10, 480);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause');
pauseBtn.position(100, 480);
pauseBtn.mousePressed(togglePause);
resetSim();
}
function resetSim() {
// Initialize atom positions in perfect hexagon
atoms = [];
velocities = [];
baseBondLength = bondLengthSlider.value();
vibrationAmplitude = vibrationAmpSlider.value();
vibrationFrequency = vibrationFreqSlider.value();
damping = dampingSlider.value();
for (let i = 0; i < N; i++) {
let angle = TWO_PI * i / N - PI / 2; // start from top
let x = center.x + baseBondLength * cos(angle);
let y = center.y + baseBondLength * sin(angle);
atoms.push(createVector(x, y));
velocities.push(createVector(0, 0));
}
time = 0;
paused = false;
pauseBtn.html('Pause');
}
function togglePause() {
paused = !paused;
pauseBtn.html(paused ? 'Play' : 'Pause');
}
function draw() {
background(245);
// Read slider values each frame for interactivity
baseBondLength = bondLengthSlider.value();
vibrationAmplitude = vibrationAmpSlider.value();
vibrationFrequency = vibrationFreqSlider.value();
damping = dampingSlider.value();
// Physics update
if (!paused) {
// Time step scaled for stable simulation
// We'll simulate radial vibrations of atoms around the ring center
// Each atom behaves like a mass on a spring with equilibrium radius = baseBondLength
// The vibration is driven by a sinusoidal forcing term at vibrationFrequency
// For each atom:
// radial displacement r_i = distance(atom_i, center)
// velocity radial component v_r_i = projection of velocity on radial direction
// acceleration = -k*(r_i - baseBondLength) - damping*v_r_i + driving force (sinusoidal)
// We'll approximate k from vibration frequency: f = (1/2pi)*sqrt(k/m)
// Assume m=1 for simplicity, so k = (2pi*f)^2
let k = sq(TWO_PI * vibrationFrequency);
let m = 1;
for (let i = 0; i < N; i++) {
let atom = atoms[i];
let vel = velocities[i];
// Vector from center to atom
let rVec = p5.Vector.sub(atom, center);
let r = rVec.mag();
// Unit radial vector
let rHat = rVec.copy().normalize();
// Radial velocity component
let v_r = p5.Vector.dot(vel, rHat);
// Spring force magnitude (Hooke's law)
let springForce = -k * (r - baseBondLength);
// Damping force magnitude
let dampingForce = -damping * v_r;
// Driving force magnitude (sinusoidal forcing)
// Phase offset so atoms vibrate with phase difference to simulate ring modes
// We'll use mode 1: all atoms in phase (simplest)
let drivingForce = vibrationAmplitude * k * sin(TWO_PI * vibrationFrequency * time);
// Total radial acceleration
let a_r = (springForce + dampingForce + drivingForce) / m;
// Convert radial acceleration to vector
let aVec = rHat.copy().mult(a_r);
// Update velocity and position using Euler integration
vel.add(p5.Vector.mult(aVec, dt));
atom.add(p5.Vector.mult(vel, dt));
// Update arrays
velocities[i] = vel;
atoms[i] = atom;
}
time += dt;
}
// Draw benzene structure
stroke(0);
strokeWeight(2);
fill(255);
// Draw bonds (lines between atoms)
for (let i = 0; i < N; i++) {
let a1 = atoms[i];
let a2 = atoms[(i + 1) % N];
line(a1.x, a1.y, a2.x, a2.y);
}
// Draw atoms (carbon atoms as circles)
fill(50, 100, 200);
noStroke();
for (let i = 0; i < N; i++) {
let a = atoms[i];
circle(a.x, a.y, 20);
}
// Draw hydrogens (attached to each carbon, further out)
stroke(150);
strokeWeight(1);
fill(255, 200, 200);
for (let i = 0; i < N; i++) {
let c = atoms[i];
let angle = TWO_PI * i / N - PI / 2;
// Hydrogen position is further out along radial direction by ~40 px
let hX = center.x + (baseBondLength + 40) * cos(angle);
let hY = center.y + (baseBondLength + 40) * sin(angle);
line(c.x, c.y, hX, hY);
circle(hX, hY, 12);
}
// Draw center point
fill(0);
noStroke();
circle(center.x, center.y, 6);
// Display parameter values on canvas
fill(0);
noStroke();
textAlign(LEFT, TOP);
text(
`Bond Length: ${baseBondLength.toFixed(1)} px\n` +
`Vibration Amplitude: ${vibrationAmplitude.toFixed(2)} px\n` +
`Vibration Frequency: ${vibrationFrequency.toFixed(2)} Hz\n` +
`Damping: ${damping.toFixed(3)}`,
10, 10
);
}