A Flash Developer Resource Site

# Thread: Random pixels in an oval (ellipse) shaped area

1. ## Random pixels in an oval (ellipse) shaped area

Hi,

I need to put a lot of random points inside some oval shaped area or, in other words, between the inner and outer limits of two ellipses.

You can test the following code I made and you'll understand what I need:

import flash.display.Bitmap;
import flash.display.BitmapData;

var white:uint = 0xFFFFFF;
var black:uint = 0x000000;

var dist:Number;
var posX:Number;
var posY:Number;
var angle:Number;

var myBitmapData:BitmapData = new BitmapData(800, 800, false, black);

function randomRange(minNum:Number, maxNum:Number):Number {
return ((Math.random() * (maxNum - minNum + 1)) + minNum);
}

return (nDegree * Math.PI/180);
}

if (nAngle >= 0 && nAngle <= 90) {
}

if (angle >= 90 && angle <= 180) {
}

if (angle >= 180 && angle <= 270) {
}

if (angle >= 270 && angle <= 360) {
}

}

function generatePoints(innerLimit:Number, outerLimit:Number, nbStars:Number):void {
var i:uint = 0;
var oldDist:Number = 0;

while (i < nbStars) {
dist = randomRange(innerLimit, outerLimit);
oldDist = dist;
angle = Math.random() * 360;

// Convert polar to cartesian

myBitmapData.setPixel(Math.round(posX)+400, Math.round(posY)+400, white);

i = i + 1
}
}

generatePoints(250, 300, 25000);

var myBitmapImage:Bitmap = new Bitmap(myBitmapData);

Test it with a stage of at least 800x800 pixels.

My problem with this code is that there are deformities at each 90 degrees and I've never been able to eliminate these.

I'm sure there's a better way to do what I want but I just didn't succeed at finding it.

Any help would be appreciated.

Thanks.

PS: If you have a fast computer you can change the 3rd parameter in the line "generatePoints(250, 300, 25000);" to 100000 (or even more) and the deformities will get clearer.

2. Originally Posted by sevynos
I'm sure there's a better way to do what I want but I just didn't succeed at finding it.
Indeed, there is. Or you could just pick random points in outscribed box until one of them eventually lands inside your ellipse.

3. Originally Posted by realMakc
Indeed, there is.
This guy actually deals with circles, so in case it's not obvious how to use it with ellipses, I made the example here.

4. Or maybe try something like this...
Code:
```import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;

var white:uint = 0xFFFFFF;
var black:uint = 0x000000;
var dist:Number;
var posX:Number;
var posY:Number;
var angle:Number;
var PI2:Number = Math.PI * 2;
var myBitmapData:BitmapData;
var myBitmapImage:Bitmap;

myBitmapData = new BitmapData(800,800,false,black);
generatePoints(myBitmapData, 400, 400, 250, 300, 90, 10, 55000);
myBitmapImage = new Bitmap(myBitmapData);

function generatePoints(bd:BitmapData, cenX:int, cenY:int, innerLimit:Number, outerLimit:Number, offX:int, offY:int, nbStars:Number):void {
var i:uint = 0;
dist= 0;
while (--nbStars > 0) {
dist = randomRange(innerLimit,outerLimit);
angle = Math.random() * PI2;
posX = cenX + ((dist + offX) * Math.cos(angle));
posY = cenY - ((dist + offY) * Math.sin(angle));
bd.setPixel(Math.round(posX), Math.round(posY), white);
}
}
function randomRange(minNum:Number, maxNum:Number):Number {
return ((Math.random() * (maxNum - minNum + 1)) + minNum);
}```

5. Thanks for your replies realMakc and dawsonk,

But, unfortunately, I ran into another problem: The script is too long to execute on Flash as I need to generate about a million points. When it all be over I won't be surprised if it take a full week to generate them all

So I translated my code to VB6 and it work except for a little bug.

If some of you are good in VB6 you can look at the thread on another site (here's for Flash!) at the following link:

http://itknowledgeexchange.techtarge...ation-problem/

You'll see my problem when you'll look at the images.

If you're not registered on itknowledgeexchange (or don't want to) feel free to reply here (if it's ok with this site's rules!)

Thanks

6. Sorry man, but obviously people have better things to do than fixing your sloppy coding. Here is the code that generates over a million random points within elliptic arc sector in a quarter of second.

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap