In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Asteroid generation

http://www.kongregate.com/forums/4-game-programming/topics/237184-as3-problem-with-trigonometry

Click to generate new. Left one is using 953's method, right one is a version I made using points on a circle with a random deviance from a base radius.

With crappy controls!
Get Adobe Flash player
by antalg 17 Jan 2012
    Embed
package 
{
    import flash.text.TextFieldType;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.display.AVM1Movie;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.geom.Point;
    
    
    public class Main extends Sprite
    {
        private var astr:Array = [];
        
        private var input:Array = [];
        
        public function Main():void {
            var labels:Array = [new TextField(), new TextField(), new TextField()];
            input = [new TextField(), new TextField(), new TextField()];
            (input[0] as TextField).type = TextFieldType.INPUT;
            (input[1] as TextField).type = TextFieldType.INPUT;
            (input[2] as TextField).type = TextFieldType.INPUT;
            
            addChild(labels[0]);
            labels[0].x = 10;
            labels[0].y = 10;
            labels[0].text = "Size:";
            
            addChild(labels[1]);
            labels[1].x = 10;
            labels[1].y = 30;
            labels[1].text = "Steps:";
            
            addChild(labels[2]);
            labels[2].x = 10;
            labels[2].y = 50;
            labels[2].text = "Freq (0 def.):";
            
            addChild(input[0]);
            input[0].x = 50;
            input[0].y = 10;
            input[0].text = "60";
            
            addChild(input[1]);
            input[1].x = 50;
            input[1].y = 30;
            input[1].text = "20";
            
            addChild(input[2]);
            input[2].x = 90;
            input[2].y = 50;
            input[2].text = "0";
            
            astr = [draw(60,20), draw2(60,20)];
            addChild(astr[0]);
            astr[0].x = 100;
            astr[0].y = 200;
        
            addChild(astr[1]);
            astr[1].x = 300;
            astr[1].y = 200;
            
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mD );
        }
        
        private function mD(e:MouseEvent):void {
            var size:Number = Number(input[0].text);
            var steps:Number = Number(input[1].text);
            
            removeChild(astr[0]);
            removeChild(astr[1]);
            astr = [draw(size,steps), draw2(size,steps)];
            
            addChild(astr[0]);
            astr[0].x = 100;
            astr[0].y = 200;
        
            addChild(astr[1]);
            astr[1].x = 300;
            astr[1].y = 200;
        }

        
        private var current:Point = new Point();
        private var degree:Number = 0;
        private function draw(size:Number, steps:int):Shape
        {
            var s:Shape = new Shape();
            degree = 0;
            
            var bmd:BitmapData = new BitmapData(steps, 1, false, 0);
            
            var freq:Number = Number(input[2].text);
            freq = freq==0?steps/6:freq;
            bmd.perlinNoise(freq, 1, Math.random() * 5, Math.random() * 1000, true, true);
            
            var dp:Number = 2 * Math.PI / steps;
            var value:Number;
            
            s.graphics.lineStyle(1, 0x000000);
            s.graphics.moveTo(size * bmd.getPixel(0, 0) / 0xffffff, 0);
            
            for (var i:int = 1; i < steps; ++i)
            {
                value = size * bmd.getPixel(i, 0) / 0xffffff;
                s.graphics.lineTo(value * Math.cos(dp*i),value * Math.sin(dp*i));
                
            }
            s.graphics.lineTo(size * bmd.getPixel(0, 0) / 0xffffff, 0);
            
            return s;
        }
        
        private function draw2(size:Number, steps:int):Shape
        {
            size >>= 1;
            
            var s:Shape = new Shape();
            degree = 0;
            
            var dp:Number = 2 * Math.PI / (steps-1);
            var value:Number = size/2;
            
            s.graphics.lineStyle(1, 0x000000);
            s.graphics.moveTo(size, 0);
            
            for (var i:int = 1; i < steps-1; ++i)
            {
                var r:Number = (size>>1)*(Math.random()-.5);
                s.graphics.lineTo((size+r) * Math.cos(dp*i), (size+r) * Math.sin(dp*i));
                
            }
            s.graphics.lineTo(size, 0);
            
            return s;
        }
    }
}