xxxxxxxxxx
74
let numPoints = 2000; // number of points to plot
const a0 = 2000; // Bohr radius in pixels, increased for better visibility
let points = []; // to store points and their densities
let angle = 0; // rotation angle
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
// Generate points and calculate probability densities
for (let i = 0; i < numPoints; i++) {
// Generate random spherical coordinates (r, theta, phi)
let r = random(0, 200); // Random radius
let theta = random(TWO_PI); // Random angle theta
let phi = random(PI); // Random angle phi
// Convert spherical coordinates to Cartesian (x, y, z)
let x = r * sin(phi) * cos(theta);
let y = r * sin(phi) * sin(theta);
let z = r * cos(phi);
// Calculate probability density based on radial distance
let probDensity = calculateProbabilityDensity(r);
// Store the point and its properties
points.push({ x: x, y: y, z: z, probDensity: probDensity });
}
}
function draw() {
background(0); // reset the background each time
// Apply a small rotation over time
angle += 0.01;
// Rotate the points in 3D space
let rotatedPoints = points.map(p => {
// Simple 3D rotation (around Y-axis)
let newX = p.x * cos(angle) - p.z * sin(angle);
let newZ = p.x * sin(angle) + p.z * cos(angle);
return { x: newX, y: p.y, z: newZ, probDensity: p.probDensity };
});
// Sort the points based on the Z-coordinate (depth) to create the 3D effect
rotatedPoints.sort((a, b) => a.z - b.z); // Draw farther points first
// Find the min and max probability densities for brightness scaling
let minDensity = min(rotatedPoints.map(p => p.probDensity));
let maxDensity = max(rotatedPoints.map(p => p.probDensity));
// Translate the origin to the center of the canvas
translate(width / 2, height / 2);
// Draw the points
for (let p of rotatedPoints) {
// Map the probability density to brightness
let brightness = map(p.probDensity, minDensity, maxDensity, 0, 255);
// Simulate perspective by scaling points based on depth (Z-coordinate)
let perspective = map(p.z, -200, 200, 0.5, 1.5); // Change point size based on depth
// Draw the point with shades of gray based on brightness (no transparency)
noStroke();
fill(brightness); // Set color based on brightness
ellipse(p.x * perspective, p.y * perspective, 2 * perspective, 2 * perspective); // scale size based on depth
}
}
// Ground state hydrogen atom probability density function
function calculateProbabilityDensity(r) {
return (1 / (PI * pow(a0, 3))) * exp(-2 * r / a0);
}