AS3.0: Random plaatsing van objecten over vorm zonder overlapping.

Ik had een script nodig om objecten willekeurig over een vorm te verspreiden zonder dat ze elkaar overlappen. Op internet viel niet veel te vinden, dus heb ik zelf een script geschreven om dit te doen. Misschien dat ik er nog iemand blij mee kan maken.

image

/**
 * HitTestDistribution
 * 
 * Author: Dion Snoeijen.
 * 
 * Purpose: Distribute objects on target shape without overlapping.
 * 
 **/
package 
{
    import flash
.display.Sprite;
    
import flash.display.StageAlign;
    
import flash.display.StageQuality;
    
import flash.display.StageScaleMode;
    
import flash.geom.Point;
    
    
[SWF(width="800"height="800"frameRate="30"backgroundColor="#FFFFFF")]
    
public class HitTestDistibution extends Sprite
    {
        
private var circle:Sprite;
        private var 
dots:Array;
        
        private var 
maxAttempts:int 500;
        private var 
dotCount:int 200;
        
        public function 
HitTestDistibution()
        
{
            
/**
             * Initialize stage.
             **/
            
this.stage.scaleMode StageScaleMode.NO_SCALE;
            
this.stage.align StageAlign.TOP_LEFT;
            
this.stage.quality StageQuality.BEST;
            
            
/**
             * Init distribution target.
             * This can be any shape, I just use a circle for the example.
             **/
            
this.circle this.dot(300400400);
            
this.addChild(this.circle);
            
            
this.dots = new Array();
            for(var 
i:int this.dotCount i++)
            
{
                dots
.push(this.dot(20));
            
}
            
            
//Go go go!!!        
            
this.distribute();
        
}
        
        
private function distribute():void
        {    
            
/**
             * Keep track of successfully placed objects.
             **/
            
var placed:Array = new Array();
            
            var 
j:int 0;
            while(
this.dots.length)
            
{
                
var point:Point = new Point(Math.random() * this.stage.stageWidthMath.random() * this.stage.stageHeight);
                var 
dot:Sprite Sprite(this.dots[j]);
                
                if(
this.circle.hitTestPoint(point.xpoint.ytrue))
                
{
                    
/**
                     * Ok, the dot is on target.
                     **/
                    
dot.point.x;
                    
dot.point.y;
                    
this.addChild(dot);
                    
                    var 
attempts:int;
                    if(
this.hittingPreviousObject(dotplaced))
                    
{
                        
/**
                         * The dot is overlapping a previously placed dot.
                         * Remove it and try again.
                         * Keep track of attempts to prevent a infinite loop.
                         **/
                        
this.removeChild(dot);
                        
attempts++;
                        if(
attempts >= this.maxAttempts)
                        
{
                            trace
('Aborted after 500 attempts. Placed ' ' objects.');
                            break;
                        
}    
                    }
                    
else
                    
{
                        
/**
                         * The dot is happy and free, continue with the next.
                         **/
                        
attempts 0;
                        
placed.push(dot);
                        
j++;
                    
}
                }
            }
        }

        
private function hittingPreviousObject(target:Spriteobjects:Array):Boolean
        {
            
var hit:Boolean false;
            for 
each(var object:Sprite in objects)
            
{
                hit 
target.hitTestObject(object);
                if(
hit)
                
{
                    
/**
                     * Some previously placed object is overlapping the target.
                     **/
                    
break;
                
}
            }
            
return hit;
        
}
        
        
private function dot(r:Numberx:Number 0y:Number 0):Sprite
        {
            
var d:Sprite = new Sprite();
            
d.graphics.beginFill(Math.random() * 0xFFFFFF);
            
d.graphics.drawCircle(xyr);
            
d.graphics.endFill();
            
            return 
d;    
        
}
    }

Precies wat ik nodig had.

Door Defi op 12 11 2011

So much info in so few words. Tosloty could learn a lot.

Door Belle op 19 11 2011



Commentaar

Alleen je naam wordt zichtbaar op de site

Naam:

Email:

URL:

Onthoud mijn persoonlijke informatie?

Informeer me over follow-up's?

Captcha






Hallo!

Mijn naam is Dion Snoeijen en ik programmeer en ontwerp websites, flash app’s en maak 3D visualisaties.

Recente Tweet

19-02 17:36


Weblog categorieën