xxxxxxxxxx
76
let numPoints = 3000; // number of points to plot
const a0 = 100000; // Bohr radius in pixels
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(PI); // Polar angle (0 to PI)
let phi = random(TWO_PI); // Azimuthal angle (0 to 2PI)
// Convert spherical coordinates to Cartesian (x, y, z)
let x = r * sin(theta) * cos(phi);
let y = r * sin(theta) * sin(phi);
let z = r * cos(theta);
// Calculate probability density with spherical harmonics for l=1, m=0
let probDensity = calculateProbabilityDensity(r, theta);
// 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
}
}
// Probability density with spherical harmonics for l=1, m=0
function calculateProbabilityDensity(r, theta) {
let radialPart = (1 / (PI * pow(a0, 3))) * exp(-r / a0); // Radial part
let angularPart = cos(theta); // Spherical harmonic Y_1^0
return pow(radialPart * angularPart, 2); // Probability density
}