B4R Code Snippet [B4R] Simulate a TWO dimension array using a ONE dimension Array

wonder

Expert
Licensed User
Longtime User
This is part of my game code, I hope it's useful. :)

B4X:
'Gets the (2D) virtual X-coordinate from a single dimension array, given the index and desired row size (max x-axis value)
Sub getArray2D_X(Index As Int, RowSize As Int) As Int
    Return index Mod rowSize
End Sub

'Gets the (2D) virtual Y-coordinate from a single dimension array, given the index and desired row size (max x-axis value)
Sub getArray2D_Y(Index As Int, RowSize As Int) As Int
    Return Index / RowSize
End Sub

'Gets the Index based of the array's virtual (2D) X, Y and row size (max x-axis value)
Sub getArray2D_Index(X As Int, Y As Int, RowSize As Int) As Int
    Return (Y * RowSize) + X
End Sub
 
Top