{"id":216,"date":"2009-03-11T00:00:46","date_gmt":"2009-03-11T07:00:46","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=939"},"modified":"2009-03-11T00:00:46","modified_gmt":"2009-03-11T07:00:46","slug":"bresenham-line","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=216","title":{"rendered":"Bresenham line"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(400,400, false, 0x000000)))).bitmapData;\r\n\r\ndrawLine(10,10,100,90, 0xFF0000);\r\ndrawLine(100,90,60,80, 0xFF0000);\r\ndrawLine(100,90,95,60, 0xFF0000);\r\n\t\r\nfor (var i:int = 0; i&lt;100; i+=1){\r\n    drawLine(i *4, 100 + i, 200, 390);\r\n}\r\n\/\/ code ported from here:\r\n\/\/ http:\/\/www.edepot.com\/linebenchmark.html\r\nfunction drawLine(x1:int, y1:int, x2:int, y2:int, col:uint = 0xFFFFFF){\r\n\tvar x:int, y:int;\r\n    var dx:int, dy:int;\r\n\tvar incx:int , incy:int\r\n\tvar balance:int;\r\n\r\n\tif (x2 &gt;= x1){\r\n\t\tdx = x2 - x1;\r\n\t\tincx = 1;\r\n\t}else{\r\n\t\tdx = x1 - x2;\r\n\t\tincx = -1;\r\n\t}\r\n\r\n\tif (y2 &gt;= y1){\r\n\t\tdy = y2 - y1;\r\n\t\tincy = 1;\r\n\t}else{\r\n\t\tdy = y1 - y2;\r\n\t\tincy = -1;\r\n\t}\r\n\r\n\tx = x1;\r\n\ty = y1;\r\n\r\n\tif (dx &gt;= dy){\r\n\t\tdy &lt;&lt;= 1;\r\n\t\tbalance = dy - dx;\r\n\t\tdx &lt;&lt;= 1;\r\n\r\n\t\twhile (x != x2){\r\n\t\t\tcanvas.setPixel(x, y, col);\r\n\t\t\tif (balance &gt;= 0){\r\n\t\t\t\ty += incy;\r\n\t\t\t\tbalance -= dx;\r\n\t\t\t}\r\n\t\t\tbalance += dy;\r\n\t\t\tx += incx;\r\n\t\t} \r\n\t\tcanvas.setPixel(x, y, col);\r\n\t}else{\r\n\t\tdx &lt;&lt;= 1;\r\n\t\tbalance = dx - dy;\r\n\t\tdy &lt;&lt;= 1;\r\n\r\n\t\twhile (y != y2){\r\n\t\t\tcanvas.setPixel(x, y, col);\r\n\t\t\tif (balance &gt;= 0){\r\n\t\t\t\tx += incx;\r\n\t\t\t\tbalance -= dy;\r\n\t\t\t}\r\n\t\t\tbalance += dx;\r\n\t\t\ty += incy;\r\n\t\t} \r\n\t\tcanvas.setPixel(x, y, col);\r\n\t}\r\n}\r\n<\/pre>\n<p>This snippet shows Brensenham&#8217;s line drawing algorithm. I ported this implementation from <a href=\"http:\/\/www.edepot.com\/linebenchmark.html\">here<\/a>&#8230; all the line algorithms in that link are easy to port to actionscript. I&#8217;ve messed with them all at some point. <\/p>\n<p>Tomorrow I&#8217;m going to post a super slow line drawing algorithm&#8230; so I figured I&#8217;d post a fast line drawing algorithm today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(400,400, false, 0x000000)))).bitmapData; drawLine(10,10,100,90, 0xFF0000); drawLine(100,90,60,80, 0xFF0000); drawLine(100,90,95,60, 0xFF0000); for (var i:int = 0; i&lt;100; i+=1){ drawLine(i *4, 100 + i, 200, 390); } \/\/ code ported from here: \/\/ http:\/\/www.edepot.com\/linebenchmark.html function drawLine(x1:int, y1:int, x2:int, y2:int, col:uint = 0xFFFFFF){ var x:int, y:int; var dx:int, dy:int; var incx:int , incy:int var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,114,26],"tags":[3,4],"class_list":["post-216","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-graphics-algorithms","category-setpixel","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/216","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=216"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}