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

Line slicer with QuickBox2D

Get Adobe Flash player
by Akiyah 31 Jul 2011
/**
 * Copyright Akiyah ( http://wonderfl.net/user/Akiyah )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/afUC
 */

package {
    import flash.display.*;
    import flash.geom.*;
    import flash.events.*;
    import flash.text.TextField;
    import com.actionsnippet.qbox.*;
    
    public class Main extends MovieClip
    {
        private const L:Number = 8;
        private const CUT_MAX:Number = 8;
        private var cut:Number = 0;
        private var pairs:Array;
        private var sim:QuickBox2D;

        public function Main() 
        {
            
            sim = new QuickBox2D(this);
            sim.createStageWalls();
            var box:QuickObject = sim.addBox({ x:4 + L/2, y:2 + L/2, width:L, height:L });
            pairs = [[ LineSliceObject.create(L), box]];

            stage.addEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler );
        }

        private function mouseDownHandler( e:MouseEvent ):void 
        {
            addEventListener( Event.ENTER_FRAME, enterFrameHandler );
            stage.addEventListener( MouseEvent.MOUSE_UP , mouseUpHandler );
        }
        
        private function t(p:Point):Point
        {
            return new Point(p.y, p.x);
        }
        
        
        private function enterFrameHandler( e:Event ):void 
        {
            if (cut >= CUT_MAX) {
                return;
            }
            cut++;
            
            var p1:Point = new Point(Math.random() * L, 0);
            var p2:Point = new Point(Math.random() * L, L);
            if (Math.random() > 0.5) {
                p1 = t(p1);
                p2 = t(p2);
            }

            var results:Array = [];
            for each (var pair:Array in pairs) {
                var sliceObj:LineSliceObject = pair[0];
                var quickObj:QuickObject = pair[1];
                var r:Array = sliceObj.slice(p1, p2);
                if (r != null) {
                    var ps1:Array = createPointsFromSliceObject(r[0]);
                    var ps2:Array = createPointsFromSliceObject(r[1]);
                    var q1:QuickObject = sim.addPoly({x:4, y:2, points:ps1, wireframe:false });
                    var q2:QuickObject = sim.addPoly({x:4, y:2, points:ps2, wireframe:false });
                    results.push([r[0], q1]);
                    results.push([r[1], q2]);
                    quickObj.destroy();
                } else {
                    results.push([sliceObj, quickObj]);
                }
            }
            pairs = results;
/*
            box.destroy();

            for each (var _sliceObj2:LineSliceObject in _lineSliceObjects) {
                var ps:Array = [];
                for each (var p:Point in _sliceObj2._pointArray) {
                    ps.push(p.x, p.y);
                }
                p = _sliceObj2._pointArray[0];
                ps.push(p.x, p.y);
                sim.addPoly({x:4, y:2, points:ps, wireframe:false });
            }
            */
        }

        private function createPointsFromSliceObject(sliceObject:LineSliceObject):Array
        {
                var ps:Array = [];
                for each (var p:Point in sliceObject._pointArray) {
                    ps.push(p.x, p.y);
                }
                p = sliceObject._pointArray[0];
                ps.push(p.x, p.y);
                return ps;
        }


        private function mouseUpHandler( e:MouseEvent ):void 
        {
            stage.removeEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler);
            stage.removeEventListener( MouseEvent.MOUSE_UP , mouseUpHandler);
            removeEventListener( Event.ENTER_FRAME, enterFrameHandler );

            sim.start();
            sim.mouseDrag();
        }
    }
}

import flash.geom.Point;

class LineSliceObject 
{
    public var _pointArray:Array;

    public static function create( n:Number ):LineSliceObject
    {
        return new LineSliceObject([new Point(0,0), new Point(n,0), new Point(n,n), new Point(0,n)]);
    }

    public function LineSliceObject( _pointArray:Array ) 
    {
        this._pointArray = _pointArray;
    }

    public function slice( _point1:Point , _point2:Point ):Array
    {
        var _pt1:Point = _point1;//globalToLocal( _point1 );
        var _pt2:Point = _point2;//globalToLocal( _point2 );
        var _newPointArray:Array = [ new Array() , new Array() ];
        var _numCloss:int = 0;
        
        for ( var i:int = 0; i < _pointArray.length ; i ++ ) 
        {
            var _pt3:Point = _pointArray[ i ];
            var _pt4:Point = ( _pointArray[ i + 1 ] ) ? _pointArray[ i + 1 ] : _pointArray[ 0 ];
            var _clossPt:Point = crossPoint( _pt1 , _pt2 , _pt3 , _pt4 );
            
            _newPointArray[ 0 ].push( _pt3 );
            if ( _clossPt )
            {
                _newPointArray[ 0 ].push( _clossPt );
                _newPointArray[ 1 ].push( _clossPt );
                _newPointArray.reverse();
                _numCloss ++;
            }
        }
        if ( _numCloss == 2 ) {
            var _newObj1:LineSliceObject = new LineSliceObject( _newPointArray[ 0 ] );
            var _newObj2:LineSliceObject = new LineSliceObject( _newPointArray[ 1 ] );
            return [_newObj1, _newObj2];
        } else {
            return null;
        }
    }
    
    private function crossPoint( _pt1:Point , _pt2:Point , _pt3:Point , _pt4:Point ):Point
    {    
        var _vector1:Point = _pt2.subtract( _pt1 );
        var _vector2:Point = _pt4.subtract( _pt3 );
        
        if ( cross( _vector1, _vector2 ) == 0.0) return null;
        
        var _s:Number = cross( _vector2 , _pt3.subtract( _pt1) ) / cross( _vector2 , _vector1 );
        var _t:Number = cross( _vector1, _pt1.subtract( _pt3 ) ) / cross( _vector1, _vector2 );

        if ( isCross( _s ) && isCross( _t ) )
        {
            _vector1.x *= _s;
            _vector1.y *= _s;
            return _pt1.add( _vector1 );
        }
        else return null;
    }
        
    private function cross( _vector1:Point , _vector2:Point ):Number
    {
        return ( _vector1.x * _vector2.y - _vector1.y * _vector2.x );
    }
    
    public static function isCross( _n:Number ):Boolean
    {
        return ( ( 0 <= _n ) && ( _n <= 1) );
    }
}