int N = 1000; // arbitrary limit int x[] = new int[N]; int y[] = new int[N]; int count = 0; boolean active = true; float r = 3; // radius for control points void setup() { size(600,400); strokeWeight(2); } void draw() { background(204); if (active) { fill(0); beginShape(); for (int j=0; j < count; j++) { vertex(x[j], y[j]); ellipse(x[j], y[j], 2*r, 2*r); } noFill(); endShape(); } else { fill(255, 127, 0); beginShape(); for (int j=0; j < count; j++) { vertex(x[j], y[j]); } endShape(CLOSE); } } void mousePressed() { if (active) { if (count == 0 || dist(x[0],y[0],mouseX,mouseY) > r) { // add a new point x[count] = mouseX; y[count] = mouseY; count++; } else { // close the polygon active = false; } } } void keyPressed() { if (key == 'r') { count = 0; active = true; } }