Zone & random positions
#1

Hello
I'm trying to make a code that creates random positions within a zone, but I failed .. I have basically created a code that takes a random position in the line of perimeter i dont want that

How i can improve?

pawn Код:
stock CreateRandomPosInZone(Float:MinX, Float:MinY, Float:MaxX, Float:MaxY)
{
    new Float:Width = floatabs(MaxX - MinX);
    new Float:Height = floatabs(MaxY - MinY);
    new Float:Perimeter = (Width * 2) + (Height * 2);
    for(new i = 0; i != MAX_IT; i++) {
        new Float:C[2],Float:dist = modulus(random(9000), Perimeter);
        if(dist <= Width) {
            C[0] = modulus(random(9000), Width) + MinX;
            C[1] = MinY;
        } else if(dist <= Width + Height) {
            C[0] = MaxX;
            C[1] = modulus(random(9000), Height) + MinY;
        } else if(dist <= (Width * 2) + Height) {
            C[0] = modulus(random(9000), Width) + MinX;
            C[1] = MaxY;
        } else {
            C[0] = MinX;
            C[1] = modulus(random(9000), Height) + MinY;
        }
        printf("%f : %f",C[0],C[1]);
    }
}

Example:

pawn Код:
CreateRandomPosInZone(625.6991, -1895.0369, 631.7022, -1888.4351);
Returns:

pawn Код:
631.702209 : -1888.586669
625.699096 : -1894.307861
631.702209 : -1888.560058
631.702209 : -1888.691406
628.663146 : -1895.036865
625.699096 : -1891.416259
625.699096 : -1894.141601
631.702209 : -1891.375732
631.702209 : -1889.986083
625.833801 : -1895.036865
631.702209 : -1892.162597
629.665283 : -1895.036865
625.699096 : -1892.171630
625.699096 : -1890.136230
629.397460 : -1888.435058
625.699096 : -1891.672607
625.699096 : -1889.453857
631.702209 : -1890.258544
631.702209 : -1892.749755
628.841857 : -1895.036865
631.702209 : -1889.935302
631.407226 : -1895.036865
631.270507 : -1888.435058
628.346740 : -1888.435058
627.863342 : -1895.036865
629.021545 : -1895.036865
628.260803 : -1888.435058
625.699096 : -1890.055419
631.702209 : -1894.930664
631.702209 : -1893.303710
Reply
#2

I'm not really sure if this is what you want - it keeps the values between max and min but you don't know the height:

pawn Код:
forward Float: FRandomEx( Float: min, Float: max );

public OnFilterScriptInit( )
{
    new
        Float: rX = FRandomEx( 625.6991, 631.7022 ), // minX, maxX
        Float: rY = FRandomEx( -1895.0369, -1888.4351 ) // minY, maxY
    ;
   
    printf( "%f | %f", rX, rY );
    return 1;
}

stock Float: FRandomEx( Float: min, Float: max ) // The original RandomEx by ******, edited for the floats.
{
    return random( floatround( max ) - floatround( min ) ) + min;
}
Reply
#3

Ow that is more simple ! thanks i will try.. no problem with the height im gonna use MapAndreas
Reply
#4

Quote:
Originally Posted by iJumbo
Посмотреть сообщение
Ow that is more simple ! thanks i will try.. no problem with the height im gonna use MapAndreas
I hope it works. By the way, change to:
pawn Код:
return random( floatround( max ) - floatround( min ) ) + floatround( min );
It seems to be better.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)