// forked from makc3d's flash on 2011-5-1
package {
import com.bit101.components.CheckBox;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer;
/**
* Explicit metaballs experiment.
*/
[SWF(backgroundColor="#FFFFFF")]
public class CassiniTest extends Sprite {
public var c2:CassiniMod = new CassiniMod;
private var N:Number = 60;
private var CENTER:Number = 232;
private var SIZE:Number = 70;
public function CassiniTest () {
addEventListener (Event.ENTER_FRAME, loop);
}
public function loop (e:Event):void {
var a:Number = 1 + Math.sin (2e-3 * getTimer ()*.5);
var c:CassiniMod = c2;
var ranges:Vector.<Range> = c.ranges (a);
graphics.clear ();
graphics.lineStyle (0,0,0);
graphics.beginFill(0x80C000,1);
var t:int, X:Number, Y:Number;
for each (var range:Range in ranges) {
// positive
X = range.x (0);
graphics.moveTo (CENTER + SIZE * X, CENTER);
for (t = 1; t < N+1; t++) {
X = range.x (t/N);
Y = c.y (X, a);
graphics.lineTo (CENTER + SIZE * X, CENTER - SIZE * Y);
}
// negative
X = range.x (0);
graphics.moveTo (CENTER + SIZE * X, CENTER);
for (t = 1; t < N+1; t++) {
X = range.x (t/N);
Y = c.y (X, a);
graphics.lineTo (CENTER + SIZE * X, CENTER + SIZE * Y);
}
}
graphics.endFill();
}
}
}
class Range {
public var a:Number;
public var b:Number;
public function Range (from:Number, to:Number) {
a = from; b = to;
}
public function x (t:Number):Number {
return a + (b - a) * t;
}
}
class CassiniMod {
public function y (x:Number, a:Number):Number {
var a2:Number = a*a;
var x2:Number = x*x;
var b:Number = 1 + (a2 + a) / 4;
var D:Number = Math.max (0, 4 * a2 * x2 + b * b);
var D2:Number = Math.max (0, Math.sqrt (D) - a2 - x2 );
return Math.sqrt (D2);
}
public function ranges (a:Number):Vector.<Range> {
var a2:Number = a * a;
var D1:Number = 3 * a2 - a - 4 ;
var D2:Number = 5 * a2 + a + 4 ;
if (D1 > 0) {
return new <Range> [
new Range ( -0.5 * Math.sqrt (D2), -0.5 * Math.sqrt (D1) ),
new Range ( +0.5 * Math.sqrt (D1), +0.5 * Math.sqrt (D2) )
];
}
return new <Range> [
new Range ( -0.5 * Math.sqrt (D2), +0.5 * Math.sqrt (D2))
];
}
}