// draw polygons using lines void setup() { // all sketch setup goes here size(400, 400); smooth(); background(255); } // setup() void draw() { } // draw() void mousePressed() { drawPolygon(mouseX, mouseY, random(20, 80), int(random(3, 12))); } // mousePressed() /** Draw a polygon based on the center point, size and number of sides. * * @param cx x coordinate * @param cy y coordinate * @param sz size * @param nSides number of sides */ void drawPolygon(float cx, float cy, float sz, int nSides) { // draw a hexagon centered at cx, cy float x1, y1, x2, y2; float radius = sz/2; float theta = 0.0; float delta = 360.0/nSides; point(cx, cy); // compute x1, y1 x1 = cx + radius; y1 = cy; for (int i = 0; i < nSides; i++) { theta = theta + delta; x2 = cx + radius * cos(radians(theta)); y2 = cy - radius * sin(radians(theta)); line(x1, y1, x2, y2); // copy x2, y2 into x1, y1 x1 = x2; y1 = y2; } } // drawPolygon