forked from: Catmull-Rom Error
CLICK and drag red dots to change the shape
/**
* Copyright Altschuler ( http://wonderfl.net/user/Altschuler )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2vX3
*/
// forked from shapevent's Catmull-Rom Error
package {
// CLICK and drag red dots to change the shape
import flash.display.*;
import flash.events.*;
import flash.geom.*;
[SWF(width = 500, height= 500)]
import flash.utils.Dictionary;
public class CatmullError extends MovieClip {
private var pnts:Array;
private var n : Number = 0;
private var d : Dictionary = new Dictionary(true);
public function CatmullError(){
// init
pnts = new Array();
// make 8 control points
for (var i:int = 0; i<8; i++){
pnts.push(dot(50 + Math.random() * 80 * i, Math.random()*(stage.stageHeight-40)+20));
d[pnts[pnts.length-1]] = (stage.stageWidth / 2) - pnts[pnts.length-1].x + Math.random() * 100;
}
addEventListener(Event.ENTER_FRAME, onLoop);
// all math from http://en.wikipedia.org/wiki/Cubic_Hermite_spline
// draggable dot
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
}
// private methods
private function onLoop(evt:Event):void {
graphics.clear();
graphics.lineStyle(0,0);
curve(pnts);
}
private function tangent(pk1:Sprite, pk_1:Sprite):Point{
return new Point((pk1.x - pk_1.x) / 2, (pk1.y - pk_1.y) / 2);
}
private function curve(p:Array, res:Number=.05):void{
var px:Number = 0;
var py:Number = 0;
var cx:Number = stage.stageWidth / 2;
var cy:Number = stage.stageHeight / 2;
var pIter:int = p.length - 1;
var m:Array = [];
m[0] = tangent(p[1], p[0]);
for (var i:int = 1; i<pIter; i++){
n+=.005;
p[i].x = cx + (Math.cos(n) * d[p[i]]);
p[i].y += (Math.sin(n));
//sp[i].y += Math.sin(i) * 2;
m[i] = tangent(p[i + 1], p[i - 1]);
}
m[pIter] = tangent(p[pIter], p[pIter - 1]);
for (var t:Number = 0; t <1; t+=res){
var t_2:Number = t * t;
var _1_t:Number = 1 - t;
var _2t:Number = 2 * t;
var h00:Number = (1 + _2t) * (_1_t) * (_1_t);
var h10:Number = t * (_1_t) * (_1_t);
var h01:Number = t_2 * (3 - _2t);
var h11:Number = t_2 * (t - 1);
for (var k:int = 0; k <pIter; k++){
var k1:int = k + 1;
var pk:Sprite = p[k];
var pk1:Sprite = p[k1];
var mk:Point = m[k];
var mk1:Point = m[k1];
px = h00 * pk.x + h10 * mk.x + h01 * pk1.x + h11 * mk1.x;
py = h00 * pk.y + h10 * mk.y + h01 * pk1.y + h11 * mk1.y;
if (k == 0){
graphics.moveTo(px, py);
}else{
graphics.lineTo(px, py);
}
// canvas.setPixel(px, py, 0xFFFFFF);
}
}
}
private function dot(xp:Number, yp:Number, col:uint = 0xFF0000, rad:Number=4):Sprite {
var s:Sprite = Sprite(addChild(new Sprite));
s.x = xp;
s.y = yp;
with(s.graphics) beginFill(col), drawCircle(0,0,rad);
s.buttonMode = true;
s.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
return s;
}
private function onDrag(evt:MouseEvent):void {
evt.currentTarget.startDrag()
}
private function onDown(evt:MouseEvent):void{
dotsVisible(false);
}
private function onUp(evt:MouseEvent):void{
stopDrag();
dotsVisible(true);
}
private function dotsVisible(bool:Boolean):void{
for (var i:int = 0; i<pnts.length; i++){
pnts[i].visible = bool;
}
}
}
}