xxxxxxxxxx
53
// Since they don't know trigonometry, let's draw points using the simple coordinate axes values and basic objects
// p1 p2 p3 are the three vertices of the triangle
let p1 = {
x: 200,
y: 50,
};
let p2 = {
x: 50,
y: 350,
};
let p3 = {
x: 350,
y: 350,
};
let ran; // Global Variable to be used to keep a track of the random point
function setup() {
createCanvas(400, 400);
background("black");
// initially giving this some random position on the canvas
ran = {
x: random(width),
y: random(height),
};
}
function draw() {
// Drawing the triangle
strokeWeight(5);
stroke("hotpink");
point(p1.x, p1.y);
point(p2.x, p2.y);
point(p3.x, p3.y);
// for loop is to make code render faster (buffering)
for (i = 0; i < 10; i++) {
strokeWeight(1);
stroke("aqua");
point(ran.x, ran.y); //Drawing the random point
attr = random([p1, p2, p3]); //Selecting which point will the random point be attracted towards, in the next iteration of the draw function
// assigning the mid-point value between the current ran and attr points as the next random point
ran.x = (ran.x + attr.x) / 2;
ran.y = (ran.y + attr.y) / 2;
}
}