/**
* Copyright dx0ne ( http://wonderfl.net/user/dx0ne )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tDMY
*/
// forked from makc3d's Cardinal Splines Part 4
package {
import flash.events.MouseEvent;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Point;
/**
* Exploring formula in Jim Armstrong "Cardinal Splines Part 4"
* @see http://algorithmist.wordpress.com/2009/10/06/cardinal-splines-part-4/
*/
[SWF(frameRate="60")]
public class CS4 extends Sprite {
private var line:Sprite;
public function CS4 () {
time = 0;
addEventListener ("enterFrame", loop);
line = new Sprite();
addChild(line);
line.graphics.lineStyle(1,0xff0000);
line.graphics.lineTo(100,100);
// generate 7 random points
var i:int;
var ptsShape:Sprite;
pts = [];
for (i = 0; i < 7; i++) {
var pt:Point = new Point (
40 + (465 - 2 * 40) * Math.random (),
40 + (465 - 2 * 40) * Math.random ()
);
ptsShape = new Sprite();
addChild (ptsShape);
ptsShape.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
ptsShape.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
ptsShape.buttonMode = true;
ptsShape.graphics.beginFill (0x00ff00, 1);
ptsShape.graphics.drawCircle (-2,-2, 5);
ptsShape.graphics.endFill();
ptsShape.x=pt.x;
ptsShape.alpha=0.3;
ptsShape.y=pt.y;
pts.push (ptsShape);
}
}
private function onMouseDownHandler(e:MouseEvent):void
{
e.currentTarget.startDrag();
}
private function onMouseUpHandler(e:MouseEvent):void
{
e.currentTarget.stopDrag();
}
private var time:Number;
private var pts:Array;
private function loop (e:*):void {
// pulse s
time += 0.1; if (time > 6.2831853) time -= 6.2831853;
var s:Number = 1;//2*(stage.mouseX/stage.stageWidth);// * Math.sin (time);
// draw spline
var i:int, j:int;
line.graphics.clear ();
line.graphics.lineStyle(1, 0xFF0000);
for (i = 0; i < pts.length - 1; i++) {
var P1:Sprite = (i < 1) ? pts [pts.length - 1] : pts [i - 1];
var P2:Sprite = pts [i];
var P3:Sprite = pts [i + 1];
var P4:Sprite = (i < pts.length - 2) ? pts [i + 2] : pts [0];
for (j = 0; j < 100+ 1; j++) {
var t:Number = j * 0.01;
var Pt:Point = new Point (
// x
s * ( -t * t * t + 2 * t * t - t) * P1.x +
s * ( -t * t * t + t * t) * P2.x +
(2 * t * t * t - 3 * t * t + 1) * P2.x +
s * (t * t * t - 2 * t * t + t) * P3.x +
( -2 * t * t * t + 3 * t * t) * P3.x +
s * (t * t * t - t * t) * P4.x,
// y
s * ( -t * t * t + 2 * t * t - t) * P1.y +
s * ( -t * t * t + t * t) * P2.y +
(2 * t * t * t - 3 * t * t + 1) * P2.y +
s * (t * t * t - 2 * t * t + t) * P3.y +
( -2 * t * t * t + 3 * t * t) * P3.y +
s * (t * t * t - t * t) * P4.y
);
if (i + j == 0)
line.graphics.moveTo (Pt.x, Pt.y);
else
line.graphics.lineTo (Pt.x, Pt.y);
}
}
}
}
}