Flag Of United States


  // Flag of United States of America - Jim Bumgardner
  // for Processing and Processing.js
  
  // Reference: http://en.wikipedia.org/wiki/Flag_of_the_United_States#Specifications
  
  int kHeight = 260; // The only number that needs changing
  
  color blueColor  = #3C3B6E;
  color whiteColor = #FFFFFF;
  color redColor   = #B22234;
  
  void setup()
  {
    size(round(kHeight*1.9), kHeight);
    noStroke();
    smooth();
    noLoop();
  }
  
  // Specification does not specify type of star (star or aster), but most flags use
  // a regular star (using 5 construction points, instead of 10)
  // so that's what I'm using here.
  
  void drawStar(int nbrPoints, float cx, float cy, float odiam, int skipPoints )
  {
    float orad = odiam / 2.0;
  
    pushMatrix();
    translate(cx, cy);
    rotate(-PI/2); // Point upwards  
  
    float  a = TWO_PI / nbrPoints;
  
    beginShape();
    int  n = 0;
    for (int i = 0; i < nbrPoints; ++i) {
      vertex( cos( a * n) * orad, sin( a * n) * orad);
      n += skipPoints;
    }
    endShape(CLOSE);
    popMatrix();
  }
  
  void draw()
  {
    int  nbrStripes = 13;
    float stripeHeight = height/(float) nbrStripes;
    float unionWidth = width * 2/5.0;
    float unionHeight= height * 7/13.0;
  
    background(whiteColor);
  
    fill(redColor);
    for (int i = 0; i < nbrStripes; i += 2) {
      rect(0,stripeHeight*i, width, stripeHeight);
    }
    fill(blueColor);
    rect(0,0,unionWidth, unionHeight);
  
    fill(whiteColor);
    
    float star_hmargin = unionWidth/12.0;
    float star_hspacing = unionWidth/6.0;
    float star_vmargin = unionHeight/10.0;
    float star_vspacing = unionHeight/10.0;
    float star_diameter = height * 0.0616;
  
    for (int y = 0; y < 9; ++y) {
      int nbrStars = 5 + ((y+1) & 1);
      float lm = star_hmargin * (1 + (y & 1));
      for (int x = 0; x < nbrStars; ++x) {
        float tm = star_vmargin;
        float px = lm + star_hspacing * x;
        float py = tm + star_vspacing * y;
        drawStar(5, px, py, star_diameter, 2);
      }
    }
  }