At 6:33 PM +1100 2/28/01, Luke Wigley wrote:
>Hi Listers,
>
>I've got some 2D arrays (list of lists), and I need to figure out the
>neighbours of any one cell in the array. For example, imagine a list like
>this:
>
>[
>  [ Q W E R T Y ],\
>  [ A S D F G H ],\
>  [ Z X C V B Z ],\
>]
>
>
>In the past, to find out the neighbours of cell in column 3, row 2 (ie the
>"D"), I'd do something like this:
>

Luke,

Here's a programmatic way of solving this without the need for padding:

on getNeighbours (array, row, col)
   nRows = count(array)
   nCols = count(array[1])

   neighbourList = []
   repeat with rowOffset = -1 to 1
     repeat with colOffset = -1 to 1
       rowIndex = row + rowOffset
       if (rowIndex >= 1) and (rowIndex <= nRows) then  -- check bounds
         colIndex = col + colOffset
         if (colIndex >= 1) and (colIndex <= nCols) then  -- check bounds
           if not((rowIndex = row) and (colIndex = col)) then  -- 
don't include itself
             neighbourList.append(array[rowIndex] [colIndex])
           end if
         end if
       end if
     end repeat
   end repeat
   return neighbourList
end

Irv
-- 
Lingo / Director / Shockwave development for all occasions.

        (Over two millions lines of Lingo code served!)

[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi  To post messages to the list,
email [EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo.  Thanks!]

Reply via email to