Snippet RandomLocInCircle

XeNiM666

I lurk for pizza
Reaction score
138
I made this out of boredom. Just wanted to test my JASS-ing skills.

This, basically picks a random location in a circle. Thought it might be useful to others.

Just copy this to your map header:
JASS:
function RandomLocInCircle takes real x_pos, real y_pos, real min_distance, real max_distance returns location
    local location center = Location( x_pos , y_pos )
    local real dis = GetRandomReal( GetRandomReal( min_distance , max_distance / 2.15 ) , max_distance )
    local real ang = GetRandomReal( 0.00 , 360.00 ) * ( 3.14159 / 180.00 )
    local real x = GetLocationX( center ) + dis * Cos( ang )
    local real y = GetLocationY( center ) + dis * Sin( ang )
    call RemoveLocation( center )
    set x_pos = 0.00
    set y_pos = 0.00
    set min_distance = 0.00
    set max_distance = 0.00
    return Location( x , y )
endfunction


And use this function:
JASS:
RandomLocInCircle( x_coordinate , y_coordinate , min_range , max_range )

Very east :)

Example of usage: Picks a random location with a maximum range of 700
JASS:
local location loc = RandomLocInCircle( GetLocationX(udg_Point[1]) , GetLocationY(udg_Point[1]) , 0.00 , 700.00 )

aaa.jpg


This is my first Snippet so go easy on me and comments are well appreciated!!

~XeNiM666
 

Attachments

  • RandomLocInCircle.w3x
    24.9 KB · Views: 163

Darius34

New Member
Reaction score
30
Well, it's not very practical in the sense that most peope use coordinates; this would require another location to be created, etc. It also uses a BJ function, which could be inlined.

Finally, there's a greater bias towards points the in the centre of the circle with this method.

Nothing is random Ace.
True, but this snippet would be more likely to pick points in the centre even with an ideal RNG.
 

SerraAvenger

Cuz I can
Reaction score
234
Well, it's not very practical in the sense that most peope use coordinates; this would require another location to be created, etc. It also uses a BJ function, which could be inlined.
Also, 2 Handle leaks,

True, but this snippet would be more likely to pick points in the centre even with an ideal RNG.

Actually it has the same chance to pick the center like it has to pick one in the outer ring. Just that the outer ring is bigger, and thus the number of picks per area is smaller, resulting in a higher density towards the center.
 

Darius34

New Member
Reaction score
30
Increased the random-ness
Uh, there still is that high density of picks in the centre, because you're using a polar offset. CMIIW

* dis / dis
This doesn't do anything. Also, constants like bj_PI and bj_DEGTORAD don't have to be inlined.
 

emjlr3

Change can be a good thing
Reaction score
395
you're leaving a handle hostage, and as said before, this is exactly not random
also, I'm going to go out on a limb here, but I think storing "180.00 / 3.1415" as a local as opposed to doing the division three time would be faster
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> storing "180.00 / 3.1415"

Well, there's a ready to use constant for that, bj_DEGTORAD.
Nothing slow there.
Nothing wrong in using it either.

However, converting twice to radians... that is very random indeed.
And wrong.

The angle should also simply be in radians to begin with...
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
Your snippet doesn't leak, it's up to the user to fix the leaks himself.
Like this:
JASS:
local location l = RandomLocInCircle(..)
call RemoveLocation(l)
set l = null
 

emjlr3

Change can be a good thing
Reaction score
395
it takes a loc that it never removes
 

XeNiM666

I lurk for pizza
Reaction score
138
ok now somehow i think i increased the random-ness. it doesnt pick the center more often. also i think i found out the handle leaks??
 

saw792

Is known to say things. That is all.
Reaction score
280
JASS:
function RandomLocInCircle takes real x_pos, real y_pos, real min_distance, real max_distance returns location
    local real dis = GetRandomReal( GetRandomReal( min_distance , max_distance / 2.15 ) , max_distance )
    local real ang = GetRandomReal( -bj_PI , bj_PI)
    local real x = x_pos + dis * Cos( ang )
    local real y = y_pos + dis * Sin( ang )
    return Location( x , y )
endfunction


That's optimised a *little* bit. Lots of useless function calls in there. That isn't the best way to increase the randomness.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top