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, setEllipseCentre); stage.addEventListener(MouseEvent.MOUSE_MOVE, drawEllipseMove); stage.addEventListener(MouseEvent.MOUSE_UP, releaseMouseButton); } private function releaseMouseButton(e : MouseEvent) : void { down = false; } private function drawEllipseMove(e : MouseEvent) : void { if(down) { drawGrid(); drawEllipse(xMid, yMid, Math.abs(xMid-e.stageX/10), Math.abs(yMid-e.stageY/10)); } } private function setEllipseCentre(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(); } private function drawEllipse(x0: int, y0 : int, a : int, b : int) : void { if (a == 0 || b == 0) return; a = Math.abs(a); b = Math.abs(b); var a2 : int = 2*a * a; var b2 : int = 2*b * b; var error : int = a*a*b; var x : int = 0; var y : int = b; var stopy : int = 0; var stopx : int = a2 * b ; while (stopy <= stopx) { mySetPixel(x0 + x, y0 + y); mySetPixel(x0 - x, y0 + y); mySetPixel(x0 - x, y0 - y); mySetPixel(x0 + x, y0 - y); ++x; error -= b2 * (x - 1); stopy += b2; if (error <= 0) { error += a2 * (y - 1); --y; stopx -= a2; } } error = b*b*a; x = a; y = 0; stopy = b2*a; stopx = 0; while (stopy >= stopx) { mySetPixel(x0 + x, y0 + y); mySetPixel(x0 - x, y0 + y); mySetPixel(x0 - x, y0 - y); mySetPixel(x0 + x, y0 - y); ++y; error -= a2 * (y - 1); stopx += a2; if (error < 0) { error += b2 * (x - 1); --x; stopy -= b2; } } } } }