{"id":151,"date":"2009-01-11T00:00:22","date_gmt":"2009-01-11T07:00:22","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=503"},"modified":"2009-01-11T00:00:22","modified_gmt":"2009-01-11T07:00:22","slug":"quadratic-bezier","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=151","title":{"rendered":"Quadratic Bezier"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar canvas:BitmapData=new BitmapData(280,280,false,0x000000);\r\naddChild(new Bitmap(canvas, PixelSnapping.AUTO, true));\r\nvar color:uint;\r\n\/\/ anchor x1, anchor y1,\r\n\/\/ control-handle x2, control-handle y2,\r\n\/\/ anchor x3, anchor y3, &#x5B;resolution incremental value between 0-1]\r\nfunction quadBezier(x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, resolution:Number=.03):void {\r\n\tvar b:Number,pre1:Number,pre2:Number,pre3:Number,pre4:Number;\r\n\tfor (var a:Number = 0; a &lt;1;a+=resolution) {\r\n\t\r\n\t\tb=1-a;\r\n\t\tpre1=(a*a);\r\n\t\tpre2=2*a*b;\r\n\t\tpre3=(b*b);\r\n\t\t\r\n\t\tcanvas.setPixel(pre1*x1 + pre2*x2  + pre3*x3 ,\r\n\t\t\t\t\t\t             pre1*y1 + pre2*y2 + pre3*y3, color);\r\n\t}\r\n}\r\n\r\n\/\/ draw a few\r\ncolor = 0xFFFFFF;\r\n\r\nfor (var i:int = 0; i&lt;20; i++){\r\nquadBezier(40,100, 150 , 20 + i * 10 , 200, 100,.01);\r\n}\r\n\r\ncolor = 0xFF0000;\r\n\r\nfor (i= 0; i&lt;20; i++){\r\nquadBezier(150,200, 100 + i * 10, 100  , 120, 30,.01);\r\n}\r\n<\/pre>\n<p>The above demos a function that draws quadratic bezier curves using setPixel().<\/p>\n<p>One of the <a href=\"https:\/\/actionsnippet.com\/?cat=16\" target=\"blank\">first posts<\/a> on this site was a snippet that used setPixel() to draw a cubic bezier curve. I recently needed to do the exact same thing but I wanted to use a quadratic bezier&#8230; I knew I had the code laying around somewhere, but I couldn&#8217;t seem to find it so <a href=\"http:\/\/en.wikipedia.org\/wiki\/B%C3%A9zier_curve\" target=\"blank\">I just looked on wikipedia<\/a> and changed the previous cubicBezier() function accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var canvas:BitmapData=new BitmapData(280,280,false,0x000000); addChild(new Bitmap(canvas, PixelSnapping.AUTO, true)); var color:uint; \/\/ anchor x1, anchor y1, \/\/ control-handle x2, control-handle y2, \/\/ anchor x3, anchor y3, &#x5B;resolution incremental value between 0-1] function quadBezier(x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, resolution:Number=.03):void { var b:Number,pre1:Number,pre2:Number,pre3:Number,pre4:Number; for (var a:Number = 0; a &lt;1;a+=resolution) { b=1-a; pre1=(a*a); pre2=2*a*b; pre3=(b*b); canvas.setPixel(pre1*x1 + pre2*x2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,24,114,25,26],"tags":[3,117,4],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-bezier","category-bitmapdata","category-graphics-algorithms","category-pixel-manipulation","category-setpixel","tag-actionscript","tag-bezier","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/151","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=151"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}