This will be a *lot* easier if you start the numbering from 0.
i.e. 0 to 15 rather than 1 to 16, 0 to 255 rather than 1 to 256 etc.
And the variables should be integers, not floating point.
Here's how to get the patch, and the vertex within the patch.
The ideas behind this should be clear if you experiment with 10 by 10, rather than 16 by 16.
You can then see when you want to use the final digit of x,y and when
you should use the digits before the last one.
The tricky looking code does the same thing, but with base sixteen digits rather than base ten.
i.e. 0 to 15 rather than 1 to 16, 0 to 255 rather than 1 to 256 etc.
And the variables should be integers, not floating point.
Here's how to get the patch, and the vertex within the patch.
patch = 16*(y/16) + (x/16) vert = 16*(y Mod 16) + (x Mod 16) ; Fast versions using bitwise operators... patch = (y And -16) Or (x Shr 4) vert = ( (y And %1111) Shl 4 ) Or (x And %1111)
The ideas behind this should be clear if you experiment with 10 by 10, rather than 16 by 16.
You can then see when you want to use the final digit of x,y and when
you should use the digits before the last one.
The tricky looking code does the same thing, but with base sixteen digits rather than base ten.