Catmull-Rom Error

I was messing with Catumull Rom spline code from awhile back and this this came out:

Actionscript:
  1. [SWF(width = 500, height= 500)]
  2. var pnts:Array = new Array();
  3. // make 8 control points
  4. for (var i:int = 0; i<8; i++){
  5.     pnts.push(dot(50 + Math.random() * 80 * i, Math.random()*(stage.stageHeight-40)+20));
  6. }
  7. addEventListener(Event.ENTER_FRAME, onLoop);
  8. function onLoop(evt:Event):void {
  9.    
  10.     graphics.clear();
  11.     graphics.lineStyle(0,0);
  12.     curve(pnts);
  13. }
  14. function tangent(pk1:Sprite, pk_1:Sprite){
  15.     return new Point((pk1.x - pk_1.x) / 2, (pk1.y - pk_1.y) / 2);
  16. }
  17. // all math from http://en.wikipedia.org/wiki/Cubic_Hermite_spline
  18. function curve(p:Array, res:Number=.05):void{
  19.     var px:Number = 0;
  20.     var py:Number = 0;
  21.     var pIter:int = p.length - 1;
  22.     var m:Array = [];
  23.     m[0] = tangent(p[1], p[0]);
  24.     for (var i:int = 1; i<pIter; i++){
  25.         m[i] = tangent(p[i + 1], p[i - 1]);
  26.     }
  27.     m[pIter] = tangent(p[pIter], p[pIter - 1]);
  28.     for (var t:Number = 0; t <1; t+=res){
  29.          var t_2:Number = t * t;
  30.          var _1_t:Number = 1 - t;
  31.          var _2t:Number = 2 * t;
  32.          var h00:Number =  (1 + _2t) * (_1_t) * (_1_t);
  33.          var h10:Number =  t  * (_1_t) * (_1_t);
  34.          var h01:Number =  t_2 * (3 - _2t);
  35.          var h11:Number =  t_2 * (t - 1);
  36.          for (var k:int = 0; k <pIter; k++){
  37.              var k1:int = k + 1;
  38.              var pk:Sprite = p[k];
  39.              var pk1:Sprite = p[k1];
  40.              var mk:Point = m[k];
  41.              var mk1:Point = m[k1];
  42.            
  43.              px = h00 * pk.x + h10 * mk.x + h01 * pk1.x + h11 * mk1.x;
  44.              py = h00 * pk.y + h10 * mk.y + h01 * pk1.y + h11 * mk1.y;
  45.              if (k == 0){
  46.                 graphics.moveTo(px, py);
  47.              }else{
  48.                 graphics.lineTo(px, py);
  49.              }
  50.              
  51.             // canvas.setPixel(px, py, 0xFFFFFF);
  52.          }
  53.     }
  54. }
  55. // draggable dot
  56. function dot(xp:Number, yp:Number, col:uint = 0xFF0000, rad:Number=4):Sprite {
  57.     var s:Sprite = Sprite(addChild(new Sprite));
  58.     s.x = xp;
  59.     s.y = yp;
  60.  
  61.     with(s.graphics) beginFill(col), drawCircle(0,0,rad);
  62.     s.buttonMode = true;
  63.     s.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
  64.     return s;
  65. }
  66. function onDrag(evt:MouseEvent):void {
  67.     evt.currentTarget.startDrag()
  68. }
  69. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  70. stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
  71. function onDown(evt:MouseEvent):void{
  72.       dotsVisible(false);
  73. }
  74. function onUp(evt:MouseEvent):void{
  75.     stopDrag();
  76.     dotsVisible(true);
  77. }
  78.  
  79. function dotsVisible(bool:Boolean):void{
  80.     for (var i:int = 0; i<pnts.length; i++){
  81.       pnts[i].visible = bool;
  82.      }
  83. }


You can see the swf over at wonderfl.

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. altschuler
    Posted March 22, 2010 at 7:24 am | Permalink

    That’s awesome!

  2. Posted March 23, 2010 at 4:22 am | Permalink

    is it possible to get your mail ; i’d love submit some code but would have a critic eye on it before ?

  3. Posted March 23, 2010 at 7:30 am | Permalink

    thanks altshuler…. y_nk, my email is on the about page… but here it is bendvent [] gmail.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*