Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

N-Arrays & Bin Files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • N-Arrays & Bin Files

    Code:
    I use this method in a mapping project to manipulate m-dimensional
     numeric arrays and numeric binary files as if both are (m, n)-dimensional
     arrays. I also use a variation of this method to store and retrieve two
     positive integer values in a single integer.
    
     An (m, n)-array is simply a matrix with "m" rows down and "n" columns across.
     An (m)-array can be substituted by setting it the size of m * n.  Then
     specific values within the array can be set or retrieved by:
    
             Record number = Row * Array Width + Column
    
             Where:
                   Row        : is "down" in the M-dimension
                   Array Width: is the number of elements in N-dimension
                   Column:    : is "right" in the N-dimension
    
     If the array is written out to a binary file specific records within
     the file can be accessed by:
    
     Record number = Row * (Array Width + 1) * Size of Value + (Column * Size of Value)
    
             Where:
                   Size of Value: is the number of bytes required to store the value
                   1:             is an alignment into the binary file
    
     This method can be used with binary files written by both (m)- and (m, n)-dimensional
     arrays.  It can also be used to write to specific locations within the binary file.
    
     To store two values as a single value:
    
             Number = Value1 * Width + Value2
    
             Where:
                   Value1: is some number
                   Width:  is some known width
                   Value2: is some number
    
                   Width can be:
                                number of bytes
                                number of words
                                number of columns
                                width of a world
                                some arbitrary number
    
     To retrieve the two values stored as a single value:
    
                   Value1 = Number \ Width
                   Value2 = Number MOD Width
    
    
    
     If you have questions, send me a personal message or start a thread in the support
     forum refering to this topic.

    Code:
    'Date     : 4-24-2009
    'Author   : Walt Decker
    'Subject  : Single Dimension Numeric Arrays and Numeric Binary Files
    'Topic    : N-Arrays & Bin Files
    
    '=====================================================================
    
    'Text
    
    ' Released to Public Domain by the author.
    
    ' I use this method in a mapping project to manipulate m-dimensional
    ' numeric arrays and numeric binary files as if both are (m, n)-dimensional
    ' arrays. I also use a variation of this method to store and retrieve two
    ' positive integer values in a single integer.
    
    ' An (m, n)-array is simply a matrix with "m" rows down and "n" columns across.
    ' An (m)-array can be substituted by setting it the size of m * n.  Then
    ' specific values within the array can be set or retrieved by:
    
    '         Record number = Row * Array Width + Column
    
    '         Where:
    '               Row        : is "down" in the M-dimension
    '               Array Width: is the number of elements in N-dimension
    '               Column:    : is "right" in the N-dimension
    
    ' If the array is written out to a binary file specific records within
    ' the file can be accessed by:
    
    ' Record number = Row * (Array Width + 1) * Size of Value + (Column * Size of Value)
    
    '         Where:
    '               Size of Value: is the number of bytes required to store the value
    '               1:             is an alignment into the binary file
    
    ' This method can be used with binary files written by both (m)- and (m, n)-dimensional
    ' arrays.  It can also be used to write to specific locations within the binary file.
    
    ' To store two values as a single value:
    
    '         Number = Value1 * Width + Value2
    
    '         Where:
    '               Value1: is some number
    '               Width:  is some known width
    '               Value2: is some number
    '
    '               Width can be:
    '                            number of bytes
    '                            number of words
    '                            number of columns
    '                            width of a world
    '                            some arbitrary number
    
    ' To retrieve the two values stored as a single value:
    
    '               Value1 = Number \ Width
    '               Value2 = Number MOD Width
    
    '=================================================================================
    
    ' If you have questions, send me a personal message or start a thread in the support
    ' forum refering to this topic.
    
    '=================================================================================
    
    #COMPILE EXE
    #BREAK ON
    DEFLNG A - Z
    
    FUNCTION PBMAIN () AS LONG
    
    LOCAL Rad AS SINGLE
    
    CONSOLE SET SCREEN 10, 20
    CONSOLE SET LOC 200, 300
    
    BmpWide = 72
    BmpHigh = 72
    
    WinWide = BmpWide           ' width of window
    WinHigh = BmpHigh           ' height of window
    
    AryWide = BmpWide - 1       ' n-elements in (m, n)-array
    AryHigh = BmpHigh - 1       ' m-elements in (m, n)-array
    
    Rad! = (4 * ATN(1)) / 180!  ' value of radians for trig functions
    Dist = (AryWide \ 2) - 10   ' distance
    Cx = AryWide \ 2            ' column center
    Cy = AryHigh \ 2            ' row center
    
    DIM Map(AryWide * AryHigh)
    MAT Map() = CON(%BLUE)      ' make all values blue
    
    '====================================================
    '  DRAW A CIRCLE IN THE SINGLE DIMENSIONAL ARRAY.
    '  THIS DATA COULD ALSO BE WRITTEN DIRECTLY TO A
    '  BINARY FILE
    '=====================================================
    
    FOR I = 0 TO 360
      X = Cx + Dist * SIN(I * Rad!)
      Y = Cy - Dist * COS(I * Rad!)
      Rec = Y * AryWide + X            'first equation above
      Map(Rec) = %WHITE
    NEXT I
    
    GRAPHIC WINDOW "test", 200, 100, BmpWide, BmpHigh TO WinHndl
    GRAPHIC ATTACH WinHndl, 0, REDRAW
    OPEN "FileTest.Tst" FOR BINARY AS #1 BASE = 0
    
    X = 0
    Y = 0
    
    '=====================================================
    '  DRAW THE CIRCLE IN THE WINDOW AND STORE THE DATA
    '  IN A BINARY FILE
    '=====================================================
    
    FOR I = 0 TO AryHigh
      Row = I * AryWide                      'first equation
      FOR J = 0 TO AryWide
        Rec = Row + J                        'first equation
        GRAPHIC SET PIXEL (X, Y), Map(Rec)
        PUT #1, , Map(Rec)                   'sequential write
        INCR X
      NEXT J
      INCR Y
      X = 0
    NEXT I
    GRAPHIC REDRAW
    CONSOLE SET FOCUS
    
    PRINT "Press a key"
    
    WAITKEY$
    
    '======================================================
    '     RETRIEVE ONLY THE CIRCLE FROM THE BINARY FILE
    '======================================================
    
    FOR I = 0 TO 360
      X = Cx + Dist * SIN(I * Rad!)
      Y = Cy - Dist * COS(I * Rad!)
      Row = Y * (AryWide + 1) * 4               'second equation
      Rec = Row + (X * 4)                       'second equation
      GET #1, Rec, Map(0)
      GRAPHIC SET PIXEL (X, Y), Map(0) AND 255  'change display color
    NEXT I
    
    GRAPHIC REDRAW
    
    CLOSE #1
    KILL "FileTest.Tst"
    
    LOCATE 1, 1
    COLOR 4, 7
    PRINT "Press a key
    WAITKEY$
    
    GRAPHIC WINDOW END
    
    END FUNCTION
    Walt Decker
Working...
X