open.mp forum
[Pawn] Rounding float to make it divisible by specified value. - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Rounding float to make it divisible by specified value. (/showthread.php?tid=210)



Rounding float to make it divisible by specified value. - Xray - 2019-04-14

Hi, I am developing a building system based on?a map grid. I?figured to use this grid (only xy axis based) I would only need to use a simple algorithm where I devide the current player position by the grid size.

Lets say I want to get the nearest grid in range of?a player, I need to round the player's position till it's devisible by the grid size to get the X Y position from that grid.



Could anyone explain me how to create a function that rounds?the player position to the nearest value where it is devisible by a specified value?


RE: Rounding float to make it divisible by specified value. - Autorojo - 2019-04-14

Have you tried floatround?



http://wiki.sa-mp.com/wiki/floatround


RE: Rounding float to make it divisible by specified value. - Xray - 2019-04-14

(2019-04-14, 10:23 PM)Autorojo Wrote: Have you tried floatround?



http://wiki.sa-mp.com/wiki/floatround



Thanks for your reply, although, that's not what I need.

I need a function which?rounds?a float to an amount till it's devisible by a specified value.


RE: Rounding float to make it divisible by specified value. - blade - 2019-04-14

Try this



stock GetGridValue(grid_size, Float:value)

{

new ivalue = floatround(value);

while (ivalue % grid_size != 0) (ivalue < 0) ? (飼�):(--ivalue);

return ivalue;

}


RE: Rounding float to make it divisible by specified value. - Xray - 2019-04-15

Sorry, I'm trying to wrap my head around this whole grid thing, heck, I'm not even sure what I exactly 'need' to have this working.
I drew an image to show what I have in mind.
[Image: jEz0ppC.png]
With the player's position being:?-508.0857,-967.1429
I would need a function that rounds x to be divisible by 1.75 (Right?)
And another function that rounds y to be divisible by 1.606.
The outcome of the rounded x & y axis would be the center of the nearest grid, right?


RE: Rounding float to make it divisible by specified value. - denNorske - 2019-04-15

I suggest you use modulo for this.

Modulo operator will return 0 when it matches a value that is dividable with 1.75 (or 1.606);

https://en.wikipedia.org/wiki/Modulo_operation

In pawn it looks like this: https://forum.sa-mp.com/showthread.php?t=485069

[Image: l3vgZTB.png]


RE: Rounding float to make it divisible by specified value. - BigETI - 2019-04-15

grid position = X: floor(position x / grid size x); Y: floor(position y / grid size y)


RE: Rounding float to make it divisible by specified value. - Xray - 2019-04-15

(2019-04-15, 09:39 AM)BigETI Wrote: grid position = X: floor(position x / grid size x); Y: floor(position y / grid size y)



Had to reread it a few times haha, but?this is indeed what I need. Thanks!