简体   繁体   中英

Representation and naming of coordinates in n-dimensional space

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.

在此处输入图片说明

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 int s? 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM