简体   繁体   English

n维空间中坐标的表示和命名

[英]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. 目前,我正在制作一款Breakout游戏,并考虑了坐标的表示形式以及它们的命名约定。 In this particular example you only have two coordinates x and y in a 2-dimensional space. 在此特定示例中,您在二维空间中只有两个坐标x和y。

在此处输入图片说明

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? 为什么在这种情况下使用int仍然有用呢? 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. 似乎不明智的做法是,当您使用变量来描述变量出现的顺序时,就像在坐标系中使用x和y一样。

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? 结束这种疯狂:如果选择int ,最好的变量名是什么? 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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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