{"id":310,"date":"2009-06-08T11:47:53","date_gmt":"2009-06-08T18:47:53","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1510"},"modified":"2009-06-08T11:47:53","modified_gmt":"2009-06-08T18:47:53","slug":"strange-bitmapdata-drawing-instructions","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=310","title":{"rendered":"Strange BitmapData Drawing Instructions"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ instructions\r\nvar ins:Array = &#x5B;0,\r\n-1, 100, 200, 3, 100, 0, 50, 1, 100, -1, 100, 150, 0, 50,\r\n-1, 160, 200, 0, 50, 3, 50, 2, 50, 3, 50, 0, 50,\r\n-1, 220, 200, 0, 50, 3, 50, 2, 50, -1, 270, 150, 3, 50, 2, 50];\r\n\r\nvar canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);\r\naddChild(new Bitmap(canvas));\r\n\r\nvar index:int = 0;\r\nvar penX:int = 0;\r\nvar penY:int = 0;\r\n\r\nread();\r\n\r\nfunction read():void{\r\n\tvar i:int, xp:int, yp:int;\r\n\tif (ins&#x5B;index+1] == -1){\r\n\t\t index++;\r\n\t\txp = ins&#x5B;++index];\r\n\t\typ = ins&#x5B;++index];\r\n\t}else{\r\n\t\txp = penX;\r\n\t\typ = penY;\r\n\t}\r\n\tvar dir:int = ins&#x5B;index];\r\n\tvar leng:int = ins&#x5B;++index];\r\n\tfor (i = 0; i&lt;leng; i++){\r\n\t\tif (dir == 0){\r\n\t\t\txp += 1;\r\n\t\t}else if (dir == 1){\r\n\t\t\typ += 1;\r\n\t\t}else if (dir == 2){\r\n\t\t\txp -= 1;\r\n\t\t}else if (dir == 3){\r\n\t\t\typ -= 1;\r\n\t\t}\r\n\t    canvas.setPixel(xp, yp, 0x000000);\r\n     }\r\n\t penX = xp;\r\n\t penY = yp;\r\n\tif (index &lt; ins.length){\r\n\t\tread();\r\n\t}\r\n}\r\n<\/pre>\n<p>I was thinking today about alternative ways to represent drawings and I wrote this snippet while brainstorming. The function read() looks through an array of instructions (ins[]) and draws an image. In this case the letters AS3 are drawn to a Bitmap using setPixel().<\/p>\n<p>The instructions work like this:<\/p>\n<p>The first value of the array is ignored &#8211; it can be set to 0, or used to store some other info.<br \/>\nIf the next value is a -1, the following two values in the array are used as a sort of moveTo().<br \/>\nThis is followed by a direction value 0 = right, 1 = down, 2 = left, 3 = up.<br \/>\nLastly there is a length value in pixels.<\/p>\n<p><em>If no -1 is found, the read() function assumes that the value is for a direction.<\/em><\/p>\n<p>So to draw a line from (100, 100) to (150, 100) you would write:<br \/>\n<code>[0, -1, 100, 100, 0, 50]<\/code><\/p>\n<p>To then draw a line down 50 pixels from the endpoint of the previous line you would add 1 (move down), 50 (length 50)&#8230; so you to end up with:<br \/>\n<code>[0, -1, 100, 100, 0, 50, 1, 50]<\/code><\/p>\n<p>and so on&#8230;<\/p>\n<p>I&#8217;m not sure really where I was going with this, but it was fun to write&#8230; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ instructions var ins:Array = &#x5B;0, -1, 100, 200, 3, 100, 0, 50, 1, 100, -1, 100, 150, 0, 50, -1, 160, 200, 0, 50, 3, 50, 2, 50, 3, 50, 0, 50, -1, 220, 200, 0, 50, 3, 50, 2, 50, -1, 270, 150, 3, 50, 2, 50]; var canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,26],"tags":[3,14,4],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-misc","category-setpixel","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=310"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}