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.

/**
* 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(300, 400, 400);
this.addChild(this.circle);
this.dots = new Array();
for(var i:int = 0 ; i < 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(j < this.dots.length)
{
var point:Point = new Point(Math.random() * this.stage.stageWidth, Math.random() * this.stage.stageHeight);
var dot:Sprite = Sprite(this.dots[j]);
if(this.circle.hitTestPoint(point.x, point.y, true))
{
/**
* Ok, the dot is on target.
**/
dot.x = point.x;
dot.y = point.y;
this.addChild(dot);
var attempts:int;
if(this.hittingPreviousObject(dot, placed))
{
/**
* 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 ' + j + ' objects.');
break;
}
}
else
{
/**
* The dot is happy and free, continue with the next.
**/
attempts = 0;
placed.push(dot);
j++;
}
}
}
}
private function hittingPreviousObject(target:Sprite, objects: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:Number, x:Number = 0, y:Number = 0):Sprite
{
var d:Sprite = new Sprite();
d.graphics.beginFill(Math.random() * 0xFFFFFF);
d.graphics.drawCircle(x, y, r);
d.graphics.endFill();
return d;
}
}
}
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
New updated CoronaSDK Autocomplete for SublimeText2 from http://t.co/CHJEszHs #CoronaSDK #Ansca #Lua #st2 @lano78
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