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

point arrangement problem

Suppose you wanted to arrange some points around a circle, one by one.
Once you place a point, you cannot move it.
You don't know how many points you will ultimately arrange.
As you place the points, try to place them far from the other points, especially the most recently placed points.
It would be nice if your algorithm could deterministically compute the position of any point in constant time.
Get Adobe Flash player
by wh0 26 Sep 2011
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hvzf
 */

package {
    import flash.display.*;
    import flash.events.*;
    public class FlashTest extends Sprite {
        
        private var n:uint = 0;
        
        public function FlashTest() {
            graphics.beginFill(0x000000);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            graphics.lineStyle(4, 0xffffff, 0.5);
            graphics.drawCircle(232.5, 232.5, 220);
            addEventListener(Event.ENTER_FRAME, frame);
        }
        
        private function place(n:uint):Number {
            // put your code here...
            var v:Number = 0;
            for (var b:uint = 1; b; b <<= 1) {
                var step:Number = 0.5 / b;
                var delta:Number = Math.round((1/3) / step) * step;
                if (n & b) v += delta;
            }
            return v;
        }
        
        private function frame(e:Event):void {
            new Dot(place(n), this);
            n++;
        }
        
    }
}

import flash.display.*;
import flash.events.*;

internal class Dot extends Shape {
    
    // private static const far:Number = 465 * Math.SQRT1_2 + 20;
    private static const far:Number = 220;
    
    private static function hue(r:uint):uint {
        return (
            0x010000 * Math.max(0, Math.min(0xff, Math.abs(r - 0x300) - 0x100)) +
            0x000100 * Math.max(0, Math.min(0xff, 0x200 - Math.abs(r - 0x200))) +
                       Math.max(0, Math.min(0xff, 0x200 - Math.abs(r - 0x400)))
        );
    }
    
    private var distance:Number = 1;
    
    public function Dot(v:Number, parent:DisplayObjectContainer) {
        graphics.beginFill(hue((v * 0x600) % 0x600));
        var t:Number = Math.PI * 2 * v;
        graphics.drawCircle(far * Math.sin(t), -far * Math.cos(t), 20);
        graphics.endFill();
        x = y = 232.5;
        parent.addChild(this);
        addEventListener(Event.ENTER_FRAME, frame);
    }
    
    private function frame(e:Event):void {
        distance += 0.05;
        if (distance >= 16) {
            removeEventListener(Event.ENTER_FRAME, frame);
            parent.removeChild(this);
        } else {
            scaleX = scaleY = 1 / distance;
            alpha = Math.min(1, 2 / distance / distance);
        }
    }
    
}