0

Background:

I am having a discussion with a friend regarding the correct way to refer to elements within a 2D array for something I'm designing. I was unhappy with using XNA's Point and Vector2 structs as array references because the properties of them are labelled X and Y.

I created my structs for these, ArrayPoint and ArrayVector2 and called my own properties I and J. My friend thinks this is a massive waste of time, however, I do not like swapping the X and Y references because my maths background has always taught me to use i and j for matrices, e.g using myArray[i, j] instead of myArray[y, x].

What is the best way to deal with this situation?

Is it simply a case of swapping x and y? Or creating your own structs to deal with it how you like? Is it all down to personal preference since this is pretty much all arbitrary anyway?

Joe Shanahan
  • 816
  • 5
  • 21

3 Answers3

2

Listen to your friend!

There are so many reasons this is a bad idea, here are a few:

  • Your maths background may say i and j. But for arrays that map into 2D space (eg: a tile map, as opposed to a matrix) then x and y are actually preferable as they convey semantic meaning that makes your code easier to understand. (ie: you have X and Y axes, not I and J).

  • (Usually the cases where i and j make more semantic sense, you won't be storing the indexers in a struct anyway - they'll just be temporary locals.)

  • Without a genuine reason, less code is always preferable to more code. Writing and testing code takes up valuable time. Using someone else's code (like the built-in XNA types) saves you time.

  • One day you might write some utility function that takes a Point. If you have two versions of Point then you could well end up with two versions of that function. More code and, worse still: duplicate code!

  • "Not Invented Here" is a really, really, really bad habit to get into as a programmer. Learn to live with other people's code (as long as it works - which, in this case, it does).

  • No one else does this. Your code is going to confuse and annoy everyone who tries to work with it.

  • Small performance hit needlessly copying things around. (Plus a few other esoteric things that will have a minor affect on performance.)

(Also: indexing into an array with a Vector2 (floating point) is kinda weird.)

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • I was using integers as indexes =P But you make a good point, what I'm doing is only a prototype anyway, I'll make a point to switch to Point and Vector2 when I finalise stuff! – Joe Shanahan Aug 16 '12 at 16:42
1

It's not down to personal preference because you're going to have to convert to/from your structs to pass data to XNA library methods. You'll incur a conversion cost which will is admittedly very small, but could add up to being pretty significant if done repeatedly in a loop.

Sure, it's silly that SpriteFont.MeasureString() returns a data structure consisting of X and Y members instead of Width and Height, but it's easier to deal with the oddity than try to fight it.

My advice would be to try getting along with using x and y as indices and avoid the additional overhead.

itsme86
  • 19,266
  • 4
  • 41
  • 57
  • But wouldn't I incur the same conversation cost by using Point over my own ArrayPoint? The only difference between the two is that x is reffered to as j, and y is reffered to as i! In terms of converting, I would much rather have to swap my x's and y's when drawing rather than every time I refer to an objects position in a grid. – Joe Shanahan Aug 15 '12 at 19:11
  • In terms of additional overhead, for me, it's more demanding to think in terms of x, y (horizontal and then vertical) when dealing with arrays because I am just so used to thinking in i, j (rows and then columns) – Joe Shanahan Aug 15 '12 at 19:17
  • The incurred cost I was referring to would be, for example, getting a Vector2 from some XNA library method, converting it to your own Vector2 data type, and then having to convert it back to a Vector2 to pass it to another XNA library method. – itsme86 Aug 15 '12 at 19:34
  • I can't think of a situation where that would actually apply though. I'm only storing object position in the array. If I used a Point instead I would still at some point have to turn that into a x, y screen coordinate for drawing, etc. – Joe Shanahan Aug 15 '12 at 20:49
0

I think that what you are doing is fine. The only time I would advise against it is if you are adding significant complexity. As yo know, XNA software and others that have to handle many calculations and graphics can really get bogged down by slight changes if they are inefficient. However, what you are doing is only aiding you and shouldn't add said complexity. I'd say go for it.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51