xxxxxxxxxx
44
//click mouse to generate a random polygon
var polygons = [];
function setup() {
createCanvas(800, 800);
polygons.length = 0;
for ( i =0; i < polygons.length; i ++){
polygons[i] = new Polygon(100,100,50,3)
}
}
function draw() {
background(220);
for (i = 0; i < polygons.length; i++) {
polygons[i].display();
}
}
class Polygon {
constructor(_x, _y, _r, _npoints) {
this.x = _x;
this.y = _y;
this.r = _r;
this.npoints = _npoints;
}
display() {
var angle = TWO_PI / this.npoints;
noFill();
beginShape();
for (var a = 0; a < TWO_PI; a += angle) {
var sx = this.x + cos(a) * this.r;
var sy = this.y + sin(a) * this.r;
vertex(sx, sy);
}
endShape(CLOSE);
}
}
function mousePressed() {
let r = random(10, 100);
let n = random(3, 20);
polygons.push(new Polygon(mouseX, mouseY, r, n));
}