Skip to main content
gamedev.net
🔒 Locked

1d -> 2d (array)

Started by logout Jan 2, 2003 at 3:58 PM 1 replies 550+ views
Original Post
logout
logout
Lets say that i got a array like :
int array[WIDTH][HEIGHT];
Index = 14:
 
how can i use the WIDTH, HEIGHT and Index to figure out the x and the y. /* This is the right way to put the problem, my poroblem aint like that but its the formula that i need: like 2d->1d = (y * WIDTH) + x; i need to reverse this prosses. */
Monder
Monder
y = index/width;
x = index%width;

In c/c++(which I assume you''re using) doing an integer devision like that just returns an integer. So if your width is 10 and the index is 14 it''ll give you 1. The % operator is called the modulous operator it returns the remainder of the devision. So in the above case width is 10 index is 14 14/10 = 1 remainder 4 so the 4 will go into x

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.