{"id":269,"date":"2011-11-17T03:06:51","date_gmt":"2011-11-17T11:06:51","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=269"},"modified":"2011-11-20T16:51:48","modified_gmt":"2011-11-21T00:51:48","slug":"how-to-draw-the-canadian-flag","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/11\/how-to-draw-the-canadian-flag\/","title":{"rendered":"How to Draw the Canadian Flag"},"content":{"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.<\/p>\n<p>Previously: <a href=\"\/blog\/2011\/11\/how-to-draw-the-usa-flag\/\">USA<\/a><\/p>\n<p>There are two ways to produce a Canadian flag (or any complicated flag) in Processing: The easy way, and the incredibly hard way.  Of course I chose the latter, but let&#8217;s start with the easy way:<\/p>\n<p><img decoding=\"async\" src=\"\/img\/canada_cheat.jpg\" \/><\/p>\n<p>If you&#8217;re a noob (or simply not a masochist), then what you want to do is grab an SVG of the flag (or maybe just grab a maple leaf).  <a href=\"http:\/\/en.wikipedia.org\/wiki\/File:Flag_of_Canada.svg\">Here&#8217;s one from Wikipedia<\/a>. Drop that in the data folder of your sketch, and then paste in the following code.<\/p>\n<pre>\r\nsize(400,200);\r\nsmooth();\r\nPShape s = loadShape(\"Flag_of_Canada.svg\");\r\nshape(s,0,0,width,height);\r\n<\/pre>\n<p>That&#8217;s it!  Pretty simple, no?  If you&#8217;re not a masochist, read no further.<\/p>\n<p>Now, on to the pain&#8230;  <\/p>\n<p>I&#8217;m not thrilled with using SVG files to produce drawings in Processing.  It makes it hard to do things like change colors, or mess with other internal elements of the drawing.<\/p>\n<p>But I had a feeling the Canadian flag was going to be a pain-in-the-ass, and it was (although it  is significantly simpler than the Flag of Mexico!).  The advanced (non-SVG) sketch I eventually produced is this one: <\/p>\n<div class=\"\" style=\"width:500px;\">\n<div class=\"figborder\" style=\"width:500px;\"><script type=\"application\/processing\">\/\/ Canada Flag- Jim Bumgardner\n\n\/\/ Reference: http:\/\/en.wikipedia.org\/wiki\/Flag_of_Canada#References\n\n\/\/ Issues: Wikipedia, does not precisely list coordinates of maple leaf, but does provide an SVG file.\n\nint kHeight = 250; \/\/ The only number that needs changing\ncolor whiteColor = color(255,255,255);\ncolor redColor = color( #D81E05 );  \/\/ Pantone PMS 485\n\nsize(kHeight*2\/1, kHeight);\nsmooth();\nnoStroke();\n\nfloat stripeWidth = width\/4.0;\n\nbackground(whiteColor);\nfill(redColor);\nrect(0,0, stripeWidth, height);\nrect(stripeWidth*3,0, stripeWidth, height);\n\nscale(height\/300.0); \/\/ Maple Leaf coords assume height is 300, scale accordingly\nbeginShape();\n  \/\/ Maple leaf: This is a translation of the leaf's SVG path in \n  \/\/ http:\/\/upload.wikimedia.org\/wikipedia\/commons\/4\/4b\/Canadian_flag_construction_sheet.svg\n  vertex(319.545,66.4328);\n  bezierVertex(321.762450,70.551350,325.734920,70.169240,329.708000,67.868830);\n  vertex(343.8548,60.25195);\n  vertex(333.3111,118.45875);\n  bezierVertex(331.093650,129.092650,338.207860,129.092650,341.718470,124.494840);\n  vertex(366.40767,95.75624);\n  vertex(370.41586,110.35014);\n  bezierVertex(370.878096,112.266930,372.910350,114.279710,375.959290,113.800470);\n  vertex(407.17899,106.9754);\n  vertex(398.97893,137.9734);\n  bezierVertex(397.223530,144.870850,395.854210,147.726660,400.751150,149.545700);\n  vertex(411.87885,154.98368);\n  vertex(358.13625,200.37368);\n  bezierVertex(356.009130,202.089770,354.934420,205.178400,355.691650,207.974880);\n  vertex(360.39527,224.02448);\n  bezierVertex(341.890870,221.806840,325.301070,219.821650,306.797070,217.606650);\n  bezierVertex(305.152980,217.585296,302.680410,219.164190,302.691750,221.133520);\n  vertex(304.10794,279.80752);\n  vertex(295.11115,279.80752);\n  vertex(297.56293,221.00752);\n  bezierVertex(297.574080,219.038200,294.847720,217.585350,293.203620,217.606700);\n  bezierVertex(274.699620,219.821690,258.109720,221.806890,239.605420,224.024530);\n  vertex(244.30904,207.97493);\n  bezierVertex(245.066268,205.178460,243.991560,202.089830,241.864440,200.373730);\n  vertex(188.12184,154.98373);\n  vertex(199.24974,149.54575);\n  bezierVertex(204.146500,147.726660,202.777170,144.870850,201.021770,137.973450);\n  vertex(192.82171,106.97545);\n  vertex(224.04141,113.80052);\n  bezierVertex(227.090350,114.279764,229.122610,112.266980,229.584840,110.350190);\n  vertex(233.59303,95.75629);\n  vertex(258.28223,124.49489);\n  bezierVertex(261.792840,129.092690,268.907030,129.092690,266.689600,118.458800);\n  vertex(256.1459,60.252);\n  vertex(270.2927,67.86888);\n  bezierVertex(274.265550,70.169290,278.238210,70.551400,280.455700,66.432850);\n  vertex(299.996,28.53725);\n  vertex(319.5454,66.43285);\n  vertex(299.996,28.53725);\n\nendShape();\n\n<\/script><\/div>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=flags\/Flag_of_Canada\">source<\/a><\/div>\n<\/div>\n<p>The problem here, obviously, is the maple leaf.  The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Flag_of_Canada\">specification on Wikipedia<\/a>, and many other websites has a lot to say about how the current 11-point maple leaf was designed in the 1960s by Jacques Saint-Cyr.  What I <i>wasn&#8217;t<\/i> able to find, on Wikipedia or elsewhere, was a clear mathematical specification of how the maple leaf is constructed.  Saint-Cyr wasn&#8217;t using a computer, back in the 60s, so the specification is going to be a reproduction of his design, and not a handy-dandy list of bezier control points.<\/p>\n<p>There are a few SVG files floating around, but they disagree with each other.  Even the image of the flag on the Wikipedia article is slightly different than the outline shown in the construction sheet in the same article.  When the flags produced by these two files are overlaid, you can see how they differ:<\/p>\n<p><img decoding=\"async\" src=\"\/img\/canada_problem.png\" \/><\/p>\n<p>So, given this problem, how does one produce a reasonably accurate flag?<\/p>\n<p>1) I can request the correct government documents from Canada, and then measure the flag accurately with calipers, and produce a set of coordinates.<\/p>\n<p>2) I can scan an accurate photo (from said Government agency) and then use raster->vector software to produce an outline.<\/p>\n<p>3) I can do what I actually did:<\/p>\n<p>I loaded the <a href=\"http:\/\/en.wikipedia.org\/wiki\/File:Canadian_flag_construction_sheet.svg\">construction SVG<\/a> from the Wikipedia article into a text editor, and found the part that corresponds to the maple leaf.  It looks like this:<\/p>\n<pre>\r\nM319.545 66.4328\r\nc2.21745,4.11855 6.18992,3.73644 10.163,1.43603\r\nl14.1468 -7.61688 -10.5437 58.2068\r\nc-2.21745,10.6339 4.89676,10.6339 8.40737,6.03609\r\netc...\r\n<\/pre>\n<p>I then wrote a Perl script that systematically translated these numbers into a list of vertex() and bezierVertex() commands, reading the relevant parts of the <a href=\"http:\/\/www.w3.org\/TR\/SVG\/paths.html\">SVG path specification<\/a> to understand what I was looking at.<\/p>\n<p>(Note: There&#8217;s a more elegant way to convert SVGs and fonts to Processing commands with the <a href=\"http:\/\/www.ricardmarxer.com\/geomerative\/\">Geomerative library<\/a> which I&#8217;ll cover in a future post).<\/p>\n<pre>\r\n  vertex(319.545,66.4328);\r\n  bezierVertex(321.762450,70.551350,325.734920,70.169240,329.708000,67.868830);\r\n  vertex(343.8548,60.25195);\r\n  vertex(333.3111,118.45875);\r\n  bezierVertex(331.093650,129.092650,338.207860,129.092650,341.718470,124.494840);\r\n  \/\/ etc...\r\n<\/pre>\n<p>It won&#8217;t surprise me if it turns out someone has already made a utility to convert SVG files to raw processing code, although I suspect most folks are happy with the loadShape() function, which already exists.  At any rate, I didn&#8217;t see any obvious tools, so I wrote a quick &#038; dirty one.<\/p>\n<p>These Perl-generated vertex commands (which assume a flag height of 300) appear in my sketch.  To get the leaf to scale properly, if the height is changed, I precede these commands with a scale command:<\/p>\n<pre>\r\nscale(height\/300.0);\r\n<\/pre>\n<p>If you&#8217;re drawing a flag at 300, great!  You get 1:1, but you can also draw the flag at 200 or 400 and it will scale to the correct ratio.<\/p>\n<p>The next flag will be South Korea, which is a walk in the park, compared to Canada.  I intend to postpone Mexico, for the time being.<\/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: USA There are two ways to produce a Canadian flag (or any complicated flag) [&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":[],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/269","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=269"}],"version-history":[{"count":10,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/269\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}