{"id":149,"date":"2009-01-09T00:00:50","date_gmt":"2009-01-09T07:00:50","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=492"},"modified":"2009-01-09T00:00:50","modified_gmt":"2009-01-09T07:00:50","slug":"bresenham-circle-no-multiplication","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=149","title":{"rendered":"Bresenham Circle No Multiplication"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar canvas:BitmapData = new BitmapData(400, 400, false, 0xCCCCCC);\r\naddChild(new Bitmap(canvas));\r\n\r\ndrawCircle(200,100, 50);\r\n\r\n\/\/ y, y radius, color\r\nfunction drawCircle(xp:Number,yp:Number, radius:Number, col:uint =0x000000):void {\r\n\tvar balance:int;\r\n\tvar xoff:int;\r\n\tvar yoff:int;\r\n\txoff=0;\r\n\tyoff=radius;\r\n\tbalance=- radius;\r\n\r\n\twhile (xoff &lt;= yoff) {\r\n\t\tcanvas.setPixel(xp+xoff, yp+yoff, col);\r\n\t\tcanvas.setPixel(xp-xoff, yp+yoff, col);\r\n\t\tcanvas.setPixel(xp-xoff, yp-yoff, col);\r\n\t\tcanvas.setPixel(xp+xoff, yp-yoff, col);\r\n\t\tcanvas.setPixel(xp+yoff, yp+xoff, col);\r\n\t\tcanvas.setPixel(xp-yoff, yp+xoff, col);\r\n\t\tcanvas.setPixel(xp-yoff, yp-xoff, col);\r\n\t\tcanvas.setPixel(xp+yoff, yp-xoff, col);\r\n\r\n\t\tif ((balance += xoff++ + xoff) &gt;= 0) {\r\n\t\t\tbalance-=--yoff+yoff;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>The above demos a circle drawing algorithm. This will draw an outlined circle with no fill. This implementation doesn&#8217;t use multiplication.<\/p>\n<p>Using setPixel to draw primitive shapes can be a good learning experience. I found this basic implementation of the Bresenham Circle algorithm a few years back and lost the original link&#8230;. I dug around for a good hour trying to find the original but to no avail. So if someone recognizes this interesting implementation &#8230;. let me know \ud83d\ude42 <\/p>\n<p>The original code was written in java I think.<\/p>\n<p>If your curious. The standard implementation I keep finding online looks something like this:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ ported from http:\/\/www.codeuu.com\/Bresenham_Circle\r\nfunction drawCircle(cx:Number, cy:Number, r:Number, col:uint):void{\r\n\tvar xp:int  = 0, yp:int= r ;\r\n\tvar d:int = 3 - (2 * r);\r\n\t\r\n\twhile(xp &lt;= yp){\r\n\t\t\r\n\t\tcanvas.setPixel(cx+xp,cy+yp,col);\r\n\t\tcanvas.setPixel(cx+yp,cy+xp,col);\r\n\t\tcanvas.setPixel(cx-xp,cy+yp,col);\r\n\t\tcanvas.setPixel(cx+yp,cy-xp,col);\r\n\t\tcanvas.setPixel(cx-xp,cy-yp,col);\r\n\t\tcanvas.setPixel(cx-yp,cy-xp,col);\r\n\t\tcanvas.setPixel(cx+xp,cy-yp,col);\r\n\t\tcanvas.setPixel(cx-yp,cy+xp,col);\r\n\t\t\r\n\t\tif (d&lt;0){\r\n\t\t\td += (4*xp)+6;\r\n\t\t}else{\r\n\t\t\td += (4*(xp-yp))+10;\r\n\t\t\typ -= 1;\r\n\t\t}\r\n\t\txp++;\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<p>I did a few speed tests against the first one that doesn&#8217;t make use of multiplication and it is only ever so slightly faster&#8230;. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>var canvas:BitmapData = new BitmapData(400, 400, false, 0xCCCCCC); addChild(new Bitmap(canvas)); drawCircle(200,100, 50); \/\/ y, y radius, color function drawCircle(xp:Number,yp:Number, radius:Number, col:uint =0x000000):void { var balance:int; var xoff:int; var yoff:int; xoff=0; yoff=radius; balance=- radius; while (xoff &lt;= yoff) { canvas.setPixel(xp+xoff, yp+yoff, col); canvas.setPixel(xp-xoff, yp+yoff, col); canvas.setPixel(xp-xoff, yp-yoff, col); canvas.setPixel(xp+xoff, yp-yoff, col); canvas.setPixel(xp+yoff, yp+xoff, col); canvas.setPixel(xp-yoff, yp+xoff, [&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,25,26],"tags":[3,115,4,116],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-graphics-algorithms","category-pixel-manipulation","category-setpixel","tag-actionscript","tag-bresenham","tag-flash","tag-multiplication"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/149","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=149"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}