好久没来了,这次给大家带来一点东东。
大家都知道Julia集是一种迭代算法,即不断将运算所得结果再投入算法计算,使得结果不断接近真实值
例如我们计算π,e都是用这个算法。
Julia集有很多形式,都需要消耗大量资源才能得出结果。而我们可以使用这个算法测试计算机性能。
因为使用BitmapData支持,所以请使用FlashPlayer8.0播放。
/**
ActionScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1
@class Test.as
@package
@author tcper@163.com Loki_tang
@codehint
@example: import Test;
var someObject:Test=new Test;
someObject.tester(.01);
@tooltip
*/
import flash.display.BitmapData;
class Test extends MovieClip {
private var n:Number;
private var cx:Number;
private var cy:Number;
private var x2:Number;
private var y2:Number;
private var rgb:Number;
private var timeGot:Number;
private var rgbOffset:Number;
private var pixelPos:Number;
private var mc:MovieClip;
private var mcOrigin:MovieClip;
private var myBitmapData:BitmapData;
//a,b都是Julia集的参数
private static var a:Number = .5;
private static var b:Number = .55;
private static var pixelNum:Number = 0;
private static var timeLimitation:Number = 4000;//计算时间限制
function Test() {
bitmapCreator();
}
private function bitmapCreator():BitmapData {
myBitmapData = new BitmapData(400, 400, false, 0xcccccccc);
return myBitmapData;
}
private function mcCreator():MovieClip {
mcOrigin = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
return mcOrigin;
}
private function getRGB(i:Number,u:Number):Number {
rgb = (i-u)*200;
return rgb;
}
private function getPixelPos(i:Number):Number {
pixelPos = Math.floor((i+1.5)*133.3);
return pixelPos;
}
private function timeCheck(i:Number):Boolean {
if ((getTimer()-i) return true;
} else {
return false;
}
}
//public Method;
public static function pixelStatistic():Number {
return pixelNum;
}
public function tester(u:Number):Void {
rgbOffset = Math.floor(100*Math.random());
timeGot = getTimer();
var mc = mcCreator();
mc.attachBitmap(bitmapCreator(), this.getNextHighestDepth());
mc._x = mc._y=-200;
//以下是Julia算法.
for (var i = -1.5; i<=1.5; i += u) {
for (var j = -1.5; j<=1.5; j += u) {
cx = i;
cy = j;
for (var n = 1; n<=100; n++) {
pixelNum++;
x2 = cx*cx-cy*cy+a;
y2 = 2*cy*cx+b;
cx = x2;
cy = y2;
if (cx*cx+cy*cy>4) {
break;
}
if (!timeCheck(timeGot)) {
return;
}
}
bitmapCreator().setPixel(getPixelPos(i), getPixelPos(j), getRGB(n,rgbOffset));
}
}
}
}