28.11.2013, 00:28
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?
Example:
Returns:
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);
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