Placing items evenly spread in a circle
A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Placing items evenly spread in a circle

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    55

    Placing items evenly spread in a circle

    I have a round workfield and want to place 10 items evenly divided around the border. So basically I want to spread 10 items over a circle (not a line). How do I do this?

    Thank you.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Here it is roughly:

    PHP Code:
    var items:Array = [item1item2item3 ...]
    var 
    arcLength:Number = (Math.PI) / items.length;

    for(var 
    i:int 0items.lengthi++){
    //    var p:Point = Point.polar(100, arcLength * arcLength);
        
    var p:Point Point.polar(100arcLength);
        
    items[i].p.x;
        
    items[i].p.y;

    That's using a radius of 100px and the origin is centered at (0,0) so you probably want to adjust those (you can just add to the x,y to change the center point for the circle).
    Last edited by neznein9; 06-09-2009 at 04:35 PM.

  3. #3
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    Shouldn't it be:

    Point.polar(100, i * arcLength);

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    55
    Thank you - I will work this around with this code.

    The only problem I have now is that I want to create an array of 3 numbers out of 99. Though the all numbers must be different. E.G. [1,2,9] is okay whil [1,1,9] isn't. I have tried a lot and nothing worked. It always gave duplicate entries.

    Edit (fixed):
    PHP Code:
    private function gatherImages():void
            
    {
                for(var 
    0AMOUNTi++)
                {
                    
    getRandomNumber();
                }
                
                
    trace(loadThis);
            }

            private function 
    getRandomNumber():void
            
    {
                var 
    no:int Math.ceil(Math.random() * TOTAL);
                var 
    add:Boolean;
                
                do {
                    
    add true;
                    
                    for(var 
    item in loadThis)
                    {
                        if(
    loadThis[item] == no)
                        {
                            
    add false;
                            
    no Math.ceil(Math.random() * TOTAL);
                        }
                    }
                } while(
    add != true)
                
                
    loadThis.push(no);
            } 
    Last edited by Mpjraaij; 06-09-2009 at 03:49 PM.

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Good catch strille *fixed*.

    :P

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    55
    One more question about this, as I add all of this in a MC. However I can't read the width of it using toolCircle.width. Can I fix this? (Sorry this is only my second AS3 project). (this one I cannot find)

    PHP Code:
            public function addToCircle(_array:Array):void
            
    {
                
    toolCircle = new MovieClip();
                
                
    arcLength = (Math.PI) / _array.length;
                
                for(var 
    i:int 0_array.lengthi++)
                {
                    var 
    p:Point Point.polar(CIRCLEarcLength);
                    var 
    thisX p.x;
                    var 
    thisY p.y;
                    var 
    element = new Element(_array[i], 00.4thisXthisY);
                    
    toolCircle.addChild(element);
                }
                
                
    /*toolCircle.x = (thisStage.stageWidth - toolCircle.width) / 2;
                toolCircle.y = (thisStage.stageHeight - toolCircle.height) / 2;*/
                
                
    trace("s:"+thisStage.stageWidth+", t:"+toolCircle.width+", is:"+(thisStage.stageWidth toolCircle.width) / 2);
                
    addChild(toolCircle);
            } 

  7. #7
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    The width should work after it's rendered - try tracing that again in the next frame.

  8. #8
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    I never used the Point.polar method... is it much faster than using sin and cos?

  9. #9
    Member
    Join Date
    Mar 2009
    Posts
    55
    Quote Originally Posted by neznein9 View Post
    The width should work after it's rendered - try tracing that again in the next frame.
    It are .as classes. I am not using frames.

  10. #10
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    If you only need it for positioning, you can just use the radius:

    PHP Code:
    toolCircle.thisStage.stageWidth .5 CIRCLE;
    toolCircle.thisStage.stageHeight .5 CIRCLE

    @joshstrike - Point.polar is roughly 4x slower than using sin/cos (even accounting for degree => radian conversion)

  11. #11
    Member
    Join Date
    Mar 2009
    Posts
    55
    LOL how stupid of me. I should have seen that. - However I am not sure yet this will work, as the images have some 'dead space'.
    EDIT: This doesn't work because of the dead space.

    If using sin/cos is faster - can I still easily use this function?
    Last edited by Mpjraaij; 06-09-2009 at 07:23 PM.

  12. #12
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    PHP Code:
    public function addToCircle(_array:Array):void{
        
    toolCircle = new MovieClip();    
        
    arcLength = (Math.PI) / _array.length;

        for(var 
    i:int 0_array.lengthi++){
            var 
    thisX CIRCLE * (Math.cos(arcLength * (Math.PI 180)));
            var 
    thisY CIRCLE * (Math.sin(arcLength * (Math.PI 180)));
            var 
    element = new Element(_array[i], 00.4thisXthisY);
            
    toolCircle.addChild(element);
        }

        
    addEventListener('enterFrame', function(e:Event):void{
            
    removeEventListener(e.typearguments.callee);
            
    toolCircle.= (thisStage.stageWidth toolCircle.width) / 2;
            
    toolCircle.= (thisStage.stageHeight toolCircle.height) / 2;
        });

        
    //trace("s:"+thisStage.stageWidth+", t:"+toolCircle.width+", is:"+(thisStage.stageWidth - toolCircle.width) / 2);
        
    addChild(toolCircle);


  13. #13
    Member
    Join Date
    Mar 2009
    Posts
    55
    Thank you. That still, however, returns 0 as width.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com