{"id":230,"date":"2009-03-24T00:00:06","date_gmt":"2009-03-24T07:00:06","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1026"},"modified":"2009-03-24T00:00:06","modified_gmt":"2009-03-24T07:00:06","slug":"cubic-hermite-spline","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=230","title":{"rendered":"Cubic Hermite Spline"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar canvas:BitmapData = new BitmapData(400,400,false, 0x000000);\r\naddChild(new Bitmap(canvas));\r\n\r\n\/\/ create some draggable dots\r\nvar p0:Sprite = dot(100, 100);\r\nvar p1:Sprite = dot(200, 200);\r\nvar m0:Sprite = dot(200, 100, 0xFFFFFF, 3)\r\nvar m1:Sprite = dot(100, 200, 0xFFFFFF, 3);\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tcanvas.fillRect(canvas.rect, 0x000000);\r\n\tvar px:Number = 0;\r\n\tvar py:Number = 0;\r\n\tfor (var t:Number = 0; t &lt;1; t+=.01){\r\n\t\tvar t_2:Number = t * t;\r\n\t\tvar t_3:Number = t_2 * t;\r\n\t\t\r\n\t\t\/\/ some repetitive math for clarity\r\n\t\tpx = (2 * t_3  - 3 * t_2 + 1) * p0.x +(t_3 - 2 * t_2 + t) *\r\n\t\t\t\t m0.x + (-2 * t_3 + 3 * t_2) * p1.x + (t_3 - t_2) * m1.x;\r\n\t\tpy = (2 * t_3  - 3 * t_2 + 1) * p0.y +(t_3 - 2 * t_2 + t) *\r\n\t\t\t\t m0.y + (-2 * t_3 + 3 * t_2) * p1.y + (t_3 - t_2) * m1.y;\r\n\t\tcanvas.setPixel(px, py, 0xFF0000);\r\n\t}\r\n}\r\n\/\/ draggable dot\r\nfunction dot(xp:Number, yp:Number, col:uint = 0x507399, rad:Number=5):Sprite {\r\n\tvar s:Sprite = Sprite(addChild(new Sprite()));\r\n\ts.x = xp;\r\n\ts.y = yp;\r\n\twith(s.graphics) beginFill(col), drawCircle(0,0,rad);\r\n\ts.buttonMode = true;\r\n\ts.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);\r\n\treturn s;\r\n}\r\nfunction onDrag(evt:MouseEvent):void {\r\n\tevt.currentTarget.startDrag()\r\n}\r\nstage.addEventListener(MouseEvent.MOUSE_UP, onUp);\r\nfunction onUp(evt:MouseEvent):void{\r\n\tstopDrag();\r\n}\r\n<\/pre>\n<p>This is the start of my exploration of Cubic Hermite Splines (like Catmull\u2013Rom)&#8230; I wrote this snippet while skimming <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cubic_Hermite_spline\" target=\"blank\">the wikipedia article<\/a> on the subject&#8230;. at first I wasn&#8217;t sure if this was correct, but I added tangent calculations for a Catmull-Rom and it worked nicely&#8230; will post that tomorrow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var canvas:BitmapData = new BitmapData(400,400,false, 0x000000); addChild(new Bitmap(canvas)); \/\/ create some draggable dots var p0:Sprite = dot(100, 100); var p1:Sprite = dot(200, 200); var m0:Sprite = dot(200, 100, 0xFFFFFF, 3) var m1:Sprite = dot(100, 200, 0xFFFFFF, 3); addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { canvas.fillRect(canvas.rect, 0x000000); var px:Number = 0; var py:Number = 0; for (var t:Number [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114,121,26],"tags":[3,4],"class_list":["post-230","post","type-post","status-publish","format-standard","hentry","category-graphics-algorithms","category-math","category-setpixel","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/230","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=230"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/230\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}