In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Bezier Curved Jigsaw edge

// forked from PESakaTFM's Bezier - Closed Loop
package
{
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    public class Bezier extends Sprite
    {
        public const NUM_POINTS:int = 8;
        public var pts:Vector.<Point> = new Vector.<Point>();
        protected var mc:Sprite;
        
        public function Bezier()
        {
            var xp:int = 30;
            
            pts[0] = new Point(xp, 250);
            addPoint(pts[0]);
            
            xp+=50;
            pts[1] = new Point(xp+10*Math.random() >> 0, 245+10*Math.random() >> 0);
            addPoint(pts[1]);
            
            var sign:Number = 2*Math.random()-1;
            sign = (sign > 0)? 1 : -1;
            
            xp-=10;
            pts[2] = new Point(xp+10*Math.random() >> 0, 250+(sign*40)+10*Math.random() >> 0);
            addPoint(pts[2]);
            
            xp+=40;
            pts[3] = new Point(xp+10*Math.random() >> 0, 250+(sign*40)+10*Math.random() >> 0);
            addPoint(pts[3]);
            
            xp-=20;
            pts[4] = new Point(xp+10*Math.random() >> 0, 245+10*Math.random() >> 0);
            addPoint(pts[4]);
            
            xp+=70;
            pts[5] = new Point(xp, 250);
            addPoint(pts[5]);
            
            draw();
        }
        
        protected function addPoint(p:Point):void
        {
            var c:Cross;
            
            c = new Cross(p);
            addChild(c);
                
            c.buttonMode = true;
            c.addEventListener(MouseEvent.MOUSE_DOWN, drag);
        }

        
        public function drawSide(pnts:Vector.<Point>, accuracy:int=20):void
        {
            var p:Vector.<Point> = new Vector.<Point>(3,true);
            
            p[0] = pnts[0];
            p[1] = pnts[1];
            p[2] = Point.interpolate(pnts[1], pnts[2], .5);
            drawCurve(p, accuracy);
                
            for(var i:int=1; i<pnts.length-3; i++)
            {
                p[0] = Point.interpolate(pnts[i], pnts[i+1], .5);
                p[1] = pnts[i+1];
                p[2] = Point.interpolate(pnts[i+1], pnts[i+2], .5);
                drawCurve(p, accuracy);
            }
            
            p[0] = Point.interpolate(pnts[i], pnts[i+1], .5);
            p[1] = pnts[i+1];
            p[2] = pnts[i+2];
            drawCurve(p, accuracy);
            
        }
        
        public function drawCurve(pnts:Vector.<Point>, accuracy:int=20):void
        {
            var g:Graphics = this.graphics;
            var a:int;
            var p:Point;
            
            g.moveTo(pnts[0].x, pnts[0].y);
            for(a=0; a <= accuracy; a++)
            {
                p = getPointAt(pnts, a/accuracy);
                g.lineTo(p.x, p.y);
            }
        }
        
        protected function getPointAt(pnts:Vector.<Point>, t:Number):Point
        {
            var n:int = pnts.length-1;
            var m:Number = Math.pow((1-t), n);
            var p:Point = new Point(pnts[0].x * m, pnts[0].y * m);
            for(var i:int=1; i<=n; i++)
            {
                m = Math.pow((1-t), n-i)*bc(n, i)*Math.pow(t, i);
                p = p.add(new Point(pnts[i].x * m, pnts[i].y * m));
            }
            return p;
        }
        
        protected function bc(n:int, k:int):int // Binomial Coefficient
        {
            return (f(n)/(f(k)*f(n-k)));
        }
        
        protected function f(value:int):int // Factorial
        {
            var i:int = value;
            var result:int = 1;
            
            for(; i>0; i--)
            {
                result *= i;
            }
            
            return result;
        }
        
        protected function draw():void
        {
            graphics.clear();
            graphics.lineStyle(1,0xFF0000);
            drawSide(pts);
            graphics.endFill();
        }
        
        protected function drag(event:MouseEvent):void
        {
            mc = event.currentTarget as Sprite;
            stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
            move();
        }
        
        protected function endDrag(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, move);
            move();
        }
        
        protected function move(event:MouseEvent=null):void
        {
            mc.x = mouseX;
            mc.y = mouseY;
            draw();
        }
    }
}
    import flash.display.Sprite;
    import flash.geom.Point;
    

class Cross extends Sprite
{
    protected var pnt:Point;
    public function Cross(p:Point)
    {
        graphics.beginFill(0xFF0000, 0);
        graphics.drawCircle(0,0,6);
        graphics.endFill();
        graphics.lineStyle(1, 0x0000FF);
        graphics.moveTo(-3,-3);
        graphics.lineTo(3, 3);
        graphics.moveTo(-3, 3);
        graphics.lineTo(3,-3);
        graphics.endFill();
        
        pnt = p;
        super.x = pnt.x;
        super.y = pnt.y;
    }
    
    override public function set x(value:Number):void
    {
        super.x = pnt.x = value;
    }
    
    override public function set y(value:Number):void
    {
        super.y = pnt.y = value;
    }
}