package { import flash.display.BitmapData; import flash.display.SimpleButton; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.KeyboardEvent; import flash.display.Bitmap; import flash.geom.Rectangle; import mx.controls.Button; import mx.containers.Canvas; public class Main extends Sprite { private var xMid : int, yMid : int; private var down : Boolean; public function Main():void { down = false; drawGrid(); if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); stage.addEventListener(MouseEvent.MOUSE_DOWN, setCircleCentre); stage.addEventListener(MouseEvent.MOUSE_MOVE, drawCircleMove); stage.addEventListener(MouseEvent.MOUSE_UP, releaseMouseButton); } private function releaseMouseButton(e : MouseEvent) : void { down = false; } private function drawCircleMove(e : MouseEvent) : void { if(down) { var x : int = xMid - e.stageX / 10; var y : int = yMid - e.stageY / 10; var r : int = Math.sqrt(x * x + y * y); drawGrid(); drawCircle(xMid, yMid, r); } } private function setCircleCentre(e : MouseEvent) : void { down = true; xMid = e.stageX / 10; yMid = e.stageY / 10; } private function drawGrid() : void { graphics.clear(); graphics.lineStyle(1, 0); for (var i : int = 0; i <= 30; ++i ) { graphics.moveTo(10 * i, 0); graphics.lineTo(10*i, 300); graphics.moveTo(0, 10 * i); graphics.lineTo(300, 10*i); } graphics.lineStyle(1, 0x00ff0000); graphics.moveTo(100, 0); graphics.lineTo(100, 300); graphics.moveTo(200, 0); graphics.lineTo(200, 300); graphics.moveTo(0, 100); graphics.lineTo(300, 100); graphics.moveTo(0, 200); graphics.lineTo(300, 200); graphics.lineStyle(1, 0); } private function mySetPixel(x : int, y : int) : void { graphics.beginFill(0x0); graphics.drawRect(x * 10, y * 10, 10, 10); graphics.endFill(); } /******************************************** * Bresenham's circle algorithm begins here * ********************************************/ private function drawCircle(x0 : int, y0 : int, r : int) :void { var y : int = r; var x : int = 0; var error : int = -r; mySetPixel(x0+x, y0+y); mySetPixel(x0+x, y0-y); mySetPixel(x0-x, y0+y); mySetPixel(x0-x, y0-y); mySetPixel(x0+y, y0+x); mySetPixel(x0+y, y0-x); mySetPixel(x0-y, y0+x); mySetPixel(x0-y, y0-x); while(x <= y) { // only one octant -> borders of octants are x=y and x = 0 mySetPixel(x0+x, y0+y); mySetPixel(x0+x, y0-y); mySetPixel(x0-x, y0+y); mySetPixel(x0-x, y0-y); mySetPixel(x0+y, y0+x); mySetPixel(x0+y, y0-x); mySetPixel(x0-y, y0+x); mySetPixel(x0-y, y0-x); // following three lines are the same as error = 2*x + 1; ++x; error += x; ++x; error += x; if (error >= 0) { --y; error -= 2 * y; } } } } }