xxxxxxxxxx
95
let points = [];
let rngEdges = [];
const numPoints = 100;
const minDistance = 30;
function setup() {
createCanvas(170, 350);
// Generate points using Mitchell's best-candidate algorithm
generatePoints();
// Connect the points using the Relative Neighborhood Graph (RNG)
generateRNG();
}
function draw() {
background(220);
// Draw the RNG edges
for (let i = 0; i < rngEdges.length; i++) {
const edge = rngEdges[i];
stroke(0, 50); // Reduced opacity for organic appearance
noFill();
beginShape();
vertex(edge.point1.x, edge.point1.y);
bezierVertex(edge.controlPoint1.x, edge.controlPoint1.y, edge.controlPoint2.x, edge.controlPoint2.y, edge.point2.x, edge.point2.y);
endShape();
}
// Draw the points
for (let i = 0; i < points.length; i++) {
const point = points[i];
fill(0);
noStroke();
ellipse(point.x, point.y, 5, 5);
}
}
function generatePoints() {
for (let i = 0; i < numPoints; i++) {
let valid = false;
let candidate;
// Try generating a valid candidate point
while (!valid) {
valid = true;
candidate = createVector(random(width), random(height));
// Check the minimum distance to existing points
for (let j = 0; j < points.length; j++) {
const existingPoint = points[j];
const distance = p5.Vector.dist(candidate, existingPoint);
if (distance < minDistance) {
valid = false;
break;
}
}
}
points.push(candidate);
}
}
function generateRNG() {
for (let i = 0; i < points.length; i++) {
const point1 = points[i];
for (let j = i + 1; j < points.length; j++) {
const point2 = points[j];
// Add an RNG edge between point1 and point2
rngEdges.push({
point1,
point2,
controlPoint1: generateControlPoint(point1, point2),
controlPoint2: generateControlPoint(point2, point1)
});
}
}
}
function generateControlPoint(point, reference) {
const dir = p5.Vector.sub(reference, point).normalize();
const distance = p5.Vector.dist(reference, point);
const angle = random(PI / 4, PI / 2); // Random angle for organic appearance
const controlDistance = distance * 0.4; // Adjust the control point's distance from the point
// Calculate the control point's position
const controlX = point.x + dir.x * controlDistance * cos(angle);
const controlY = point.y + dir.y * controlDistance * sin(angle);
return createVector(controlX, controlY);
}