notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FORKED
  1. // forked from makc3d's Lagrange polynomial
  2. // http://en.wikipedia.org/wiki/Lagrange_polynomial
  3. package {
  4.     import flash.display.Sprite;
  5.     [SWF(frameRate="7")] 
  6.     public class FlashTest extends Sprite {
  7.         public function FlashTest() {
  8.             Wonderfl.capture_delay (10);
  9.             stage.addEventListener ("mouseDown", onMouseDown);
  10.             stage.addEventListener ("mouseUp", onMouseUp);
  11.             stage.addEventListener ("enterFrame", onEnterFrame);
  12.         }
  13.         public var mouseDown:Boolean = false;
  14.         public function onMouseDown (e:*):void {
  15.             posx.length = 0;
  16.             posy.length = 0;
  17.             mouseDown = true;
  18.         }
  19.         public function onMouseUp (e:*):void {
  20.             mouseDown = false;
  21.         }
  22.         public var posx:Array = [];
  23.         public var posy:Array = [];
  24.         public function onEnterFrame (e:*):void {
  25.             if (mouseDown) {
  26.                 posx.push (mouseX);
  27.                 posy.push (mouseY);
  28.             }
  29.             var i:int = 0, ts:Array = [];
  30.             graphics.clear ();
  31.             graphics.lineStyle ();
  32.             graphics.beginFill (0xFF0000);
  33.             for (i = 0; i < posx.length; i++) {
  34.                 ts.push (i);
  35.                 graphics.drawCircle (posx [i], posy [i], 3);
  36.             }
  37.             graphics.endFill ();
  38.             if (!mouseDown && (posx.length > 1)) {
  39.                 graphics.lineStyle (00);
  40.                 for (var t:Number = 0; t < 100; t += 0.1) {
  41.                     var tx:Number = lagrangeInterpolatingPolynomial (
  42.                         ts, posx, t);
  43.                     var ty:Number = lagrangeInterpolatingPolynomial (
  44.                         ts, posy, t);
  45.                     if (t == 0)
  46.                         graphics.moveTo (tx, ty);
  47.                     else
  48.                         graphics.lineTo (tx, ty);
  49.                 }
  50.             }
  51.         }
  52.         public function lagrangeInterpolatingPolynomial (
  53.             pos:Array, val:Array, desiredPos:Number):Number {
  54.             var degree:int = pos.length;
  55.             var retVal:Number = 0
  56.             for (var i:int = 0; i < degree; i++) {
  57.                 var weight:Number = 1
  58.                 for (var j:int = 0; j < degree; j++) {
  59.                     // The i-th term has to be skipped
  60.                     if (j != i) {
  61.                         weight *= (desiredPos - pos[j]) / (pos[i] - pos[j]);
  62.                     }
  63.                 }
  64.                 retVal += weight * val[i];
  65.             }
  66.  
  67.             return retVal; 
  68.         }
  69.     }
  70. }
noswf
Get Adobe Flash Player