xxxxxxxxxx
88
let points = []; // Array to store random points around the heart
let numPoints = 5000; // Total number of random points to try
let heartPoints = []; // Array to store points on the heart
let maxDistance = 80; // Maximum distance for point generation
let sigma = 10; // Standard deviation for the Gaussian function
function setup() {
createCanvas(400, 400);
// Generate points on the heart to use as a reference
for (let t = 0; t < TWO_PI; t += 0.01) {
let x = heartX(t);
let y = heartY(t);
heartPoints.push(createVector(x + width / 2, -y + height / 2)); // Center the heart
}
// Generate random points around the heart
for (let i = 0; i < numPoints; i++) {
let x = random(-200, 200); // Sample within a bounding box
let y = random(-200, 200);
// Check distance to the heart function
let closestDistance = Infinity;
for (let heartPoint of heartPoints) {
let d = dist(x + width / 2, -y + height / 2, heartPoint.x, heartPoint.y);
if (d < closestDistance) {
closestDistance = d;
}
}
// Calculate probability using Gaussian decay
let probability = gaussianProbability(closestDistance);
// Add point based on probability
if (random() < probability) {
points.push(createVector(x + width / 2, -y + height / 2)); // Store the point
}
}
}
function draw() {
background(255);
// Draw the heart shape
stroke(255, 0, 0); // Red color for the heart
strokeWeight(2);
noFill();
beginShape();
for (let t = 0; t < TWO_PI; t += 0.01) {
let x = heartX(t);
let y = heartY(t);
vertex(x + width / 2, -y + height / 2); // Center the heart
}
endShape(CLOSE);
// Draw the random points
fill(255, 0, 0); // Red color for the points
noStroke();
for (let point of points) {
ellipse(point.x, point.y, 5, 5); // Draw each point
}
}
// Heart shape function
function heartX(t) {
return 160 * pow(sin(t), 3);
}
function heartY(t) {
return 150 * cos(t) - 55 * cos(2 * t) - 30 * cos(3 * t) - 10 * cos(4 * t);
}
// Gaussian probability function
function gaussianProbability(distance) {
if (distance > maxDistance) {
return 0; // No points beyond maxDistance
}
return exp(-pow(distance, 2) / (2 * pow(sigma, 2))); // Gaussian decay
}
// Quadratic probability function
function quadraticProbability(distance) {
if (distance > maxDistance) {
return 0; // No points beyond maxDistance
}
return max(0, 1 - pow(distance / maxDistance, 2)); // Quadratic decay
}