Bresenham Circle Filled

var canvas:BitmapData = new BitmapData(400, 400, false, 0xCCCCCC);
addChild(new Bitmap(canvas));

fillCircle(100,100,50,0xFF0000);

function fillCircle(xp:Number,yp:Number, radius:Number, col:uint = 0x000000):void {
    var xoff:int =0;
	var yoff:int = radius;
	var balance:int = -radius;

	while (xoff <= yoff) {
		 var p0:int = xp - xoff;
		 var p1:int = xp - yoff;
		 
		 var w0:int = xoff + xoff;
		 var w1:int = yoff + yoff;
		 
		 hLine(p0, yp + yoff, w0, col);
		 hLine(p0, yp - yoff, w0, col);
		 
		 hLine(p1, yp + xoff, w1, col);
		 hLine(p1, yp - xoff, w1, col);
		
		if ((balance += xoff++ + xoff) >= 0) {
			balance-=--yoff+yoff;
		}
	}
}
 
function hLine(xp:Number, yp:Number, w:Number, col:uint):void {
	for (var i:int = 0; i < w; i++){
		canvas.setPixel(xp + i, yp, col);
	}
}

An implementation of yesterdays post that draws a filled circle instead of an outlined circle.

Leave a Reply

Your email address will not be published. Required fields are marked *