TwelvestoneFlash

a point within a given radius


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
baron ruhstoff
 
2009-01-03

All righty... I can get a point within a given range of another point: // returns a point within a randomly determined distance from another point public static function randomLocationInRange(p : Point, range : Number) { var pResult : Point = new Point(0,0); pResult.x = randomRange(p.x, range); pResult.y = randomRange(p.y, range); // done! return pResult; } However, this returns a point within a rectangle. What I would really like is to get a point within a circle. I can get the random angle, but I'm stumped with regards to determining the subsequent x,y vals. My thinking is that the solution involves reversing this : // returns the distance between two points public static function distance(p1 : Point, p2 : Point) { var dx : Number = 0 ; var dy : Number = 0; dx = p2.x - p1.x; dy = p2.y - p1.y; return Math.sqrt(dx * dx + dy * dy); } Trigonometry confuses and frightens me. Any suggestions?

baron ruhstoff
 
2009-01-03

Got it. Sloppy, but it works: public static function randomLocationInRange(p : Point, range : Number, circle:Boolean = true) { var pResult : Point = new Point(0,0);

        // TODO: work with radians rather than degrees
        var angle:Number = Math.floor(Math.random()*360);

        if (circle){
            // finds a point within a circle
            pResult.x = p.x + Math.cos(toRadians(angle))*range*Math.random();
            pResult.y = p.y + Math.sin(toRadians(angle))*range*Math.random();
        } else {
            // finds a point within a rectangle
            pResult.x = randomRange(p.x, range);
            pResult.y = randomRange(p.y, range);                
        }           
        // done!
        return pResult;
    }

Yeah, defining angle in radians would be better than defining it in degrees, but as I said... trigonometry, confusion, and fear.

JLM
 
2009-01-03

var angle:Number = Math.random()*2*Math.PI;

baron ruhstoff
 
2009-01-06

I am clearly an idiot. k

JLM
 
2009-01-06

no just used to degrees

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

a point within a given radius