{"id":331,"date":"2009-06-30T09:30:16","date_gmt":"2009-06-30T16:30:16","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1658"},"modified":"2009-06-30T09:30:16","modified_gmt":"2009-06-30T16:30:16","slug":"bresenhams-circle-and-setvector","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=331","title":{"rendered":"Bresenham&#8217;s Circle and setVector()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar canvasSize:int = 400;\r\nvar canvas:BitmapData = new BitmapData(canvasSize, canvasSize, false, 0xFFFFFF);\r\naddChild(new Bitmap(canvas));\r\nvar size:int = canvas.width * canvas.height;\r\nvar pixels:Vector.&lt;uint&gt; = canvas.getVector(canvas.rect);\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t for (var i:int = 0; i&lt;500; i++){\r\n\t  fillCircle(int(Math.random() * canvasSize),\r\n\t\t\t\t\t  int(Math.random() * canvasSize),\r\n\t\t\t\t\t  int(Math.random() * 5 + 3),\r\n\t\t\t\t\t  uint(Math.random() * 0xFFFF));\r\n\t }\r\n\t canvas.lock();\r\n\t canvas.setVector(canvas.rect, pixels);\r\n\t canvas.unlock();\r\n}\r\n\r\nfunction fillCircle(xp:int,yp:int, radius:int, col:uint = 0x000000):void {\r\n\tvar xoff:int =0;\r\n\tvar yoff:int = radius;\r\n\tvar balance:int = -radius;\r\n\twhile (xoff &lt;= yoff) {\r\n\t\t var p0:int = xp - xoff;\r\n\t\t var p1:int = xp - yoff;\r\n\t\t var w0:int = xoff + xoff;\r\n\t\t var w1:int = yoff + yoff;\r\n\t\t hLine(p0, yp + yoff, w0, col);\r\n\t\t hLine(p0, yp - yoff, w0, col);\r\n\t\t hLine(p1, yp + xoff, w1, col);\r\n\t\t hLine(p1, yp - xoff, w1, col);\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\nfunction hLine(xp:int, yp:int, w:int, col:uint):void {\r\n\tvar index:int = xp + yp * canvasSize;\r\n\tfor (var i:int = 0; i &lt;w; i++){\r\n\t\tindex++;\r\n\t\tif (index &gt; -1 &amp;&amp; index &lt; size){\r\n\t\t  pixels&#x5B;index] = col;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>In the past I&#8217;ve posted examples of Bresenham&#8217;s Circle (<a href=\"https:\/\/actionsnippet.com\/?p=492\">here<\/a> and <a href=\"https:\/\/actionsnippet.com\/?p=496\">here<\/a>). Both of those examples make use of setPixel(). Today&#8217;s snippet demos a version of Bresenham&#8217;s Circle that works with setVector().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var canvasSize:int = 400; var canvas:BitmapData = new BitmapData(canvasSize, canvasSize, false, 0xFFFFFF); addChild(new Bitmap(canvas)); var size:int = canvas.width * canvas.height; var pixels:Vector.&lt;uint&gt; = canvas.getVector(canvas.rect); addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { for (var i:int = 0; i&lt;500; i++){ fillCircle(int(Math.random() * canvasSize), int(Math.random() * canvasSize), int(Math.random() * 5 + 3), uint(Math.random() * 0xFFFF)); } canvas.lock(); canvas.setVector(canvas.rect, pixels); canvas.unlock(); [&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,14,4,126],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-graphics-algorithms","category-pixel-manipulation","category-setpixel","tag-actionscript","tag-as3","tag-flash","tag-fp10"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/331","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=331"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}