繁体   English   中英

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

[英]Representation and naming of coordinates in n-dimensional space

目前,我正在制作一款Breakout游戏,并考虑了坐标的表示形式以及它们的命名约定。 在此特定示例中,您在二维空间中只有两个坐标x和y。

在此处输入图片说明

是(甚至二维的)坐标系:数组的最佳表示形式吗? 为什么在这种情况下使用int仍然有用呢? 什么时候切换到数组有意义? 似乎不明智的做法是,当您使用变量来描述变量出现的顺序时,就像在坐标系中使用x和y一样。

哪个会更有效? 使用二维数组比使用两个基本整数快吗? 将值更新为整数或数组会更快吗? 我认为使用更大维度的数组要容易得多。

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};

结束这种疯狂:如果选择int ,最好的变量名是什么? 这些是我的挣扎:

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

正暗淡。 作为低层结构的数组已经足够好,并且是针对您的情况的最佳选择:

  • 这些坐标的大小是静态的。
  • 您可以通过元素的索引轻松访问元素。
  • 迭代要快得多且易于阅读。
  • 无需搜索或排序算法。

只要确保您最初定义了确切的尺寸即可避免铸造。

希望能帮助到你。

暂无
暂无

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

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