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

singleslit(壁ドラッグ可、波長可変)

Get Adobe Flash player
by irobutsu 19 Feb 2010
    Embed
/**
 * Copyright irobutsu ( http://wonderfl.net/user/irobutsu )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7fw5
 */

package {

    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.IDataInput;
    import flash.accessibility.Accessibility;
    import flash.display.Sprite;

    [SWF(width="400", height="400",backgroundColor=0xffffff)]

    public class singleslit extends Sprite {
    	private	var px:int=280;
  	private var py:int=280;
	private	var hu:kabeEdge;
       	private var hd:kabeEdge;
       	private var t:int=0;
	private var nowDrag:Sprite=null;
	private var lambda:Tsumami;
	private var omega:Number=0.15;

	protected function namiColor(a:Number):int {
	    var kk:int;
	    if( a>0 ) {
		kk= a*256.0;
		if( kk>255 ) {kk=255;}
		return(0xffffff-0x0101*kk);
	    } else {
		kk= -256.0*a;
		if( kk>255 ) {kk=255;}
    		return (0xffffff-0x010100*kk);
	    }
	}
       	public function singleslit() {
	    hd=new kabeEdge(150,0,193);
	    hu=new kabeEdge(250,208,360);
	    lambda=new Tsumami(100,30,300,370,"波長",this);
	    // 初期値100、最小20、最大300。位置370。
	    addChild(hd);
	    addChild(hu);
	    addChild(lambda);

       	    addEventListener(Event.ENTER_FRAME,update);
	    stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
	    stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
        }

	private function mouseUp(e:MouseEvent):void {
	    if( nowDrag != null ) {
		if( nowDrag==hd) hd.endDrag();
		if( nowDrag==hu) hu.endDrag();
		if( nowDrag==lambda) lambda.endDrag();
		nowDrag=null;
	    }
	}

	private function mouseDown(e:MouseEvent):void {
	    var x:int=e.stageX;
	    var y:int=e.stageY;
	    if( nowDrag != null ) {
		return;
	    }
	    if( hu.hitTestPoint(x,y,false) ) {
		nowDrag=hu;
		hu.goDrag();
	    } else if( hd.hitTestPoint(x,y,false) ) {
		nowDrag=hd;
		hd.goDrag();
	    } else if( lambda.hitTestPoint(x,y,false) ) {
		nowDrag=lambda;
		lambda.goDrag();
	    }else{
		px=e.stageX;
		py=e.stageY;
	    }
	}
        private function update(e:Event):void {
	    var y1:int;
            var y2:int;
            var i:int;
            var j:int;
	    t  +=1;
	    graphics.clear();
            graphics.lineStyle( 3, 0x007700 );
            graphics.moveTo(60,0);
            graphics.lineTo(60,hd.value()+10);
            graphics.moveTo(60,hu.value());
            graphics.lineTo(60,370);
	    for(i=60+2; i<px ; i++ ) {
        	for(j=hd.value()+12; j<hu.value() ; j+=10 ) {
        	    y1= j+(py-j)*(i-60-2)/(px-60-2);
        	    y2=j+(py-j)*(i-60-1)/(px-60-2);
        	    graphics.lineStyle(1,namiColor(Math.sin(
        			2*Math.PI*Math.sqrt(
        			    ((i-60-2)*(i-60-2)+(j-y1)*(j-y1)
        			    )as Number)/lambda.value()-omega*t
         		    )));
        	    if(y2>y1+1 ) {
        		graphics.drawRect(i,y1,1,y2-y1);
        	    } else if ( y1 > y2+1 ) {
        		graphics.drawRect(i,y1,1,y1-y2);
        	    } else {
        		graphics.drawRect(i,y1,1,1);
		    }
		}
            }
	    i=0;
            var a:Number=0.0;
            for(j=hd.value()+12; j<hu.value() ; j+=10) {
            	a+= Math.sin(2*Math.PI*Math.sqrt((
            		    (px-62)*(px-62)+(j-py)*(j-py)
            		) as Number)/lambda.value()-omega*t);
            	i++;
            }
            graphics.lineStyle(1,0x0);
            graphics.beginFill(namiColor((a/(i as Number))));
            graphics.drawRect(5,5,20,20);
            graphics.endFill();
	    hd.update(e);
	    hu.update(e);
	    lambda.update(graphics);
        }
    }

}

import flash.events.*;
import flash.events.MouseEvent;
import flash.utils.IDataInput;
import flash.accessibility.Accessibility;
import flash.display.*;
import flash.system.*;
import flash.geom.*;
import flash.text.*

class Tsumami extends Sprite {
    private var val:int;
    private var min:int;
    private var max:int;
    private var nowDrag:Boolean;
    private var txt:TextField;
    private var label:String;

    public function Tsumami(now:int,minimum:int,maximum:int,Y:int,l:String,parent:Sprite) {
	nowDrag=false;
	min=minimum;
	max=maximum;
	y=Y;
	x=now;
	label=l;
	txt=new TextField();y
	txt.text=label;
	txt.width=30;
	txt.height=30;
	txt.y=Y;
	parent.addChild(txt);
    }

    public function goDrag():void {
	nowDrag=true;
	startDrag(false,new Rectangle(min,y,max-min,0));
    }
    public function endDrag():void {
	nowDrag=false;
	stopDrag();
    }

    public function doDrag(e:MouseEvent):void {
	val= e.stageX;
	if( val< min ){val=min; }
	if( val> max ){val=max; }
	x=val;
    }

    public function setValue(a:int):void { 
	if( a>max ) {
	    a=max;
	}
	if( a<min ) {
	    a=min;
	}
	val=a; 
	x=a;
    }

    public function value():int {val=x; return val;}

    public function update(egraphics:Graphics):void {
	egraphics.lineStyle(1,0);
	egraphics.beginFill(0x777777);
	egraphics.drawRect(min,y+10,max-min,10);
	egraphics.endFill();

	graphics.lineStyle(1,0x007700);
	if( nowDrag ) {
	    graphics.beginFill(0xff0000);
	}else {
	    graphics.beginFill(0xaa0000);
	}
	graphics.drawRect(0,0,10,30);
	graphics.endFill();	
    }
}


class kabeEdge extends Sprite {
    private var val:int;
    private var min:int;
    private var max:int;
    private var nowDrag:Boolean;    
    function kabeEdge(value:int,minimum:int,maximum:int):void {
	nowDrag=false;
	val=value;
	min=minimum;
	max=maximum;
	x=50;
	y=value;
    }
    public function goDrag():void {
	nowDrag=true;
	startDrag(false,new Rectangle(50,min,0,max-min));
    }
    public function endDrag():void {
	nowDrag=false;
	stopDrag();
    }

    public function doDrag(e:MouseEvent):void {
	val= e.stageY;
	if( val< min ){val=min; }
	if( val> max ){val=max; }
	y=val;
    }

    public function value():int { return y;}
    public function update(e:Event):void {
	graphics.lineStyle(2,0x007700);
	if( nowDrag ) {
	    graphics.beginFill(0xff0000);
	}else {
	    graphics.beginFill(0xaa0000);
	}
	graphics.drawRect(0,0,10,10);
	graphics.endFill();
    }
}