flash on 2011-10-2
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import frocessing.color.ColorHSV;
public class Main extends Sprite
{
private var bd:BitmapData;
private var max:Number = 1.0;
private var min:Number = 0.0;
private var size:int = 256;
private var minSize:int = 2;
private var color:ColorHSV = new ColorHSV();
public function Main()
{
bd = new BitmapData(size, size, false);
addChild(new Bitmap(bd));
stage.addEventListener(MouseEvent.CLICK, onMouseClick);
onMouseClick();
}
private function onMouseClick(event:MouseEvent = null):void
{
generateHeightmap(0, 0, size, Math.random(), Math.random(), Math.random(), Math.random());
}
public function generateHeightmap(x:int, y:int, size:int, tl:Number, tr:Number, bl:Number, br:Number):void
{
if (size < minSize)
{
var value:Number = (tl + tr + bl + br) / 4;
color.gray(value * 256);
bd.fillRect(new Rectangle(x, y, x + minSize, y + minSize), color.value);
}
else
{
var midPoint:Number = (tl + tr + bl + br) / 4 + getRandomHeight(size);
if (midPoint < min) midPoint = min;
if (max < midPoint) midPoint = max;
var top:Number = (tl + tr) / 2;
var bottom:Number = (bl + br) / 2;
var left:Number = (tl + bl) / 2;
var right:Number = (tr + br) / 2;
size /= 2;
generateHeightmap(x, y, size, tl, top, left, midPoint);
generateHeightmap(x + size, y, size, top, tr, midPoint, right);
generateHeightmap(x, y + size, size, left, midPoint, bl, bottom);
generateHeightmap(x + size, y + size, size, midPoint, right, bottom, br);
}
}
public function getRandomHeight(value:Number):Number
{
return (Math.random() - 0.5) * value / size;
}
}
}