{"id":296,"date":"2009-05-25T18:10:44","date_gmt":"2009-05-26T01:10:44","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1439"},"modified":"2009-05-25T18:10:44","modified_gmt":"2009-05-26T01:10:44","slug":"sine-cosine-gradient","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=296","title":{"rendered":"sine cosine Gradient"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width=600,height=500,frameRate=30)]\r\nvar canvas:BitmapData=new BitmapData(600,500,false,0x000000);\r\naddChild(new Bitmap(canvas));\r\nvar size:Number=canvas.width*canvas.height;\r\nvar w:Number=canvas.width;\r\nvar wd:Number=1\/w;\r\nvar pix:Vector.&lt;uint&gt; = new Vector.&lt;uint&gt;();\r\nvar sin:Number;\r\nvar cos:Number;\r\nvar dx:Number=110;\r\nvar dy:Number=52;\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tdx+=0.001;\r\n\tcanvas.lock();\r\n\tfor (var i:int = 0; i&lt;size; i++) {\r\n\t\tvar xp:Number=i%w;\r\n\t\tvar yp:Number=int(i*wd);\r\n\t\tvar xx:Number=xp*0.05+dx;\r\n\t\tvar yy:Number=yp*0.05+dy;\r\n\t\tvar t:Number= (xx * yy) % 3.14159265;\r\n\t\t\/\/compute sine\r\n\t\t\/\/ technique from http:\/\/lab.polygonal.de\/2007\/07\/18\/fast-and-accurate-sinecosine-approximation\/\r\n\t\t\/\/ by Michael Baczynski\r\n\t\tif (t&lt;0) {\r\n\t\t\tsin=1.27323954*t+.405284735*t*t;\r\n\t\t} else {\r\n\t\t\tsin=1.27323954*t-0.405284735*t*t;\r\n\t\t}\r\n\t\t\/\/compute cosine: sin(t + PI\/2) = cos(t)\r\n\t\tt+=1.57079632;\r\n\t\tif (t&gt;3.14159265) {\r\n\t\t\tt-=6.28318531;\r\n\t\t}\r\n\t\tif (t&lt;0) {\r\n\t\t\tcos=1.27323954*t+0.405284735*t*t;\r\n\t\t} else {\r\n\t\t\tcos=1.27323954*t-0.405284735*t*t;\r\n\t\t}\r\n\t\tvar c:Number=sin+cos*cos*cos;\r\n\t\t\/\/ fast math abs\r\n\t\tc=c&lt;0? -c:c;\r\n\t\tc=c*140;\r\n\t\t\/\/ math max 255\r\n\t\tc=c&gt;255?255:c;\r\n\t\tpix&#x5B;i]=c&lt;&lt;16|c&lt;&lt;8|c;\r\n\t}\r\n\tcanvas.setVector(canvas.rect, pix);\r\n\tcanvas.unlock();\r\n}\r\n<\/pre>\n<p>The above snippet will animate a gradient that looks like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/sin_cos_texture.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width=600,height=500,frameRate=30)] var canvas:BitmapData=new BitmapData(600,500,false,0x000000); addChild(new Bitmap(canvas)); var size:Number=canvas.width*canvas.height; var w:Number=canvas.width; var wd:Number=1\/w; var pix:Vector.&lt;uint&gt; = new Vector.&lt;uint&gt;(); var sin:Number; var cos:Number; var dx:Number=110; var dy:Number=52; addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { dx+=0.001; canvas.lock(); for (var i:int = 0; i&lt;size; i++) { var xp:Number=i%w; var yp:Number=int(i*wd); var xx:Number=xp*0.05+dx; var yy:Number=yp*0.05+dy; var t:Number= (xx * yy) % 3.14159265; [&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,79],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-graphics-algorithms","category-pixel-manipulation","category-vector"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/296","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=296"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/296\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}