Poll

Which do you use / prefer ?

map
  • [y]
4 (44.4%)
map[y]
2 (22.2%)
Other
1 (11.1%)
Could not care less
2 (22.2%)

Total Members Voted: 8

Voting closed: December 06, 2012, 09:45:26 PM

Author Topic: map[x][y] or map[y][x]  (Read 17416 times)

Z

  • Rogueliker
  • ***
  • Posts: 905
  • Karma: +0/-0
    • View Profile
    • Z's Roguelike Stuff
Re: map[x][y] or map[y][x]
« Reply #15 on: November 30, 2012, 04:17:10 PM »
As Jo said, [ x][y] is the standard in math, physics, and most importantly programming.

I agree that (x,y) is a standard when writing coordinates. But you do not usually index arrays in maths and physics. Well, you do, when you are using matrices. But note that matrix indices are usually written as aij where i is the row number (y) and j is the column number (x).

Also, most graphical formats I know (BMP, SDL, OpenGL textures, internal VGA memory...) list the pixels row by row (row 1, row 2, row 3, ...) which corresponds to [y][ x] indexing.

I have looked at some available roguelike sources, and some of them use [y][ x], some use [ x][y] (without any clear majority).

XLambda

  • Rogueliker
  • ***
  • Posts: 208
  • Karma: +0/-0
    • MSN Messenger - tau_iota@live.de
    • View Profile
    • The Weird Rogue
Re: map[x][y] or map[y][x]
« Reply #16 on: November 30, 2012, 07:19:42 PM »
Well, curses used y,x coordinates. And in linear algebra matrix elements are usually defined as aij denoting the element in the ith row and jth column. I think it's just a maths thing.
« Last Edit: November 30, 2012, 07:21:45 PM by XLambda »

guest509

  • Guest
Re: map[x][y] or map[y][x]
« Reply #17 on: December 01, 2012, 06:29:44 AM »
  I just asked my niece what comes first, 'x' or 'y'. After successfully completing the ABC song (many times) it was clear that 'x' does in fact come before 'y'.

  I'd consider that to be pretty much the final word on the subject.

requerent

  • Rogueliker
  • ***
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: map[x][y] or map[y][x]
« Reply #18 on: December 01, 2012, 07:29:26 AM »
  I just asked my niece what comes first, 'x' or 'y'. After successfully completing the ABC song (many times) it was clear that 'x' does in fact come before 'y'.

  I'd consider that to be pretty much the final word on the subject.

+1

Omnomnom

  • Rogueliker
  • ***
  • Posts: 79
  • Karma: +0/-0
    • View Profile
    • Email
Re: map[x][y] or map[y][x]
« Reply #19 on: December 03, 2012, 12:17:56 AM »
I found arrays were too impersonal. I give all my tiles their own names.

Code: [Select]
Tile tilex1y1;
Tile tilex2y1;
Tile tilex7y3;
Tile amy;
Tile tilex14y30;
Tile true;
Tile false;
Tile tilex18y25;
Tile dontStandHere;
etc

Don't seem to be making much progress though for some reason

Quendus

  • Rogueliker
  • ***
  • Posts: 447
  • Karma: +0/-0
  • $@ \in \{1,W\} \times \{1,H\}$
    • View Profile
    • Klein Roguelikes
Re: map[x][y] or map[y][x]
« Reply #20 on: December 03, 2012, 12:53:22 AM »
I found arrays were too impersonal. I give all my tiles their own names.

Code: [Select]
Tile tilex1y1;
Tile tilex2y1;
Tile tilex7y3;
Tile amy;
Tile tilex14y30;
Tile true;
Tile false;
Tile tilex18y25;
Tile dontStandHere;
etc

Don't seem to be making much progress though for some reason

This is the future of programming. All it needs for this paradigm to work is an good actor for each tile.

naughty

  • Rogueliker
  • ***
  • Posts: 59
  • Karma: +0/-0
    • View Profile
Re: map[x][y] or map[y][x]
« Reply #21 on: December 03, 2012, 10:57:53 AM »
Quote
In terms of [ x][y] versus [y][ x], it won't affect performance on modern machines either way, but tty based displays drew the screen from left to right top to bottom making [ x][y] make more sense. I think virtually everyone uses [ x][y], and every open source program I've seen uses [ x][y]. I use [ x][y] too. As Jo said, [ x][y] is the standard in math, physics, and most importantly programming.

There is an enormous performance difference (can be upto 100X on tight loops) between [x ][y ] and [y ][x ] but most roguelikes aren't performance sensitive enough to notice the difference. The difference is even bigger on more modern hardware because CPU clock rates are so high that memory latency is a big issue. This is something you really need to be aware of when writing very high performance software like high end games, scientific applications and so on.

This is vital knowledge for working on the engines of AAA games for example.

In C multidimensional arrays are laid out with the rightmost array index increasing first so:

Code: [Select]
char map[X][Y] = ...

// The above is the same as a 1D array laid out as:
{ [X0,Y0], [X0,Y1], ..., [X0,YN], [X1,Y0], [X1,Y1], ..., [X1,YN] ... }


Memory is broken up into little contiguous arrays called cache-lines (normally 64 bytes these days). Only so many cache lines can be in the L1 cache, which is the fastest to access memory apart from registers. Getting a new cache-line into the cache is a lot slower than using something already in the cache. So the fastest way to access memory (all things being equal) is to access memory very close to previously accessed memory. For example:

Code: [Select]
const int DIM = 256;
char map[DIM][DIM] = {0};

// FAST LOOP
// This is 'column' major, it iterates through the Y dimension first.
for (int x=0; x < DIM; ++x) {
    for (int y=0; y < DIM; ++y) {
        map[x][y] = // something...
   }
}

// SLOW LOOP
// This is 'row' major, it iterates through the X dimension first.
for (int y=0; y < DIM; ++y) {
    for (int x=0; x < DIM; ++x) {
        map[x][y] = // something...
   }
}

Now the fast loop is 'column' major (top-to-bottom, left-to-right) but we have a natural assumption that we iterate in the same way we read, e.g. left-to-right, top-to-bottom. But to make that the fast way to loop you need to order you map as [y ][x ].

This is where the controversy comes from you can't have both [x ][y ] and 'left-to-right, top-to-bottom' iteration order you have to pick one if you want the best performance.

However most roguelikes don't need to care because they don't tax the hardware much at all but if you wanted to do FOV calculations on every tile for some reason you better do it the optimal way or it'll be a hell of a lot slower.