xxxxxxxxxx
95
const WIDTH = 800;
const HEIGHT = 800;
const NB_POINTS = 800;
const LEN_PATH = 20;
const MIN_DISTANCE = 25;
const MAX_DISTANCE = 75;
const MAX_TRIES = 400;
let points = [[WIDTH/2, HEIGHT]];
let path = [0];
function computeDistance(p1, p2) {
return sqrt(pow(p2[0] - p1[0], 2) + pow(p2[1] - p1[1], 2));
}
function setup() {
createCanvas(WIDTH, HEIGHT);
for (let i = 0; i < NB_POINTS; i++) {
points.push([random() * WIDTH, random() * HEIGHT])
}
for (let i = 0; i < LEN_PATH; i++) {
let nbTries = 0;
while (true) {
if (nbTries > MAX_TRIES) {
console.log("not found")
path.push(int(random() * NB_POINTS));
nbTries = 0;
break;
}
let drawn = int(random() * NB_POINTS);
//console.log(points[drawn][1])
//console.log(points[path[i]][1]);
if (points[drawn][1] < points[path[i]][1]) {
//console.log(points[drawn])
let dist = computeDistance(points[drawn], points[path[i]]);
//console.log(dist);
if (dist > MIN_DISTANCE && dist < MAX_DISTANCE) {
path.push(drawn);
nbTries = 0;
break;
}
}
nbTries += 1;
}
}
}
function draw() {
background(220);
stroke(30, 30, 100);
strokeWeight(5);
for (let i = 0; i < points.length; i++) {
point(points[i][0], points[i][1]);
}
stroke(60,60, 250);
strokeWeight(3);
for (let i = 0; i < path.length - 1; i++) {
let p1 = points[path[i]];
let p2 = points[path[i + 1]];
line(p1[0], p1[1], p2[0], p2[1]);
}
stroke(255,0, 0);
strokeWeight(5);
for (let i = 0; i < path.length; i++) {
point(points[path[i]][0], points[path[i]][1]);
}
}