Weird little math problem

Blitz3D Forums/Blitz3D Programming/Weird little math problem

Craig H. Nisbet(Posted 2004) [#1]
Hey guys, I have a math problem I'm trying to work out. Here's how it goes. I have a 2d array I would like to inclose in a type. Types only seem to handle 1d arrays. Anyway, I figure with a few tricky little functions, that I could use the one interger index in that 1d array to extrapolate back out to a 2 dementional array. For axample:

Array(30,30);this would be the original array that I want to simulate with a 1d Array in a type.

So an array index in the 1d array at index(50), would represtent the x and y Indexes(20,2), at least I think. I'm not quite sure how to acomplish this. Any ideas?


Warren(Posted 2004) [#2]
If it's a 2D array, you must be accessing it with (for example) X and Y values. So to compute the 1D index it's just:

((Y * 30) + X)

That should do 'er.


Perturbatio(Posted 2004) [#3]
Something like this?

Const ArraySize = 30

Dim testArray(ArraySize*ArraySize)


SetArray(2,2,5)
SetArray(3,3,10)

;MAIN LOOP
While Not KeyDown(1)
	Print GetArray(2,2)
	Print GetArray(3,3)
	WaitKey()
	Wend
End
;END MAIN LOOP


Function SetArray(i1,i2, value)
	testArray( (i1 * ArraySize) +i2) = Value
End Function


Function GetArray(i1,i2)
	Return testArray( (i1 * ArraySize) +i2)
End Function


*EDIT*
Damn, beaten to the post.


Craig H. Nisbet(Posted 2004) [#4]
Wow, that definately goes to show that I shouldn't code while I'm drunk. It's so obvious, I can't help but feel stupid. Thanks man!


kalimon(Posted 2004) [#5]
yeah thats linear bank. Useful in sooo many places.... specially when packing up comm signals.