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 int current = -1; 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(); if (count > 0) { line(mouseX,mouseY,x[count-1],y[count-1]); } } 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; } } else { // check if they are clicking near a vertex for (int j=0; j < count; j++) { if (dist(mouseX,mouseY,x[j],y[j]) <= r) { current = j; } } } } void mouseDragged() { if (current != -1) { x[current] += mouseX - pmouseX; y[current] += mouseY - pmouseY; } } void mouseReleased() { current = -1; } void keyPressed() { if (key == 'r') { count = 0; active = true; } }