Representation and naming of coordinates in n-dimensional space

291 views Asked by At

Currently I am making a Breakout game, and I thought about the representation of coordinates as well as the naming convention for them. In this particular example you only have two coordinates x and y in a 2-dimensional space.

enter image description here

Is the best representation for (even 2-dimensional) coordinates-systems: arrays? Why would it still be useful to use int in these kind of situations? When does it make sense to switch to an array? It seems bad practise that when you use variables just as a way to describe the order in which they appear like you do with x and y in a coordinate system.

Which will be more efficient? Will working with a 2-dimensional array be faster than working with two basic integers? Will updating a value be faster as an integer or array? I assume that working with arrays with more dimensions be far easier to manipulate.

int[] coordinates = {1,2}; //initializing, which way is faster? 
int xPosition = 1;
int yPosition = 2;

xPosition = 2; //updating the coordinates, which way is faster?
yPosition = 3;
coordinates = {2, 3};

To end this madness: What would be the best variable names if you were to choose ints? These are my struggles:

int xPosition, yPosition //a bit long
int xPos, yPos //looks short and clear to me, but maybe there is an 
//'normal' way to do it?
int xpos, ypos //short and looks less clear but represents better imo
// that it's one entity
int positionX, positionY //auto-complete takes twice as long
int posY, posX //harder to see what's meant here for me
1

There are 1 answers

2
lenhuy2106 On

n-dim. Arrays as a low level structure are good enough and an optimal choice for your case:

  • Those coordinates are static in size.
  • You easily access the elements by their index.
  • Iterating is much faster and easy to read.
  • No search or sort algortihms needed.

Just make sure you initially define the exact size to avoid castings.

Hope it helps.