06-20-2024, 09:52 PM
WOW! I walked away for a bit and found a flurry of solutions.
All while I was working on my own solution... heh.
All while I was working on my own solution... heh.
Code: (Select All)
SUB Resize2DArray (Array(), yLowerNew, yUpperNew, xLowerNew, xUpperNew)
'
' Redimension a two-dimensional array by altering the upper
' and/or lower bounds of either dimension.
'
' The new array's existing elements will be in the
' same (x,y) position as they were previously.
'
' If reducing an array dimension, DATA LOSS WILL OCCUR.
'
'
DIM y, x
DIM yLowerCurrent, yUpperCurrent, xLowerCurrent, xUpperCurrent
DIM yLower, xLower, yUpper, xUpper
' start by getting our current array boundaries
yLowerCurrent = LBOUND(Array, 1)
yUpperCurrent = UBOUND(Array, 1)
xLowerCurrent = LBOUND(Array, 2)
xUpperCurrent = UBOUND(Array, 2)
' do we actually require a resize?
IF yLowerNew = yLowerCurrent AND xLowerNew = xLowerCurrent AND yUpperNew = yUpperCurrent AND xUpperNew = xUpperCurrent THEN
EXIT SUB
END IF
' Find the smaller of the lower bounds and the larger of
' the upper bounds.
yLower = (yLowerNew + yLowerCurrent + ABS(yLowerNew - yLowerCurrent)) / 2
xLower = (xLowerNew + xLowerCurrent + ABS(xLowerNew - xLowerCurrent)) / 2
yUpper = (yUpperNew + yUpperCurrent - ABS(yUpperNew - yUpperCurrent)) / 2
xUpper = (xUpperNew + xUpperCurrent - ABS(xUpperNew - xUpperCurrent)) / 2
' create a temporary array with the updated dimensions
DIM Temp(yLowerNew TO yUpperNew, xLowerNew TO xUpperNew)
' Copy original array values to new array
FOR y = yLower TO yUpper
FOR x = xLower TO xUpper
Temp(y, x) = Array(y, x)
NEXT
NEXT
' redimension original array
REDIM Array(yLowerNew TO yUpperNew, xLowerNew TO xUpperNew)
' copy temporary values back to original array
FOR y = yLower TO yUpper
FOR x = xLower TO xUpper
Array(y, x) = Temp(y, x)
NEXT
NEXT
END SUB