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

forked from: GeomTest III

GeomTest III
@author Test Dept
Get Adobe Flash player
by Test_Dept 06 Mar 2011
/**
 * Copyright Test_Dept ( http://wonderfl.net/user/Test_Dept )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/flwH
 */

// forked from Test_Dept's GeomTest III
package {

    import flash.display.Sprite;
        
    /**
     * GeomTest III
     * @author Test Dept
     */
    [SWF(backgroundColor="#ffffff", width="465", height="465")]
    public class GeomTestIII extends Sprite {
        public function GeomTestIII() {
            addChild(new GeomTestImpl() );    
        }        
    }
}

import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;

class GeomTestImpl extends Sprite {
        
    private var _a1 : UIPoint;
    private var _a2 : UIPoint;
    private var _b1 : UIPoint;
    private var _b2 : UIPoint;
    private var _c : UIPoint;

    public function GeomTestImpl() {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
    }
    
    private function addedToStageHandler(event : Event) : void {

        addChild(_a1 = new UIPoint(0x000000, 10, 200, "a1", true) );
        addChild(_a2 = new UIPoint(0x000000, 120, 250, "a2", true) );
        addChild(_b1 = new UIPoint(0x000000, 200, 10, "b1", true) );
        addChild(_b2 = new UIPoint(0x000000, 260, 150, "b2", true) );
        addChild(_c = new UIPoint(0x000000, 0, 0, "c") );

        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }
    
    private function enterFrameHandler(event : Event) : void {

        var a1 : Point = new Point(_a1.x, _a1.y);
        var a2 : Point = new Point(_a2.x, _a2.y);
        var b1 : Point = new Point(_b1.x, _b1.y);
        var b2 : Point = new Point(_b2.x, _b2.y);
                
        var ratio : Number = 0.5;
        function getRatioPoint(p1 : Point, p2 : Point) : Point {
            return new Point(
                p1.x + (p2.x - p1.x) * ratio,
                p1.y + (p2.y - p1.y) * ratio);
        }

        var p01 : Point = getRatioPoint(a1, a2);
        var p12 : Point = getRatioPoint(a2, b2);
        var p23 : Point = getRatioPoint(b2, b1);

        var r1 : Point = getRatioPoint(p01, p12);
        var r2 : Point = getRatioPoint(p12, p23);
        
        var r12 : Point = getRatioPoint(r1, r2);
        
        _c.x = r12.x;
        _c.y = r12.y;
        
        var c01 : Point = GeoUtil.getCrossPoint(r1, r2, a1, a2);
        var c23 : Point = GeoUtil.getCrossPoint(r1, r2, b2, b1);

        var g : Graphics = graphics;
        g.clear();
        
        g.lineStyle(1, 0x000000);
        g.moveTo(a1.x, a1.y);
        g.lineTo(a2.x, a2.y);
        g.moveTo(b1.x, b1.y);
        g.lineTo(b2.x, b2.y);

        g.lineStyle(1, 0xcccccc);
        g.moveTo(a2.x, a2.y);
        g.lineTo(b2.x, b2.y);

        g.lineStyle(1, 0xff0000);
        g.moveTo(p01.x, p01.y);
        g.lineTo(p12.x, p12.y);
        g.lineTo(p23.x, p23.y);

        g.lineStyle(1, 0x00ff00);
        g.moveTo(r1.x, r1.y);
        g.lineTo(r2.x, r2.y);
        
        g.lineStyle(1, 0x0000ff);
        g.moveTo(a1.x, a1.y);
        g.curveTo(c01.x, c01.y, r12.x, r12.y);
        g.curveTo(c23.x, c23.y, b1.x, b1.y);
    }
}

class GeoUtil {

    public static function getCrossPoint(
        a1 : Point, a2 : Point,
        b1 : Point, b2 : Point
    ) : Point {
        
        var a12 : Point = a2.subtract(a1);
        var b12 : Point = b2.subtract(b1);

        var mat : Matrix = new Matrix(
            a12.x, a12.y, 
            b12.x, b12.y);
        
        mat.invert();
        
        var st : Point = mat.transformPoint(b1.subtract(a1) );
        
        var s : Number = st.x;

        return new Point(
            a1.x + a12.x * s,
            a1.y + a12.y * s);
    }
}

class UIPoint extends Sprite {

    private var _pressed : Boolean;
    private var _dragX : Number;
    private var _dragY : Number;
    
    public function UIPoint(
        color : uint, x : Number, y : Number, label : String,
        movable : Boolean = false
    ) {

        var g : Graphics = graphics;
        g.clear();
        g.beginFill(color, 0.5);
        g.drawCircle(0, 0, 5);
        g.endFill();
        
        var labelField : TextField = new TextField();
        labelField.type = TextFieldType.DYNAMIC;
        labelField.autoSize = TextFieldAutoSize.LEFT;
        labelField.selectable = false;
        labelField.text = label;
        labelField.x = 4;
        labelField.y = 4;
        addChild(labelField);

        this.x = x;
        this.y = y;
        
        buttonMode = movable;
        useHandCursor = movable;

        if (movable) {
            _pressed = false;
            addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);    
        }
    }
    
    private function mouseDownHandler(event : MouseEvent) : void {

        if (_pressed) return;
        
        stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler);
        stage.addEventListener(Event.MOUSE_LEAVE, stage_mouseLeaveHandler);

        _dragX = mouseX;
        _dragY = mouseY;
        _pressed = true;
    }

    private function stage_mouseMoveHandler(event : MouseEvent) : void {
        x += (mouseX - _dragX);
        y += (mouseY - _dragY);
    }
    
    private function stage_mouseUpHandler(event : MouseEvent) : void {
        releaseMouse();
    }
    
    private function stage_mouseLeaveHandler(event : Event) : void {
        releaseMouse();
    }

    private function releaseMouse() : void {

        if (!_pressed) return;

        stage.removeEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMoveHandler);
        stage.removeEventListener(Event.MOUSE_LEAVE, stage_mouseLeaveHandler);
        
        _pressed = false;
    }
}