{"id":240,"date":"2011-11-13T23:21:00","date_gmt":"2011-11-14T07:21:00","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=240"},"modified":"2011-11-13T23:21:00","modified_gmt":"2011-11-14T07:21:00","slug":"how-to-draw-the-usa-flag","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/11\/how-to-draw-the-usa-flag\/","title":{"rendered":"How to Draw the USA Flag"},"content":{"rendered":"<div class=\"exr\"><img decoding=\"async\" src=\"\/img\/old_glory.jpg\" \/><\/div>\n<p>This is one a series of posts on how to draw world flags. Finding the specifications for these flags, and attempting to reproduce the flag perfectly, according to the specifications, is a great way to teach yourself basic graphics programming.<\/p>\n<p>Previously: <a href=\"\/blog\/2011\/11\/how-to-draw-the-australian-flag\/\">Australia<\/a><\/p>\n<p>Having figured out how to draw stars, and having drawn plenty of stripes, we are ready to tackle the Stars and Stripes.  The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Flag_of_the_United_States#Specifications\">specification on Wikipedia<\/a> provides a handy chart and some useful measurements, which I&#8217;ve reworded a bit for consistency (I always use &#8216;width&#8217; for horizontal measurements and height for &#8216;vertical&#8217; measurements).<\/p>\n<div class=\"exc\"><img decoding=\"async\" src=\"\/img\/usa_flag_spec.png\"><\/div>\n<ul>\n<li>Hoist (height) of the flag: A = 1.0\n<li>Fly (width) of the flag: B = 1.9\n<li>Hoist (height) of the Union: C = 0.5385 (A \u00d7 7\/13, spanning seven stripes)\n<li>Fly (width) of the Union: D = 0.76 (B \u00d7 2\/5, two fifths of the flag width)\n<li>E = F = 0.0538 (C\/10, One tenth of the height of the Union)\n<li>G = H = 0.0633 (D\/12, One twelfth of the width of the Union)\n<li>Diameter of star: K = 0.0616\n<li>Height of stripe: L = 0.0769 (A\/13, One thirteenth of the flag height)\n<\/ul>\n<p>I&#8217;m going to base all my numbers on the height (or hoist), which you can change at the top of the script.<\/p>\n<pre>\r\nint kHeight = 260;\r\n<\/pre>\n<p>My colors come from the wikipedia specification.<\/p>\n<pre>\r\ncolor blueColor  = #3C3B6E;\r\ncolor whiteColor = #FFFFFF;\r\ncolor redColor   = #B22234;\r\n<\/pre>\n<p>When setting the size of the sketch, I use the specified 1.9:1 aspect ratio.<\/p>\n<pre>\r\n  size(round(kHeight*1.9), kHeight);\r\n<\/pre>\n<p>In the draw function, we set some useful measurements, that come from the specification. <\/p>\n<pre>\r\n  int  nbrStripes = 13;\r\n  float stripeHeight = height\/(float) nbrStripes;\r\n  float unionWidth = width * 2\/5.0;\r\n  float unionHeight= height * 7\/13.0;\r\n<\/pre>\n<p>We first fill the frame with white, and then draw our 13 stripes.<\/p>\n<pre>\r\n  background(whiteColor);\r\n\r\n  fill(redColor);\r\n  for (int i = 0; i < nbrStripes; i += 2) {\r\n    rect(0,stripeHeight*i, width, stripeHeight);\r\n  }\r\n<\/pre>\n<p>This produces the following image.<\/p>\n<div class=\"exl \" style=\"width:190px;\">\n<div class=\"figborder\" style=\"width:190px;\"><script type=\"application\/processing\">\/\/ Flag of United States of America - Jim Bumgardner\n\/\/ for Processing and Processing.js\n\n\/\/ Reference: http:\/\/en.wikipedia.org\/wiki\/Flag_of_the_United_States#Specifications\n\nint kHeight = 100; \/\/ The only number that needs changing\n\ncolor blueColor  = #3C3B6E;\ncolor whiteColor = #FFFFFF;\ncolor redColor   = #B22234;\n\nvoid setup()\n{\n  size(round(kHeight*1.9), kHeight);\n  noStroke();\n  smooth();\n  noLoop();\n}\n\n\/\/ Specification does not specify type of star (star or aster), but most flags use\n\/\/ a regular star (using 5 construction points, instead of 10)\n\/\/ so that's what I'm using here.\n\nvoid drawStar(int nbrPoints, float cx, float cy, float odiam, int skipPoints )\n{\n  float orad = odiam \/ 2.0;\n\n  pushMatrix();\n  translate(cx, cy);\n  rotate(-PI\/2); \/\/ Point upwards  \n\n  float  a = TWO_PI \/ nbrPoints;\n\n  beginShape();\n  int  n = 0;\n  for (int i = 0; i < nbrPoints; ++i) {\n    vertex( cos( a * n) * orad, sin( a * n) * orad);\n    n += skipPoints;\n  }\n  endShape(CLOSE);\n  popMatrix();\n}\n\nvoid draw()\n{\n  int  nbrStripes = 13;\n  float stripeWidth = height\/(float) nbrStripes;\n  float unionWidth = width * 2\/5.0;\n  float unionHeight= height * 7\/13.0;\n\n  background(whiteColor);\n\n  fill(redColor);\n  for (int i = 0; i < nbrStripes; i += 2) {\n    rect(0,stripeWidth*i, width, stripeWidth);\n  }\n\n\/*\n  fill(blueColor);\n  rect(0,0,unionWidth, unionHeight);\n\n  fill(whiteColor);\n  \n  float star_hmargin = unionWidth\/12.0;\n  float star_hspacing = unionWidth\/6.0;\n  float star_vmargin = unionHeight\/10.0;\n  float star_vspacing = unionHeight\/10.0;\n  float star_diameter = height * 0.0616;\n\n  for (int y = 0; y < 9; ++y) {\n    int nbrStars = 5 + ((y & 1) == 0? 1 : 0);\n    float lm = star_hmargin * ((y & 1) == 0? 1 : 2);\n    for (int x = 0; x < nbrStars; ++x) {\n      float tm = star_vmargin; \/\/  * ((x & 1) == 0? 1 : 2);\n      float px = lm + star_hspacing * x;\n      float py = tm + star_vspacing * y;\n      drawStar(5, px, py, star_diameter, 2);\n    }\n  } *\/\n}\n\n\n<\/script><\/div>\n<\/div>\n<p>Notice that I could have drawn the top stripes short, but it needlessly complicates the code.  It's simpler to just draw them all the way across, and then draw the blue union on top of them, as I do in the next step.<br clear=\"all\" \/><\/p>\n<div class=\"exl \" style=\"width:190px;\">\n<div class=\"figborder\" style=\"width:190px;\"><script type=\"application\/processing\">\/\/ Flag of United States of America - Jim Bumgardner\n\/\/ for Processing and Processing.js\n\n\/\/ Reference: http:\/\/en.wikipedia.org\/wiki\/Flag_of_the_United_States#Specifications\n\nint kHeight = 100; \/\/ The only number that needs changing\n\ncolor blueColor  = #3C3B6E;\ncolor whiteColor = #FFFFFF;\ncolor redColor   = #B22234;\n\nvoid setup()\n{\n  size(round(kHeight*1.9), kHeight);\n  noStroke();\n  smooth();\n  noLoop();\n}\n\n\/\/ Specification does not specify type of star (star or aster), but most flags use\n\/\/ a regular star (using 5 construction points, instead of 10)\n\/\/ so that's what I'm using here.\n\nvoid drawStar(int nbrPoints, float cx, float cy, float odiam, int skipPoints )\n{\n  float orad = odiam \/ 2.0;\n\n  pushMatrix();\n  translate(cx, cy);\n  rotate(-PI\/2); \/\/ Point upwards  \n\n  float  a = TWO_PI \/ nbrPoints;\n\n  beginShape();\n  int  n = 0;\n  for (int i = 0; i < nbrPoints; ++i) {\n    vertex( cos( a * n) * orad, sin( a * n) * orad);\n    n += skipPoints;\n  }\n  endShape(CLOSE);\n  popMatrix();\n}\n\nvoid draw()\n{\n  int  nbrStripes = 13;\n  float stripeWidth = height\/(float) nbrStripes;\n  float unionWidth = width * 2\/5.0;\n  float unionHeight= height * 7\/13.0;\n\n  background(whiteColor);\n\n  fill(redColor);\n  for (int i = 0; i < nbrStripes; i += 2) {\n    rect(0,stripeWidth*i, width, stripeWidth);\n  }\n\n  fill(blueColor);\n  rect(0,0,unionWidth, unionHeight);\n\n\/*\n  fill(whiteColor);\n  \n  float star_hmargin = unionWidth\/12.0;\n  float star_hspacing = unionWidth\/6.0;\n  float star_vmargin = unionHeight\/10.0;\n  float star_vspacing = unionHeight\/10.0;\n  float star_diameter = height * 0.0616;\n\n  for (int y = 0; y < 9; ++y) {\n    int nbrStars = 5 + ((y & 1) == 0? 1 : 0);\n    float lm = star_hmargin * ((y & 1) == 0? 1 : 2);\n    for (int x = 0; x < nbrStars; ++x) {\n      float tm = star_vmargin; \/\/  * ((x & 1) == 0? 1 : 2);\n      float px = lm + star_hspacing * x;\n      float py = tm + star_vspacing * y;\n      drawStar(5, px, py, star_diameter, 2);\n    }\n  } *\/\n}\n\n\n<\/script><\/div>\n<\/div>\n<pre>\r\n  fill(blueColor);\r\n  rect(0,0,unionWidth, unionHeight);\r\n<\/pre>\n<p><br clear=\"all\" \/><br \/>\nFinally, we draw the stars.  This is the only complicated bit, as the stars are interspaced like bricks.<\/p>\n<pre>\r\n  fill(whiteColor);\r\n  \r\n  float star_hmargin = unionWidth\/12.0;\r\n  float star_hspacing = unionWidth\/6.0;\r\n  float star_vmargin = unionHeight\/10.0;\r\n  float star_vspacing = unionHeight\/10.0;\r\n  float star_diameter = height * 0.0616;\r\n\r\n  for (int y = 0; y < 9; ++y) {\r\n    int nbrStars = 5 + ((y+1) &#038; 1);\r\n    float lm = star_hmargin * (1 + (y &#038; 1));\r\n    for (int x = 0; x < nbrStars; ++x) {\r\n      float tm = star_vmargin;\r\n      float px = lm + star_hspacing * x;\r\n      float py = tm + star_vspacing * y;\r\n      drawStar(5, px, py, star_diameter, 2);\r\n    }\r\n  }\r\n<\/pre>\n<p>The number of stars in each row (and the left margin of each row) is different, depending on whether the rows are even or odd.  I used an old hacker's trick to determine even\/oddness and produce the correct values.  (y & 1) extracts the 1s bit from the binary representation of the number y.  It's 0 when the number is even, 1 when the number is odd.<\/p>\n<p>This tests if the 1's bit on the number is cleared, as it is for even numbers. This same trick is also used to determine the left-margin (lm).<\/p>\n<p>The stars are drawn by the routine drawStar, which I created in <a href=\"\/blog\/2011\/11\/stars-and-asters\/\">a previous tutorial on star drawing<\/a>.<\/p>\n<p>And now we have our flag:<\/p>\n<div class=\"\" style=\"width:494px;\">\n<div class=\"figborder\" style=\"width:494px;\"><script type=\"application\/processing\">\/\/ Flag of United States of America - Jim Bumgardner\n\/\/ for Processing and Processing.js\n\n\/\/ Reference: http:\/\/en.wikipedia.org\/wiki\/Flag_of_the_United_States#Specifications\n\nint kHeight = 260; \/\/ The only number that needs changing\n\ncolor blueColor  = #3C3B6E;\ncolor whiteColor = #FFFFFF;\ncolor redColor   = #B22234;\n\nvoid setup()\n{\n  size(round(kHeight*1.9), kHeight);\n  noStroke();\n  smooth();\n  noLoop();\n}\n\n\/\/ Specification does not specify type of star (star or aster), but most flags use\n\/\/ a regular star (using 5 construction points, instead of 10)\n\/\/ so that's what I'm using here.\n\nvoid drawStar(int nbrPoints, float cx, float cy, float odiam, int skipPoints )\n{\n  float orad = odiam \/ 2.0;\n\n  pushMatrix();\n  translate(cx, cy);\n  rotate(-PI\/2); \/\/ Point upwards  \n\n  float  a = TWO_PI \/ nbrPoints;\n\n  beginShape();\n  int  n = 0;\n  for (int i = 0; i < nbrPoints; ++i) {\n    vertex( cos( a * n) * orad, sin( a * n) * orad);\n    n += skipPoints;\n  }\n  endShape(CLOSE);\n  popMatrix();\n}\n\nvoid draw()\n{\n  int  nbrStripes = 13;\n  float stripeHeight = height\/(float) nbrStripes;\n  float unionWidth = width * 2\/5.0;\n  float unionHeight= height * 7\/13.0;\n\n  background(whiteColor);\n\n  fill(redColor);\n  for (int i = 0; i < nbrStripes; i += 2) {\n    rect(0,stripeHeight*i, width, stripeHeight);\n  }\n  fill(blueColor);\n  rect(0,0,unionWidth, unionHeight);\n\n  fill(whiteColor);\n  \n  float star_hmargin = unionWidth\/12.0;\n  float star_hspacing = unionWidth\/6.0;\n  float star_vmargin = unionHeight\/10.0;\n  float star_vspacing = unionHeight\/10.0;\n  float star_diameter = height * 0.0616;\n\n  for (int y = 0; y < 9; ++y) {\n    int nbrStars = 5 + ((y+1) & 1);\n    float lm = star_hmargin * (1 + (y & 1));\n    for (int x = 0; x < nbrStars; ++x) {\n      float tm = star_vmargin;\n      float px = lm + star_hspacing * x;\n      float py = tm + star_vspacing * y;\n      drawStar(5, px, py, star_diameter, 2);\n    }\n  }\n}\n\n\n<\/script><\/div>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=flags\/Flag_of_United_States\">source<\/a><\/div>\n<\/div>\n<p>Previously: <a href=\"\/blog\/2011\/11\/how-to-draw-the-australian-flag\/\">Australia<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is one a series of posts on how to draw world flags. Finding the specifications for these flags, and attempting to reproduce the flag perfectly, according to the specifications, is a great way to teach yourself basic graphics programming. Previously: Australia Having figured out how to draw stars, and having drawn plenty of stripes, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,6,8,14],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-easy","tag-flag","tag-intermediate","tag-stars"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/comments?post=240"}],"version-history":[{"count":10,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/240\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}