Friday, July 18, 2014

Working with Grids

While working on a course of Algorithms and data structures I had the need to represent a grid of x/y in an array. After a bit of thought I realized that for any grid of size N, you can represent the grid as an N*N length array. To convert an array element into X/Y coordinates, you do the following:

x = p/N
y = p%n

To get the array position p from an x/y coordinate:

p = N*x+y


No comments: