code on 2008-12-17
// write as3 code here..
package
{
import flash.display.*;
import flash.events.*;
[SWF(frameRate="30", backgroundColor="#000000")]
public class WaveSimp extends Sprite
{
private var _w:Number;
private var _h:Number;
private var _g:Graphics;
private var px:Number;
private var py:Number;
private var _P:Array;
private var _n:int = 40;
public function WaveSimp()
{
_w = stage.stageWidth;
_h = stage.stageHeight;
_g = graphics;
_P = [];
var delta:Number = Math.PI*2/_n;
for ( var n:int = 0; n<_n; n++ )
_P[n] = new WavePoint( _w/4, Math.cos(delta * n), Math.sin(delta * n) );
px = mouseX;
py = mouseY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove );
stage.addEventListener(Event.ENTER_FRAME, draw );
stage.quality = "medium";
}
private function mouseMove( e:MouseEvent ):void
{
px = ( mouseX - px ) * 0.15;
py = ( mouseY - py ) * 0.15;
_P[0].a += Math.sqrt( px*px + py*py );
px = mouseX;
py = mouseY;
}
private function cal(ps:Array):void
{
var i:int;
var wp0:WavePoint = WavePoint(ps[_n-1]);
var wp:WavePoint = WavePoint(ps[0]);
var wp1:WavePoint = WavePoint(ps[1]);
calAccel( wp0, wp, wp1 );
wp0 = wp;
wp = wp1;
for ( i = 1; i < _n-1; i++ )
{
wp1 = WavePoint(ps[i+1]);
calAccel( wp0, wp, wp1 );
wp0 = wp;
wp = wp1;
}
calAccel( wp0, wp, WavePoint(ps[0]) );
for ( i = 0; i < _n; i++ )
{
wp = WavePoint(ps[i]);
wp.vr += wp.a;
wp.a *= 0.98;
}
}
private function calAccel( wp0:WavePoint, wp:WavePoint, wp1:WavePoint ):void
{
var pv:Number = ( wp0.vr + wp1.vr - 4*wp.vr )*0.1;
wp.a += pv;
wp1.a += -pv*0.4;
wp0.a += -pv*0.4;
}
private function draw( e:Event ):void
{
cal(_P);
_g.clear();
_g.beginFill(0xffffff);
var wp:WavePoint = WavePoint( _P[_n - 1] );
_g.moveTo( _w*0.5 + wp.x, _h*0.5 + wp.y );
for ( var i:int = 0; i<_n; i++ )
{
wp = WavePoint( _P[i] );
_g.lineTo( _w*0.5 + wp.x, _h*0.5 + wp.y );
}
_g.endFill();
}
}
}
class WavePoint
{
public var r:Number;
public var vr:Number;
public var vx:Number;
public var vy:Number;
public var a:Number;
public function WavePoint( r:Number, vx:Number, vy:Number )
{
this.r = r;
this.vr = 0;
this.vx = vx;
this.vy = vy;
a = 0;
}
public function get x():Number
{
return (vr + r) * vx;
}
public function get y():Number
{
return (vr + r) * vy;
}
}