{"id":226,"date":"2009-03-21T09:04:09","date_gmt":"2009-03-21T16:04:09","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1000"},"modified":"2009-03-21T09:04:09","modified_gmt":"2009-03-21T16:04:09","slug":"low-precision-sine-and-cosine","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=226","title":{"rendered":"Low Precision Sine and Cosine"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar t:Number=0, cos:Number, sin:Number;\r\n\r\nvar canvas:BitmapData = new BitmapData(400,400,false, 0x000000);\r\naddChild(new Bitmap(canvas));\r\n\r\nvar shape:Shape = new Shape();\r\nwith(shape.graphics) beginFill(0x568338, .2), drawCircle(0,0,10);\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t\r\n\t     t += .1;\r\n\t\t\/\/ -- low precision sine\/cosine\r\n\t    \/\/always wrap input angle to -PI..PI\r\n\t\tif (t &lt; -3.14159265){\r\n\t\t\tt += 6.28318531;\r\n\t\t}else{\r\n\t\tif (t &gt;  3.14159265)\r\n\t\t\tt -= 6.28318531;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/compute sine\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\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\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\tt -= 1.57079632;\r\n\t\t\r\n\t\t\/\/ move the shape\r\n\t\tshape.x = 200 + 100 * cos;\r\n\t\tshape.y = 200 + 100 * sin;\r\n\t\t\r\n\t\t\/\/ draw to the canvas\r\n\t\tcanvas.draw(shape, shape.transform.matrix);\r\n}\r\n<\/pre>\n<p>This snippet draws a circle using low precision sine and cosine&#8230; you&#8217;ll notice that its not a perfect looking circle:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/fast_sin_cos_circ.jpg\" \/><br \/>\nBack in January I saw <a href=\"http:\/\/lab.polygonal.de\/2007\/07\/18\/fast-and-accurate-sinecosine-approximation\/\" target=\"blank\">this blog post<\/a> by Michael Baczynski over at <a href=\"http:\/\/lab.polygonal.de\/\">http:\/\/lab.polygonal.de\/<\/a>. The blog post describes a technique for fast sine and cosine approximation &#8211; (I highly recommend giving it a read &#8211; very fun stuff).  <\/p>\n<p>It&#8217;s worth noting that there is a higher precision sine and cosine that will likely draw a better looking circle but will be about half as fast. According to <a href=\"http:\/\/lab.polygonal.de\/2007\/07\/18\/fast-and-accurate-sinecosine-approximation\/\" target=\"blank\" target=\"blank\">the original post <\/a>&#8230; the low precision technique is approximately 14x faster than using Math.cos()\/Math.sin().<\/p>\n<p>There some other really great posts over at polygonal <a href=\"http:\/\/lab.polygonal.de\/\" target=\"blank\">go check them out<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var t:Number=0, cos:Number, sin:Number; var canvas:BitmapData = new BitmapData(400,400,false, 0x000000); addChild(new Bitmap(canvas)); var shape:Shape = new Shape(); with(shape.graphics) beginFill(0x568338, .2), drawCircle(0,0,10); addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { t += .1; \/\/ &#8212; low precision sine\/cosine \/\/always wrap input angle to -PI..PI if (t &lt; -3.14159265){ t += 6.28318531; }else{ if (t &gt; 3.14159265) t -= 6.28318531; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121,13],"tags":[124,4],"class_list":["post-226","post","type-post","status-publish","format-standard","hentry","category-math","category-motion","tag-actionscrpt","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/226","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=226"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/226\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}