Random Loc in Circular Range?

Kenoriga

Ultra Cool Member
Reaction score
34
Like "GetRandomLocInRect", is there one that can get a random location in a circle?

I found some Circle Rect thingies in JassCraft, and they require some mathematics. If there isn't any direct way to achieve "random range in a circle", I would have to make the circle range from a rect range myself?
 

Ghan

Administrator - Servers are fun
Staff member
Reaction score
889
This might be a weird way of doing it....

Center a rect on your point.
Pick a random point in your rect.
If it's X distance away or less from your point, go with it.
Else pick a new point....
 

Chocobo

White-Flower
Reaction score
409
Get middle of the circle, use a polar projection using a random offset between 0 and distance from the middle of circle to the range limit of circle, and use a random angle.

Something like (there may be some errors) :

JASS:
function RandomPointCircle takes real x, takes real y, takes real d returns Location
    return Location(x+GetRandomReal(0,d)*Cos(GetRandomReal(0,360)*3.14159/180.0, y+GetRandomReal(0,d)*Sin(GetRandomReal(0,360)*3.14159/180.0)
endfunction
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232

waaaks!

Zinctified
Reaction score
256
i think this is faster when coded correctly (if something is wrong) than Rheias, because polarprojectionbj still uses some cos and sin formulas
JASS:
function RandomPointCircle takes real x, takes real y, takes real d returns Location
    return Location(x+GetRandomReal(0,d)*Cos(GetRandomReal(0,360)*3.14159/180.0, y+GetRandomReal(0,d)*Sin(GetRandomReal(0,360)*3.14159/180.0)
endfunction


but if chocobo coded it wrongly then use rheias' function
JASS:
function RandomPointInCircle takes point p, real r returns point
    return PolarProjectionBJ(p,GetRandomReal(0,r),GetRandomReal(0,360))
endfunction
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Chocobo's code is better, no doubts, but if he wants to use location rather then x and y, the function is more comfortable.
 

Kenoriga

Ultra Cool Member
Reaction score
34
That is some tough formulas there o.o

What is real x and real y, the coordinates of the location? Then what about real d?
 

waaaks!

Zinctified
Reaction score
256
x and y is the center of ur target, and d is the distance on how far u will create the explosions
 

Doomhammer

Bob Kotick - Gamers' corporate spoilsport No. 1
Reaction score
67
JASS:
function RandomPointCircle takes real x, takes real y, takes real d returns Location
    return Location(x+GetRandomReal(0,d)*Cos(GetRandomReal(0,360)*3.14159/180.0, y+GetRandomReal(0,d)*Sin(GetRandomReal(0,360)*3.14159/180.0)
endfunction


this is not bad, but then I found some issues that could further be improved:
1) GetRandomReal(0,360)*3.14159/180.0 could be reduced by one operation to GetRandomReal(0,2)*3.14159
2) thinking about the geometric side, you'd only get a "circle" if the sin and cos are used on the same radius and the same angle which is your GetRandomReal(0,360)*3.14159/180.0; calling the randomizer twice will get two different randoms, and thus two different angles, and thus a random geometric shape, instead of random coordinates within the shape of a circle. this brings us to
3) 4 calls of randomizer where only 2 calls are necessary; they make your function slower than necessary

That's how an improved version could look like:
JASS:
function RandomPointCircle takes real x, real y, real d returns location
    local real a=GetRandomReal(0,2)*3.14159
    set d=GetRandomReal(0,d)
    return Location(x+d*Cos(a), y+d*Sin(a))
endfunction
 

Vexorian

Why no custom sig?
Reaction score
187
When you use polar projections the random distribution is not uniform... (try it, and see how it is more likely to pick points in the center)

JASS:
function RandomPointCircle takes real x, real y, real d returns location
    local real cx = GetRandomReal(-d,d)
    local real ty = SquareRoot(d*d-cx*cx)
    return Location(x+cx, y+ GetRandomReal(-ty,ty) )
endfunction

And one squareroot is arguably faster than a sin+cos .

Edit: fixed a mistake
 

Kenoriga

Ultra Cool Member
Reaction score
34
I think someone has to illustrate what each of the variables mean now, getting pretty confused here. Not that I really understood the first few sin and cos actually.

So... what is the SquareRoot used for in Vexorian's function?
 

Doomhammer

Bob Kotick - Gamers' corporate spoilsport No. 1
Reaction score
67
ok, let#s get to the basics:

to describe a circle with 2 variables x and y, you have two options:

1) have your circle described with trigonometric functions:
needed: radius, (angle in degrees from 0 to 360 or in radians from 0 to 2 pi )
then your circle can be described as:
x coordinate: x = cos ( angle )
y coordinate: y = sin ( angle )
That's the variation so far.

2) have your circle described with basic geometrics: needed: radius;
in rectangular triangles you can always follow the Pythagorean rule [cathetus ] a² + [cathetus ] b² = [hypotenuse] c². Going from there on you can deduct (or check out the circle of Thales for further reference and mind-connection between rectangles and circles), that a circle in coordinates can also be described as
x² + y² = r². The problem we get here is that for each of the variables we have 2 solutions: |x|=Sqr(r²-y²), and |y|=Sqr(r²-x²), so it's x and -x, and y and -y.
This fact is what Vexorian made use of to get random coordinates within a circle: see how he extented the randomizer into the negative. The advantage of his function is that it comes along without the use of trigonometric function calls, namely sin and cos, which are said to be rather slow (internal algorithms instead of direct calculation). With "it is said" I mean that the guys at wc3 have made some tests quite some time ago, and the trigonometric functions are indeed a bit slower than let's say the square-root call, but it's not dramatic after all. So that's basically it. Hope that helps.

links:
http://en.wikipedia.org/wiki/Trigonometry
 

Kenoriga

Ultra Cool Member
Reaction score
34
^ I knew the Pythagoras stuff and the trigonometry basics just in school in this semester... and that you almost enlightened me with the circle in coordinates stuff, but I just can't visualise how Pythagoras' Theorem works about with Vex's variables, cx and d.
 

Chocobo

White-Flower
Reaction score
409
^ I knew the Pythagoras stuff and the trigonometry basics just in school in this semester... and that you almost enlightened me with the circle in coordinates stuff, but I just can't visualise how Pythagoras' Theorem works about with Vex's variables, cx and d.

cx is random distance.
ty is the square root of d² - cx² (max distance and random distance : SquareRoot((d-cx)(d+cx)) if you think properly).
cx is added to x for the real x, y is added to a random number between -ty and ty.
 

Kenoriga

Ultra Cool Member
Reaction score
34
I still don't understand... I hope someone is willing to draw a diagram on what is going on about the variables in Vex's function, as I can't see how cx and d makes up a right-angled triangle.

Plus, I don't think Doomhammer's function works, because my random points end up like a thick circumference in a circle, with "varying radii"(points don't fit on the circumference exactly") and the top left corner of the map the middle of the circle. Either that, or the distances for random points are too great, I used 200.00 for real d, and it can go far above 1000 range from the original point.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Red: cx
Green: ty
Blue: d
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    1.7 KB · Views: 303

Kenoriga

Ultra Cool Member
Reaction score
34
Clears up everything! Therefore d is somewhat like a constant, and -d < cx < d... Then cx and ty are added, in a way that they will never go out of the circle...

Omg what a dumbass am I...! :banghead::banghead::banghead:
 
Reaction score
333
Vexorian's function is very clever, and I am no maths whiz, but it seems to me that the distribution of points would not be uniform or even rotationally symmetrical. Is this the case?
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top