펄린노이즈(perlin noise)를 연습하다가 나온 실패작(?).
아래에 코드(ActionScript3.0)가 있지만 펄린노이즈 코드는 아닙니다.
그래도 그냥 올려봅니다...
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Vector3D;
import flash.utils.setInterval;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var bd:BitmapData = new BitmapData(500, 500, false);
var bmp:Bitmap = new Bitmap(bd);
bmp.x = bmp.y = 50;
addChild(bmp);
var vertices:Vector.<Vector.<Vector3D>> = new Vector.<Vector.<Vector3D>>();
for (var i:int = 0; i < 10; i++) {
vertices[i] = new Vector.<Vector3D>();
for (var j:int = 0; j < 10; j++) {
vertices[i][j] = new Vector3D(uRand(), uRand());
trace(i, j);
if(i>0 && j>0)
noise50(bd, i*50,j*50,vertices[i-1][j-1], vertices[i][j-1], vertices[i-1][j], vertices[i][j]);
}
}
}
private function uRand():Number
{
return Math.random()*2-1;
}
private function noise50(bd:BitmapData, destX:int, destY:int, gx0y0:Vector3D, gx1y0:Vector3D, gx0y1:Vector3D, gx1y1:Vector3D):void
{
var x0:Number = 0;
var y0:Number = 0;
var x1:Number = 1;
var y1:Number = 1;
for (var yi:Number = 0.00; yi < 1; yi+=0.02) {
for (var xi:Number = 0.00; xi < 1; xi+=0.02) {
var s:Number = gx0y0.dotProduct(new Vector3D(xi-x0, yi-y0));
var t:Number = gx1y0.dotProduct(new Vector3D(xi-x1, yi-y0));
var u:Number = gx0y1.dotProduct(new Vector3D(xi-x0, yi-y1));
var v:Number = gx1y1.dotProduct(new Vector3D(xi-x1, yi-y1));
var sxi:Number = 3*(xi-x0)*(xi-x0) - 2*(xi-x0)*(xi-x0)*(xi-x0);
var syi:Number = 3*(yi-y0)*(yi-y0) - 2*(yi-y0)*(yi-y0)*(yi-y0);
var a:Number = s+sxi*(t-s);
var b:Number = u+sxi*(v-u);
var zi:Number = a+syi*(b-a);
bd.fillRect(new Rectangle(destX+xi*50,destY+yi*50,1,1),0x010203*int((zi+0.5)*4095));
}
}
}
}
}