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

つなひき

点どうしをつないでいる線の長さの合計を最短にするプログラム
四隅は固定で、線がだんだん短くなっていきます。
色が多い線は複数の線が重なっている線。
何度クリックしても同じ形ができれば、成功。
Get Adobe Flash player
by shohei909 06 Jul 2010
    Embed
/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aDbo
 */

package {
    //点どうしをつないでいる線の長さの合計を最短にするプログラム
    
    //四隅は固定で、線がだんだん短くなっていきます。
    //色が多い線は複数の線が重なっている線。
    
    //何度クリックしても同じ形ができれば、成功。
    
    import flash.display.Sprite;
    import flash.events.*;
    
    public class FlashTest extends Sprite {
        public var spider:SpiderTest = new SpiderTest();
        public function FlashTest() {
           addChild( spider );
           stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
        } 
        public function onDown(e:Event):void{
            spider.shaffle();
        }
    }
}


import flash.geom.*;
import flash.display.*;
import flash.events.*;
import mx.utils.*;



class SpiderTest extends Bitmap{
    public static const WIDTH:int = 456;
    public static const HEIGHT:int = 456;
    public static const NUM:int = 10;
    public static const CON:int = 2;
    public var baseBitmap:BitmapData;
    public var joints:Vector.<Joint>=new Vector.<Joint>();
    public var lines:Vector.<Line>=new Vector.<Line>();
    private var count:int=0;
    
    public function SpiderTest():void{
        baseBitmap = new BitmapData(WIDTH,HEIGHT,false,0x000000)      
        super(baseBitmap.clone());
        createJoints();shaffle();
        addEventListener(Event.ENTER_FRAME,onFrame);
    }
    
    public function onFrame(e:Event):void{
        drow();
        move();move();move();
        move();move();move();
        move();move();move();
        move();move();move();
    }
    private function createJoints():void{
        for(var i:int =0;i<NUM;i++){    joints.push(new Joint());    }
        for(var j:int =0;j<NUM;j++){    
            while(joints[j].connection.length <= CON){  connect(j);  }     
        }
    }
    
    private function connect(j:int):void{
        while(true){
            var rand:int = Math.random()*NUM;
            if(rand!=j){break;}
        }
        joints[j].connection.push(rand);
        joints[rand].connection.unshift(j);
        lines.push( new Line(joints[j],joints[rand]) );
    }
    
    public function shaffle():void{
        var margin:int = 20;
        joints[0].x=margin;         joints[0].y=margin;
        joints[1].x=WIDTH-margin;   joints[1].y=margin;
        joints[2].x=margin;         joints[2].y=HEIGHT-margin;
        joints[3].x=WIDTH-margin;   joints[3].y=HEIGHT-margin;
        for(var i:int =4;i<NUM;i++){
            joints[i].x = Math.random()*WIDTH;
            joints[i].y = Math.random()*HEIGHT;
        }
    }
    private function drow():void{
        bitmapData.lock();
        bitmapData.merge(baseBitmap,baseBitmap.rect,new Point(),200,200,200,200);
        for(var j:int =0;j<lines.length;j++){
            lines[j].draw();
            bitmapData.draw(lines[j]);
        }
        for(var i:int =0;i<NUM;i++){
            bitmapData.draw(joints[i],new Matrix(1,0,0,1,joints[i].x,joints[i].y));
        }
        bitmapData.unlock();
    }
    
    //最短経路探索
    private function move():void{
        var speed:Number = Math.random()*2;
        for(var i:int =4;i<NUM;i++){
            var l:int = joints[i].connection.length;
            for(var j:int =0;j<l;j++){
                var target:Joint = joints[joints[i].connection[j]]; 
                if( target.x-joints[i].x !=0  || target.y-joints[i].y !=0){
                    var dir:Number = Math.atan2(target.x-joints[i].x,target.y-joints[i].y);
                    joints[i].x += Math.sin(dir)*speed;
                    joints[i].y += Math.cos(dir)*speed;
                }
            }
        }
    }
}


class Joint extends Shape{
    public var connection:Vector.<int> = new Vector.<int>();
    public function Joint(){
        graphics.lineStyle(1,0x222222);
        graphics.beginFill(0xFFFFFF);
        graphics.drawCircle(0,0,6);
    }
}

class Line extends Shape{
    public var start:Joint;
    public var end:Joint;
    public var color:uint;
    public function Line(s:Joint,e:Joint){
        start=s; end=e;
        color = 0xFFFFFF*Math.random();    
        color = ColorUtil.adjustBrightness(color,30);
    }
    public function draw():void{
        graphics.clear();
        graphics.lineStyle(3*Math.random()+1,color);
        graphics.moveTo(start.x,start.y);
        graphics.lineTo(end.x,end.y);
    }
    
}