Calling all algorithm lovers!
July 15, 2009 3:36 PM
Subscribe
I need help solving an interesting algorithm using 'C'!
Say for example, I have a daisy chain laid out like this which snakes from 0 to 15:
15 14 13 12
08 09 10 11
07 06 05 04
00 01 02 03
I know the height and width ahead of time (4x4), but I want to know what index of the daisy chain corresponds to a certain (x,y) coordinate.
If I want the index of coordinate (1,2), I would get 9.
If I want the index of coordinate (3,1), I would get 4.
If I want the index of coordinate (3,3), I would get 12.
etc...
Using 'C' I envision it something like this:
#define WIDTH 4
#define HEIGHT 4
Index = CalculateIndex(WIDTH, HEIGHT, 1, 2);
//Index should be equal to 9.
What's inside CalculateIndex?
posted by niwnfyc to computers & internet (6 comments total)
2 users marked this as a favorite
int CalculateIndex(int w, int h, int x, int y) { if (y % 2 == 0) { return y * w + x; } else { return (y + 1) * w - x - 1; } }posted by Emanuel at 3:52 PM on July 15 [1 favorite]