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

螺旋いろいろ

左クリックで螺旋タイプを切り替え
マウスカーソルが原点にあるときが基準パラメータ
参考:Wikipedia:渦巻 
http://ja.wikipedia.org/wiki/%E6%B8%A6%E5%B7%BB
Get Adobe Flash player
by Nao_u 15 Apr 2009
//
// 左クリックで螺旋タイプを切り替え
// マウスカーソルが原点にあるときが基準パラメータ
// 参考:Wikipedia:渦巻 
//      http://ja.wikipedia.org/wiki/%E6%B8%A6%E5%B7%BB
//
package {  
    import flash.display.Sprite;  
    import flash.events.*;  
    [SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="60")]   
     
    public class FlashTest extends Sprite {  
        public function FlashTest() {  
            Main = this;  
            initialize();  
            stage.addEventListener(Event.ENTER_FRAME,update);   
            stage.addEventListener(MouseEvent.MOUSE_UP,    MouseCheckUp);   
            stage.addEventListener(MouseEvent.MOUSE_DOWN,  MouseCheckDown);   
        }  
    }  
}          
 
import flash.display.Sprite;   
import flash.events.Event;  
import flash.events.MouseEvent;   
import flash.text.TextField;  
var Main:Sprite;  
var SCREEN_W:Number = 465; 
var SCREEN_H:Number = 465; 
var Text:TextField 

var Func:Object;
var FuncNo:int = 0;
var FUNC_MAX:int = 4;

var Step:Number = 2.0 * Math.PI / 30;
var Scale:Number = 100.0;

function initialize():void{  
    Text = new TextField();  
    Text.text = "----";
    Text.autoSize = "left";
    Main.addChild(Text);   
    
    graphicClear();

    FuncNo = 0;
    Func = setUseFunctionType( FuncNo );
}  

function graphicClear():void{
    Main.graphics.clear(); 
    Main.graphics.lineStyle(1.2,0xd0d000);     
    Main.graphics.moveTo( SCREEN_W/2,          0 );     
    Main.graphics.lineTo( SCREEN_W/2,   SCREEN_H );        
    Main.graphics.moveTo(          0, SCREEN_H/2 );     
    Main.graphics.lineTo(   SCREEN_W, SCREEN_H/2 );        
}

function setUseFunctionType( type:int ):Object{
    var func:Object = new Object;
    switch( type ){
    case 0: 
        func.name = "アルキメデスの螺旋";
        func.a = 0.0;
        func.b = 0.05;
        func.da = 5.0;
        func.db = 0.5;
        func.f = function( t:Number, a:Number, b:Number ):Number{
            return a + b * t;
        }
        break;

    case 1:
        func.name = "対数螺旋";
        func.a = 2.0;    
        func.b = 0.9;
        func.da = 40.0;    
        func.db = 0.4;
        func.f = function( t:Number, a:Number, b:Number ):Number{
            return a * Math.pow( b, t );
        }
        break;

    case 2:
        func.name = "双曲螺旋";
        func.a = 5.0;    
        func.b = 0.0;
        func.da = 200.0;    
        func.db = 3.0;
        func.f = function( t:Number, a:Number, b:Number ):Number{
            if( Math.abs(t) <= 0.0001 ) t = 0.0001;
            return b + a / t;
        }
        break;

    case 3:
        func.name = "リチュース";
        func.a = 1.0;    
        func.b = 0.0;
        func.da = 20.0;    
        func.db = 3.0;
        func.f = function( t:Number, a:Number, b:Number ):Number{
            if( Math.abs(t) <= 0.0001 ) t = 0.0001;
            return b + a / Math.sqrt(t);
        }
        break;

    case 4:
        func.name = "フェルマーの螺旋";
        func.a = 0.2;    
        func.b = 0.0;
        func.da = 2.0;    
        func.db = 0.0;
        func.f = function( t:Number, a:Number, b:Number ):Number{
            if( Math.abs(t) <= 0.0001 ) t = 0.0001;
            return b + a * Math.sqrt(t);
        }
        break;
    }
    return func;
}

var T:Number = 0;
var PrevT:Number = 0;
var MAX_T:Number = 2.0 * Math.PI * 16.0
function update(e :Event):void{  
    if( MouseData & MOUSE_LEFT_TRG ){
        if( ++FuncNo > FUNC_MAX ) FuncNo = 0;
        Func = setUseFunctionType( FuncNo );
    }
    graphicClear();

    var mx:Number = Main.stage.mouseX / SCREEN_W - 0.5;   
    var my:Number = Main.stage.mouseY / SCREEN_H - 0.5;   
    var a:Number = Func.a + mx * Func.da;
    var b:Number = Func.b + my * Func.db;
    Text.text = Func.name + "  a=" + Math.round(a*100)/100 + " b=" + Math.round(b*100)/100;
    T = PrevT = 0.000001;

    for( T=0; T<MAX_T; T+=Step ){
        draw( Func.f, a, b, T, PrevT ); 
        PrevT = T;    
    }
    MouseUpdate();
} 

function draw( func:Function, a:Number, b:Number, t:Number, pt:Number ):void{  
    var sx:Number, sy:Number, ex:Number, ey:Number;
    var col:int = 0xff4040;

    var r0:Number = func( pt, a, b );
    var r1:Number = func(  t, a, b );
    
    sx = r0 * Math.cos( pt );
    sy = r0 * Math.sin( pt );
    ex = r1 * Math.cos( t );
    ey = r1 * Math.sin( t );
    drawLine( sx, sy, ex, ey, col );

    // フェルマーの螺旋・特殊処理
    if( FuncNo == 4 ){
        r0 = -func( pt, a, b );
        r1 = -func(  t, a, b );
        sx = r0 * Math.cos( pt );
        sy = r0 * Math.sin( pt );
        ex = r1 * Math.cos( t );
        ey = r1 * Math.sin( t );
        drawLine( sx, sy, ex, ey, col );
    }
}

function drawLine( sx:Number, sy:Number, ex:Number, ey:Number, col:int ):void{  
    Main.graphics.lineStyle(1.2,col);     
    Main.graphics.moveTo( Scale * sx + SCREEN_W/2, Scale * sy + SCREEN_H/2 );     
    Main.graphics.lineTo( Scale * ex + SCREEN_W/2, Scale * ey + SCREEN_H/2 );        
} 

var MOUSE_LEFT:int = 0x01;  
var MOUSE_LEFT_TRG:int = 0x02;  
var MouseData:int;  
function MouseCheckDown(event:MouseEvent):void{  
    MouseData |= MOUSE_LEFT;  
    MouseData |= MOUSE_LEFT_TRG;  
}           

function MouseCheckUp(event:MouseEvent):void{  
    MouseData &= ~MOUSE_LEFT;  
}           

function MouseUpdate():void{  
    MouseData &= ~MOUSE_LEFT_TRG;  
}