xxxxxxxxxx
83
// the point that will keep moving
let w;
// number of seed points
let N = 3;
// to let the seed points be vertices of a polygon
let radius;
let angle;
// the index of the random point we will pick each turn
let index = 1000;
// to buffer things
let times = 50;
let ratio = 2;
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
translate(width / 2, height / 2);
// draw the polygon
angle = 360 / N;
radius = min(width, height) / 2 - 50;
// draw the vertices
stroke("red");
strokeWeight(10);
for (let i = 0; i < N; i++) {
let p = {
x: radius * cos(i * angle),
y: radius * sin(i * angle)
};
//drawPoint(p);
}
// pick a random point to begin with
w = {
x: random(-width / 2, width / 2),
y: random(-height / 2, height / 2)
};
}
function draw() {
translate(width / 2, height / 2);
for (let i = 0; i < times; i++) {
// draw the point
stroke("white");
strokeWeight(1);
drawPoint(w);
// if it was an even polygon, an additional rule
if (N % 2 == 0) {
let currentIndex = index;
// to enter the loop for the first time, we initiate it to true
let diagonal = true;
while (diagonal) {
index = floor(random(0, N));
diagonal = abs(currentIndex - index) == N / 2;
} // we do not exit the loops except if we do not have a diagonal attraction point
} else {
// if it was odd
index = floor(random(0, N));
}
let attraction = {
x: radius * cos(index * angle),
y: radius * sin(index * angle)
};
//get the coordinates of the new point tx and ty
w.x = (w.x + attraction.x) / ratio;
w.y = (w.y + attraction.y) / ratio;
}
}
function drawPoint(p) {
point(p.x, p.y);
}