wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Warcraft III Modding > Developer's Corner > Triggers & Scripts
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 07-25-2009, 03:09 PM   #1
ploks
User
 
Join Date: Jun 2007
Posts: 99

ploks has little to show at this moment (6)

Default GetRandomPointInCircle

I'm tryin to get a random point in a circle which is defined by the centre coordinates (or location) and the radius. First I thought of taking a random angle and then randomize a point on the line that is created between the centre and the edge of the circle. However, that way there are more results near the centre.


The second approach was to get a randomized point in a square and then check whether the distance between the point and the centre is lower than the radius.


Expand GetRandomPointInCircle:

However this function returns a location (and takes one) and I have learned that locs are evil. Is this a nice way? how can I optimize it? And, does it even work?
ploks is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 07-25-2009, 03:14 PM   #2
Kwah
Christmassy.
 
Kwah's Avatar
 
Join Date: Jun 2008
Posts: 644

Submissions (4)

Kwah will become famous soon enough (43)Kwah will become famous soon enough (43)

Default

Collapse Something like this:
function GetRandomPointInCircle takes real X, real Y, real radius returns location
    local real x2 = X 
    local real y2 = Y
    local real x1 = GetRandomReal(x2-radius,x2+radius)
    local real y1 = GetRandomReal(y2-radius,y2+radius)
    local real dx = x2 - y2
    local real dy = y2 - y1
    local location loc
        loop 
        exitwhen (dx*dx + dy*dy) <= radius*radius 
            if (dx*dx + dy*dy) > radius*radius then
                set dx = x2 - y2
                set dy = y2 - y1
            endif
        endloop 
    set loc = Location(dx,dy) 
    return loc
endfunction

Last edited by Kwah : 07-25-2009 at 03:20 PM.
Kwah is offline   Reply With Quote
Old 07-25-2009, 03:17 PM   #3
ploks
User
 
Join Date: Jun 2007
Posts: 99

ploks has little to show at this moment (6)

Default

How would I return 2 values? A struct, seems a bit too much, or 2 functions one for the X value and one for the Y?

And about the location cleaning, doesn't my way works with a global loc that is moved between different points?
ploks is offline   Reply With Quote
Old 07-25-2009, 03:21 PM   #4
Kwah
Christmassy.
 
Kwah's Avatar
 
Join Date: Jun 2008
Posts: 644

Submissions (4)

Kwah will become famous soon enough (43)Kwah will become famous soon enough (43)

Default

Updated the code.

Leave cleaning the returned location in the hands of the user.
Kwah is offline   Reply With Quote
Old 07-25-2009, 03:26 PM   #5
ploks
User
 
Join Date: Jun 2007
Posts: 99

ploks has little to show at this moment (6)

Default

Quote:
Originally Posted by Kwah
Updated the code.

Leave cleaning the returned location in the hands of the user.

Seems correct! Thanks. Maybe there should be a system for moving locations around. (if that's more efficient than creating new all the time) kind of GroupUtils for locations and rects.
ploks is offline   Reply With Quote
Old 07-25-2009, 03:45 PM   #6
chobibo
100% Genuine Retard!
 
chobibo's Avatar
 
Join Date: Jan 2007
Posts: 797

chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)

Send a message via Yahoo to chobibo
Default

Collapse JASS:
function GetCircleRandomX takes real center_x, real radius returns real
    return GetRandomReal(center_x, radius)*Cos(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

function GetCircleRandomY takes real center_y, real radius returns real
    return GetRandomReal(center_y, radius)*Sin(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

Can't you use those?
__________________
Where's mah sig?
chobibo is offline   Reply With Quote
Old 07-25-2009, 04:30 PM   #7
ploks
User
 
Join Date: Jun 2007
Posts: 99

ploks has little to show at this moment (6)

Default

Wont the result of those be more centred to the centre of the circle, or do I fail to understand the functions? (My knowledge in cos and sin is limited at the best)
ploks is offline   Reply With Quote
Old 07-25-2009, 04:31 PM   #8
Wizardum
User
 
Wizardum's Avatar
 
Join Date: Jan 2008
Posts: 59

Wizardum has little to show at this moment (4)

Send a message via MSN to Wizardum
Default

Quote:
Originally Posted by chobibo
Collapse JASS:
function GetCircleRandomX takes real center_x, real radius returns real
    return GetRandomReal(center_x, radius)*Cos(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

function GetCircleRandomY takes real center_y, real radius returns real
    return GetRandomReal(center_y, radius)*Sin(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

Can't you use those?

does this actually work? your using random angles for each of the coordinates, I guess x and y should have the same angle, am I wrong?
Wizardum is offline   Reply With Quote
Old 07-25-2009, 05:25 PM   #9
Vexorian
Free Software Terrorist
 
Vexorian's Avatar


Technical Director
 
Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)

Hero Contest #3 - 2nd Place

Default

Quote:
Originally Posted by chobibo
Collapse JASS:
function GetCircleRandomX takes real center_x, real radius returns real
    return GetRandomReal(center_x, radius)*Cos(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

function GetCircleRandomY takes real center_y, real radius returns real
    return GetRandomReal(center_y, radius)*Sin(GetRandomReal(0,360)*bj_DEGTORAD)
endfunction

Can't you use those?
Those got a wrong random distribution.

Try creating a lot of points using those functions, you'll see a bias towards the center.

http://mathworld.wolfram.com/DiskPointPicking.html
__________________
Zoom (requires log in)Wc3 map optimizer 5.0
Someone should fix .wav sound in this thing.
Zoom (requires log in)JassHelper 0.A.2.A
Turns your simple code into something that is complicated enough to work.
Faster != more useful
Vexorian is offline   Reply With Quote
Old 07-25-2009, 05:26 PM   #10
chobibo
100% Genuine Retard!
 
chobibo's Avatar
 
Join Date: Jan 2007
Posts: 797

chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)

Send a message via Yahoo to chobibo
Default

Quote:
does this actually work? your using random angles for each of the coordinates, I guess x and y should have the same angle, am I wrong?

Oh sorry about that, thanks for the correction.

EDIT: Deleted misleading part. Vexorian thanks for telling me lol.
__________________
Where's mah sig?

Last edited by chobibo : 07-25-2009 at 05:50 PM.
chobibo is offline   Reply With Quote
Old 07-25-2009, 05:36 PM   #11
Vexorian
Free Software Terrorist
 
Vexorian's Avatar


Technical Director
 
Join Date: Apr 2003
Posts: 14,898

Submissions (37)

Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)Vexorian has a reputation beyond repute (1062)

Hero Contest #3 - 2nd Place

Default

Except now your function is not random at all?
__________________
Zoom (requires log in)Wc3 map optimizer 5.0
Someone should fix .wav sound in this thing.
Zoom (requires log in)JassHelper 0.A.2.A
Turns your simple code into something that is complicated enough to work.
Faster != more useful
Vexorian is offline   Reply With Quote
Old 07-25-2009, 05:45 PM   #12
chobibo
100% Genuine Retard!
 
chobibo's Avatar
 
Join Date: Jan 2007
Posts: 797

chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)

Send a message via Yahoo to chobibo
Default

Darn... lol. I think it doesn't even need a function now...
__________________
Where's mah sig?
chobibo is offline   Reply With Quote
Old 07-25-2009, 06:07 PM   #13
ploks
User
 
Join Date: Jun 2007
Posts: 99

ploks has little to show at this moment (6)

Default

Quote:
Originally Posted by chobibo
Darn... lol. I think it doesn't even need a function now...

I would say that the question is that: Can it be made in a function?


I fail to see how, especially efficient. (The loop might repeat itself, forever and ever...)
ploks is offline   Reply With Quote
Old 07-25-2009, 06:25 PM   #14
chobibo
100% Genuine Retard!
 
chobibo's Avatar
 
Join Date: Jan 2007
Posts: 797

chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)chobibo has a spectacular aura about (101)

Send a message via Yahoo to chobibo
Default

Yes, with locations, a struct or a parallel array, what do you want from those three?
__________________
Where's mah sig?
chobibo is offline   Reply With Quote
Old 07-25-2009, 06:30 PM   #15
ploks
User
 
Join Date: Jun 2007
Posts: 99

ploks has little to show at this moment (6)

Default

Quote:
Originally Posted by chobibo
Yes, with locations, a struct or a parallel array, what do you want from those three?

I saw somewhere else that locations where still faster than a struct and I think a parallel array. So I guess I keep locations. Will figure something out.


Or can't you have a long incredibly complicated functions that put all (or some) of the coordinates that exist in a circle in a array. After that it will randomize an index and take those coordinates. Get my idea? Or maybe it's just stupid. I just feel unsafe with the possibility of so many runs in a loop for just a random point...
ploks is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 12:34 PM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services