Results 1 to 3 of 3

Thread: Converting 2d array coordinates to 1d array

  1. #1

    Converting 2d array coordinates to 1d array

    I'm working on a tile-based game and having a lot of trouble figuring out how to work with the grid. For performance reasons I'm needing to use a single Vector.<T> to represent the 2d grid (Flash 10). But there's a catch -- I have a smaller grid within the main grid that I pull data from to affect the main grid with. Here's what I'm talking about:


    In order to get to the inner grid's next row in main grid coordinates, I have to increase the index cursor (current position in the list) by a specific amount (determined by the position and dimensions of the sub array).

    Basically, I'm running a loop through the main array and trying to catch the iteration when it runs into a cell "owned" by the sub-array in order (so the form of the sub-array appears as it would when sketched in a 2d grid, (eg perfectly square or rectangle).

    I need to find equations that can give me the main list index number based and the index offset from the end of the sub-array to the beginning of the sub-array in the next row. The only givens I have are the sub-array's position, dimensions and the main arrays dimensions.

    I think I've figured out where the main array index is supposed to start:
    Code:
    numMainArrayColumns * subArrayY - (numMainArrayColumns - subArrayX)
    Mad props for any help!
    cold constructs
    flash, js, ui r&d

  2. #2
    Not completely sure what your question actually is.. I'll provide you with a sample which I think might help.

    Note that its for FP9 (no vectors)


    public class Main
    {
    private const COLUMNS:int = 5;
    private const SUB_COLUMNS:int = 3;

    private const MODIFIER:int = 8;

    // Premade array
    private var grid:Array = [1, 1, 1, 1, 1,
    1, 0, 0, 0, 1,
    1, 0, 2, 0, 1,
    1, 0, 1, 0, 1,
    1, 1, 1, 1, 1
    ];

    public function Main()
    {
    // trace the full array
    traceSquare(0, 0, 5);

    // Create a square with (SUB_COLUMNS (3) in size) at position (2, 2) == index 12
    createInnerSquare(2, 2, SUB_COLUMNS);

    // trace the full array after modifying
    traceSquare(0, 0, 5);
    }

    private function createInnerSquare(x:int, y:int, cols:int):void
    {
    var startIndex:int = getIndex(x, y);
    for (var i:int = 0; i < cols; i++)
    {
    for (var j:int = 0; j < cols; j++)
    {
    /* This would be the formula you are looking for:
    ** startIndex + i + (COLUMNS * j)
    **********
    ** startIndex == the index where your inner square starts
    ** i == x, j == y, and COLUMNS is how many columns the MAIN array has.
    */
    grid[startIndex + i + (COLUMNS * j)] = MODIFIER;
    }
    }
    }

    /*
    * 2D to 1D formula
    */
    private function getIndex(x:int, y:int):int
    {
    return (y * COLUMNS) + x;
    }

    /*
    * Just for tracing, no real function otherwise
    */
    private function traceSquare(x:int, y:int, cols:int):void
    {
    var str:String;
    for (var i:int = 0; i < cols; i++)
    {
    str = "";
    for (var j:int = 0; j < cols; j++)
    {
    str += grid[getIndex(x + j, y + i)];
    }
    trace(str);
    }
    trace("--------");
    }

    }


    Of course I realise you dont wanna modify it like I did here, I just did it to prove it could access an inner array.

    Good luck!
    Last edited by Valaran; August 25th, 2009 at 04:14 AM.

  3. #3
    Code:
    private var rowLength:int
    function getTile1D(X:int, Y:int):int{ 
       var ArrayPosition:int = (Y*rowLength) + X 
       return(ArrayPosition)
    }
    function getTile2D(ArrayPosition:int):Array{
       var X:int = ArrayPosition % rowLength
       var Y:int = (ArrayPosition - X) / rowLength
       var ArrayCoordinate:Array = [X, Y]
       return(ArrayCoordinate)
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012