# Array index from X/Y pos on a grid.

## Recommended Posts

I got an array which holds nodes on a grid. To find out where in the array I want to put each node, so I easily can find them again by giving X/Y, I do this:

index = (y * totalSize + x);

This has been working nice so far, but I'm at a complete loss on how to reverse this calculation if I got the node index and want to know what position that would be on the grid. Any hints for a tired brain?

##### Share on other sites
I think this should work for you:

x = index % totalsize;
y = index / totalsize;

##### Share on other sites
Awesome! Made some tests and it works just like you said.

Thanks.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628354
• Total Posts
2982236

• 10
• 9
• 11
• 24
• 11