58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
// Generates a bell curve SVG and writes it to _includes/bell-curve.svg
|
|
// Usage: node scripts/generate-bell-curve.js
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const width = 600;
|
|
const height = 300;
|
|
const padding = { top: 20, right: 20, bottom: 40, left: 20 };
|
|
|
|
const plotW = width - padding.left - padding.right;
|
|
const plotH = height - padding.top - padding.bottom;
|
|
|
|
// Standard normal PDF
|
|
function gaussian(x) {
|
|
return Math.exp(-0.5 * x * x) / Math.sqrt(2 * Math.PI);
|
|
}
|
|
|
|
// Generate points from -4 to 4
|
|
const steps = 200;
|
|
const xMin = -4, xMax = 4;
|
|
const points = [];
|
|
for (let i = 0; i <= steps; i++) {
|
|
const x = xMin + (xMax - xMin) * (i / steps);
|
|
points.push({ x, y: gaussian(x) });
|
|
}
|
|
|
|
const yMax = gaussian(0); // peak value
|
|
|
|
function sx(x) {
|
|
return padding.left + ((x - xMin) / (xMax - xMin)) * plotW;
|
|
}
|
|
|
|
function sy(y) {
|
|
return padding.top + plotH - (y / yMax) * plotH;
|
|
}
|
|
|
|
// Build the curve path
|
|
const linePath = points.map((p, i) => {
|
|
const cmd = i === 0 ? "M" : "L";
|
|
return `${cmd}${sx(p.x).toFixed(2)},${sy(p.y).toFixed(2)}`;
|
|
}).join(" ");
|
|
|
|
// Build the filled area path (closed along the baseline)
|
|
const areaPath = linePath
|
|
+ ` L${sx(xMax).toFixed(2)},${sy(0).toFixed(2)}`
|
|
+ ` L${sx(xMin).toFixed(2)},${sy(0).toFixed(2)} Z`;
|
|
|
|
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" role="img" aria-label="Standard normal distribution (bell curve)">
|
|
<path d="${areaPath}" fill="#d0d0d0" opacity="0.5"/>
|
|
<path d="${linePath}" fill="none" stroke="currentColor" stroke-width="2"/>
|
|
<line x1="${sx(xMin)}" y1="${sy(0)}" x2="${sx(xMax)}" y2="${sy(0)}" stroke="currentColor" stroke-width="1"/>
|
|
</svg>`;
|
|
|
|
const outPath = path.join(__dirname, "..", "_includes", "bell-curve.svg");
|
|
fs.writeFileSync(outPath, svg);
|
|
console.log(`Wrote ${outPath}`);
|