Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug when redimensioning multi-dimension arrays?
#32
(06-20-2024, 09:52 PM)12centuries Wrote: WOW! I walked away for a bit and found a flurry of solutions.

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

i recommend you put the x in first position and y in second, (x, y) all graphics commands work that way.
the only thing that doesn't is locate row, column which is why i am in habit or saying row, column for locate.
easy to remember alphabetically x comes before y.
b = b + ...
Reply


Messages In This Thread
RE: Bug when redimensioning multi-dimension arrays? - by bplus - 06-21-2024, 12:43 AM



Users browsing this thread: 12 Guest(s)