A Flash Developer Resource Site

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3

    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);
    }

    function toRad(nDegree:Number):Number {
    return (nDegree * Math.PI/180);
    }

    function addDist(nAngle:Number, nDist:Number, nMaxAdd:Number):Number {
    var nPcAdd:Number = 0;
    var nFinalAdd:Number = 0;

    if (nAngle >= 0 && nAngle <= 90) {
    nPcAdd = (90 - nAngle)/90;
    }

    if (angle >= 90 && angle <= 180) {
    nPcAdd = (angle - 90)/90;
    }

    if (angle >= 180 && angle <= 270) {
    nPcAdd = (270 - angle)/90;
    }

    if (angle >= 270 && angle <= 360) {
    nPcAdd = (angle - 270)/90;
    }

    nFinalAdd = nMaxAdd * nPcAdd;

    return nFinalAdd;
    }

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

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

    dist += addDist(angle, dist, 90);
    added = dist - oldDist;

    // Convert polar to cartesian
    posX = dist * Math.cos(toRad(angle));
    posY = dist * Math.sin(toRad(angle));

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

    i = i + 1
    }
    }

    generatePoints(250, 300, 25000);

    var myBitmapImage:Bitmap = new Bitmap(myBitmapData);
    addChild(myBitmapImage);

    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. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    Quote Originally Posted by sevynos View Post
    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.
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    Quote Originally Posted by realMakc View Post
    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.
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    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);
    addChild(myBitmapImage);
    
    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. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    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. #6
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    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.
    who is this? a word of friendly advice: FFS stop using AS2

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