void setup() { size(600, 600); background(255); noLoop(); } // setup()
void draw() { // Draw a polygon stroke(0); polygon(100, 100, 5, 150); // Draw a filled polygon noStroke(); fill(252, 107, 107); filledPolygon(width/2, height/2, 6, 300); } // draw()
void polygon(float x, float y, int N, float s) { // Draw a polygon of N sides of size, s at <x, y> float r = s/2; float theta = 0.0; float delta = TWO_PI/N; void setup() { size(600, 600); background(255); noLoop(); } // setup()
void draw() { // Draw a polygon stroke(0); polygon(100, 100, 5, 150); // Draw a filled polygon noStroke(); fill(252, 107, 107); filledPolygon(width/2, height/2, 6, 300); } // draw()
void polygon(float x, float y, int N, float s) { // Draw a polygon of N sides of size, s at <x, y> float r = s/2; float theta = 0.0; float delta = TWO_PI/N; pushMatrix(); translate(x, y); float x1 = r * cos(theta); float y1 = r * sin(theta); for (int i=1; i <= N; i++) { theta = theta + delta; float x2 = r*cos(theta); float y2 = r* sin(theta); stroke(0); line(x1, y1, x2, y2); x1 = x2; y1 = y2;
} popMatrix(); } // polygon()
void filledPolygon(float x, float y, int N, float s) { // Draw a fillable polygon of N sides of size, s at <x, y> float r = s/2; float theta = 0.0; float delta = TWO_PI/N; pushMatrix(); translate(x, y); beginShape(); for (int i=1; i <= N+1; i++) { theta = theta + delta; float x1 = r*cos(theta); float y1 = r*sin(theta); vertex(x1, y1); } endShape(CLOSE); popMatrix(); } // filledPolygon()