Published
Edited
May 20, 2018
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function generateDistribution(triangles) {
const totalArea = triangles.reduce((sum, triangle) => sum + getTriangleArea(triangle), 0);
const cumulativeDistribution = [];
for (let i = 0; i < triangles.length; i++) {
const lastValue = cumulativeDistribution[i - 1] || 0;
const nextValue = lastValue + getTriangleArea(triangles[i]) / totalArea;
cumulativeDistribution.push(nextValue);
}
// [area1, area1 + aera2, area1 + area2 + area3, ...]
return cumulativeDistribution;
}
Insert cell
Insert cell
function selectRandomTriangle(triangles) {
const cumulativeDistribution = generateDistribution(triangles);
const rnd = Math.random();
const index = cumulativeDistribution.findIndex(v => v > rnd);
return triangles[index];
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more