Circle maths, i guess

Discussions about Coding and Scripting

Circle maths, i guess

Postby >@tack!< » Thu Aug 18, 2011 11:55 pm

How to get a random location within a circle? i tried some stuff but they werent perfectly distrubuted in the circle ( particles ) like i once had shown in a video.
User avatar
>@tack!<
Adept
 
Posts: 330
Joined: Sat Apr 17, 2010 4:51 pm
Personal rank: lol?

Re: Circle maths, i guess

Postby JackGriffin » Fri Aug 19, 2011 12:46 am

q = Rnd() * (PI * 2)
r = Sqrt(Rnd())
x = (radius * r) * Cos(q)
y = (radius * r) * Sin(q)
/away for now.
JackGriffin
Godlike
 
Posts: 1836
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: Retired as of Sep-11

Re: Circle maths, i guess

Postby anth » Fri Aug 19, 2011 12:50 am

Isn't this basic maths? :o Either way, if the middle of the circle is at (x=0, y=0) and the circle has a radius of r then this would give you a random location within the circle:

Code: Select all
function GetRandomLocation(float radius, out float x_loc, out float y_loc)
{
    local float angle, distance;

    angle = RandRange(0.0, 2 * PI);
    distance = RandRange(0.0, radius);

    x_loc = Cos(angle) * distance;
    y_loc = Sin(angle) * distance;
}
Last edited by anth on Fri Aug 19, 2011 1:08 pm, edited 1 time in total.
anth
Experienced
 
Posts: 110
Joined: Thu May 13, 2010 2:23 am

Re: Circle maths, i guess

Postby >@tack!< » Fri Aug 19, 2011 7:23 am

Yes i tried this one too and its maybe perfect enough BUT think like this: if radius is VERY LONG and say the difference between 2 angles is minimum 0.000001 rad. Then at large radii you'll get empty spaces between 2 angles or am I wrong? But I may have an idea that works if not I'll just use this because i won't use large radii.

Edit: sec I might misread your posts ill read it on my laptop when I wake up lol xd
Edit2: hm no i read it the way i meant to. however i dont get JackGriffin's r = Sqrt(Rnd()) part.
Edit3: using r = Sqrt(Rnd()) made a better circle than without. Can i maybe have an explanation why r = Sqrt(Rnd()) is needed?
Edit4: nvm i get it :), secret edit 5: no i dont :(
User avatar
>@tack!<
Adept
 
Posts: 330
Joined: Sat Apr 17, 2010 4:51 pm
Personal rank: lol?

Re: Circle maths, i guess

Postby JackGriffin » Fri Aug 19, 2011 12:52 pm

It's there to add uniformity to the frequency of points. The disparity between q and r is high at a low Rnd() value but approaches equality as you progress towards Rnd() being 1 (the center). It's actually hard to not get results that congregate more to the center, isn't it?

Thank Ferali, he hit me with "Oh you need a perihelical check" once in replying to a question I had. I ended up having to order a book from Amazon to understand what the hell he meant.
/away for now.
JackGriffin
Godlike
 
Posts: 1836
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: Retired as of Sep-11

Re: Circle maths, i guess

Postby >@tack!< » Fri Aug 19, 2011 1:10 pm

ah yes i get it thanks and ferali too :p
User avatar
>@tack!<
Adept
 
Posts: 330
Joined: Sat Apr 17, 2010 4:51 pm
Personal rank: lol?

Re: Circle maths, i guess

Postby Feralidragon » Fri Aug 19, 2011 5:57 pm

Trigonometry... I advice you to study it, the basics aren't that difficult at all, and once you domain at least the basics, this and other math problems in mod making become quite easy to solve with simple plain math, as long they have angles and vectors into the mix.
User avatar
Feralidragon
Site Staff
 
Posts: 3801
Joined: Wed Feb 27, 2008 6:24 pm
Location: Portugal - Lisbon
Personal rank: The Unreal Boy


Return to Coding, Scripting

Who is online

Users browsing this forum: No registered users and 0 guests