/** Bryn Mawr College, Department of Computer Science
Draw a regular polygon with a given radius and number of sides */ void setup() { size(500, 500); smooth(); } // setup() void draw() { background(0); //strokeWeight(3); //stroke(255); noStroke(); fill(10, 193, 97); //drawFilledPolygon(width/2, height/2, 200, 8); //drawPolygon(width/2, height/2, 200, 8, s); drawFilledStar(width/2, height/2, 200, 6); } // draw() void drawPolygon(float cx, float cy, float radius, int vertices) { // Draw a regular polygon centered at with given radius // and number of sides (vertices) float theta = 0, delta = 2*PI/vertices; float x1 = cx + radius*cos(theta); float y1 = cy + radius*sin(theta); float x2, y2; for (int i=0; i < vertices; i++) { theta += delta; x2 = cx + radius*cos(theta); y2 = cy + radius*sin(theta); line(x1, y1, x2, y2); x1 = x2; y1 = y2; } } // drawPolygon() void drawFilledPolygon(float cx, float cy, float radius, int vertices) { // Draw a regular polygon centered at with given radius and // number of sides, vertices // The polygon will be filled with the set fill color prior to the call float theta = 0, delta = 2*PI/vertices; float x1 = cx + radius*cos(theta); float y1 = cy + radius*sin(theta); float x2, y2; beginShape(); vertex(x1, y1); for (int i=0; i < vertices; i++) { theta += delta; x2 = cx + radius*cos(theta); y2 = cy + radius*sin(theta); vertex(x2, y2); } endShape(CLOSE); } // drawFilledPolygon() void drawFilledStar(float cx, float cy, float radius, int points) { // Draw a regular star centered at with given radius and // number of points // The star will be filled with the set fill color prior to the call int vertices = points * 2; float theta = 0, delta = 2*PI/vertices; float inner = radius/2; float x1 = cx + radius*cos(theta); float y1 = cy + radius*sin(theta); float x2, y2; beginShape(); vertex(x1, y1); for (int i=0; i < vertices; i++) { // outer point theta += delta; x2 = cx + inner*cos(theta); y2 = cy + inner*sin(theta); vertex(x2, y2); x1 = x2; y1 = y2; // inner point theta += delta; x2 = cx + radius*cos(theta); y2 = cy + radius*sin(theta); //line(x1, y1, x2, y2); vertex(x2, y2); x1 = x2; y1 = y2; } endShape(CLOSE); } // drawFilledStar()