Simple Array indexing question.

Blitz3D Forums/Blitz3D Programming/Simple Array indexing question.

Craig H. Nisbet(Posted 2005) [#1]
If I wanted to index a two dimensional array using an a single integer(index) value instead of and X and Y value, what math would I have to do to derive the X, and Y, from the Index. Basically the same way that frames are reference from the loadanimimage command. The images are stacked in an array in the image file, but are refenced using a single frame number.


Shambler(Posted 2005) [#2]
I can't test this but I think it would be

Dim Myarray(50,50)

index=10

value=Myarray( (index – (index mod 50))/50 , index mod 50 )

print "value at index=10 is ";value


Stevie G(Posted 2005) [#3]
for Dim ( 50, 50 ) it should be ...

index = 10
value = MyArray( ( Index mod 51 ) + floor( index / 51 ))

quick demo ..

Graphics 640,480,16,1
Const Xcomp = 10
Const Ycomp = 5
Dim MyArray( Xcomp-1 , Ycomp-1 ) 

SetBuffer BackBuffer()

;populate it
For y = 0 To Ycomp - 1 
	For x = 0 To Xcomp - 1
		Index = x + y*Xcomp
		MyArray(  x, y)  = Rand(100)
		Color 255,255,255
		Text x*30,y*30, MyArray( x,y )
		Color 255,0,0
		Text x*30,y*30+10,Index
	Next
Next

Color 255,255,0
Index = 15
Text 0,230,"X = "+Getx(Index)
Text 0,240,"Y = "+Gety(Index)
Text 0,250,"Value @ Index "+Index+" : "+MyArray( Getx(Index), Gety(Index ) )

Flip
MouseWait

;=============================================
;=============================================
;=============================================

Function GetX( Index )
 
 Return ( Index Mod Xcomp )

End Function

;=============================================
;=============================================
;=============================================

Function GetY( Index )

 Return Floor( Index / Xcomp )

End Function