View Single Post
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-16-2020 , 02:04   Re: Which method is more efficient?
Reply With Quote #5

Quote:
Originally Posted by Natsheh View Post
PHP Code:
random_coord_within_region(Float:xFloat:yradius=5.0Float:random_coord[2])
{
   
random_coord[0] = random_float(-radiusradius) + x;
   
random_coord[1] = random_float(-radiusradius) + y;

This is generating points inside the rectangle R(x - radius, x + radius, y - radius, y + radius), not in a circle.




Quote:
Originally Posted by Black Rose View Post
Code:
stock random_coord1(coord[2], range) {     new newCoord[3], tempCoord[3];     tempCoord[0] = coord[0];     tempCoord[1] = coord[1];     do {         newCoord[0] = tempCoord[0] + random_num(-range, range);         newCoord[1] = tempCoord[1] + random_num(-range, range);     }     while ( get_distance(newCoord, tempCoord) > range ) }

I don't understand the second code so I'm not sure if it's correct. That itself should be a reason to chose the first code. But the first code is faster as well.

Spoiler
The second code is actually very clever. To define a circle we need the center(which is the variable Origin) and a radius. We end up with circle C(Origin[0], Origin[1], r). Then by varying the radius, we can generate points inside the main circle, so ultimately we are generating points over the surface of the disk.
The only problem is these points will be specified in polar coordinates, we need to convert into cartesian coordinates which is what he is doing here(in order to have them fully defined in polar coordinates we need to pick a random theta angle in [0, 2pi))
PHP Code:
rand_coord[0] = r*floatcos(thetaradian)
rand_coord[1] = r*floatsin(thetaradian
PHP Code:
cos(theta)
sin(theta
This code is guaranteed to work and produce good results so I'd go with it. It is slower because of the usage of sin and cos functions, but if you REALLY need the last bit of speed you can build a table and cache the values of sin & cos for angles theta in [0, 2pi)

The first code appears to be faster in that particular test, but it is non deterministic. Technically, it may never converge to a point inside the circle.
__________________

Last edited by HamletEagle; 10-16-2020 at 03:06.
HamletEagle is offline