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

flash on 2013-9-20

Get Adobe Flash player
by novic.one 20 Sep 2013
    Embed
/**
 * Copyright novic.one ( http://wonderfl.net/user/novic.one )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2qpr
 */

package {
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Matrix;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        private const WIDTH:Number = 100;
        private const HEIGHT:Number = 100;
        
        private var points:Array = [];
        private var matrix:Matrix;
        
        private var shape:Sprite;
        private var handlesContainer:Sprite;
        
        private var P1:Sprite;
        private var P2:Sprite;
        
        public function FlashTest() {
            x = y = 100;
            addChild(shape = new Sprite());
            
            shape.graphics.lineStyle(2, 0, 1, false, "none");
            shape.graphics.beginFill(0xcdefab);
            shape.graphics.drawRect(0, 0, WIDTH, HEIGHT);
            
            addChild(handlesContainer = new Sprite());
            
            createHandle(new Point(0, HEIGHT));
            createHandle(new Point(WIDTH / 2, 0));
            createHandle(new Point(WIDTH, HEIGHT));
            
            addChild(P1 = new Sprite());
            addChild(P2 = new Sprite());
            
            P1.graphics.beginFill(0xff0000);
            P1.graphics.drawRect(-5, -5, 10, 10);
            
            P2.graphics.beginFill(0xff0000);
            P2.graphics.drawRect(-5, -5, 10, 10);
            
            update();
        }
        
        private function update():void {
            var a:Number = (points[2].y - points[0].y) / (points[2].x - points[0].x);
            
            var x0:Number = points[1].x;
            var y0:Number = points[1].y;
            var b:Number = y0 - a * x0;
            
            var r:Number = Point.distance(points[0], points[2]) / 2;
            
            var A:Number = a * a + 1;
            var B:Number = 2 * (a * b - y0 * a - x0);
            var C:Number = x0 * x0 + y0 * y0 + b * b - r * r - 2 * y0 * b;
            
            var D:Number = B * B - 4 * A * C;
            var sD:Number = Math.sqrt(D);
            
            var x1:Number = (-B - sD) / 2 / A;
            var x2:Number = (-B + sD) / 2 / A;
            
            var y1:Number = a * x1 + b;
            var y2:Number = a * x2 + b;
            
            var matrix:Matrix = new Matrix();
            matrix.tx = x1;
            matrix.ty = y1;
            matrix.a = (x2 - x1) / WIDTH;
            matrix.b = (y2 - y1) / HEIGHT;
            
            matrix.c = (points[0].x - x1) / WIDTH;
            matrix.d = (points[0].y - y1) / HEIGHT;
            
            P1.x = x1;
            P1.y = y1;
            
            P2.x = x2;
            P2.y = y2;
            
            shape.transform.matrix = matrix;
        }

        
        private function createHandle(pt:Point):void {
            var handle:Sprite = new Sprite();
            handle.graphics.lineStyle(1);
            handle.graphics.beginFill(0xabcdef);
            handle.graphics.drawRect(-5, -5, 10, 10);
            
            handlesContainer.addChild(handle);
            handle.x = pt.x;
            handle.y = pt.y;
            
            points.push(pt);
            
            var moveHandler:Function = function(e:Event):void {
                pt.x = handle.x;
                pt.y = handle.y;
                update();
            };
            var upHandler:Function = function(e:Event):void {
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
                stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
                handle.stopDrag();
            };
            
            handle.addEventListener(MouseEvent.MOUSE_DOWN, function(e:Event):void {
                handle.startDrag();
                stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
                stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
            });

        }
        
        
    }
}