[Gambas-user] "wrapping" function based on recent Mod thread

Jussi Lahtinen jussi.lahtinen at ...626...
Tue Oct 8 15:14:18 CEST 2013


http://stackoverflow.com/questions/707370/clean-efficient-algorithm-for-wrapping-integers-in-c
>
> It's also possible that I mistranslated the C code in the above link, as
> I don't know C.
>

Yes, because Gambas makes automatic conversions and thus the line where
division is, gives sometimes
different answers than in C. Example kX += range_size * 1.5 instead of 1
like in C.


int Wrap(int kX, int const kLowerBound, int const kUpperBound)
{
    int range_size = kUpperBound - kLowerBound + 1;

    if (kX < kLowerBound)
        kX += range_size * ((kLowerBound - kX) / range_size + 1);

    return kLowerBound + (kX - kLowerBound) % range_size;
}


-->

Private Function Wrap(iX As Integer, iLowerBound As Integer, iUpperBound As
Integer) As Integer

Dim iRange As Integer = iUpperBound - iLowerBound + 1

    If iX < iLowerBound Then
    iX += iRange * CInt((iLowerBound - iX) / iRange + 1)
    Endif

    Return iLowerBound + ((iX - iLowerBound) Mod iRange)

End


How is your game progressing?
Games like yours require a lot of work even with Gambas.
It's interesting to see what you will come up to.

Jussi



More information about the User mailing list