Javascript 计算全世界的字符串循环数|十六进制网格

Javascript 计算全世界的字符串循环数|十六进制网格,javascript,html,2d,game-engine,Javascript,Html,2d,Game Engine,所以我有一个十六进制网格,有一个100x100的网格。。。网格是递归的,因此当您向左滚动超过“0,0”位置时,它会绘制“99,0”行等。。。我的问题是找到一个算法,让我计算,如果我是循环回来 例如: 96-97-98-99-0-1-2-3-4 两者的起始位置都小于结束位置 如果我在第2行,屏幕平移到第98行: 2到98工作(98)-100=-2然后-2-(2)=4的距离 96到98失败(98)-100=-2然后-2-(96)=距离98(正确为2) 两者的结束位置都小于起始位置 然而,这并不是双向

所以我有一个十六进制网格,有一个100x100的网格。。。网格是递归的,因此当您向左滚动超过“0,0”位置时,它会绘制“99,0”行等。。。我的问题是找到一个算法,让我计算,如果我是循环回来

例如: 96-97-98-99-0-1-2-3-4

两者的起始位置都小于结束位置 如果我在第2行,屏幕平移到第98行:

2到98工作(98)-100=-2然后-2-(2)=4的距离

96到98失败(98)-100=-2然后-2-(96)=距离98(正确为2)

两者的结束位置都小于起始位置 然而,这并不是双向的。。。所以我们这样做。。。 从第98行到第2行:

98到2个工程(2)+100=102然后102-(98)=4的距离

96到98失败(96)+100=196然后196-(98)=距离98(正确值为2)

正如你所看到的,我不能说如果开始<结束或开始>结束,因为数字循环会把它搞糟。我需要检测“越界”时的一些方式


在乔纳森的建议之后,我意识到客户的展示和幕后活动不必排队。我已经改变了引擎,所以有2个十六进制值。。。一个是实际位置,如98,20,02,1。另一个是来自视图端口的文字位置

看起来像这样:

 Actual:  96  97  98  99  00  01  02  03  04  <= display in client & reference hexGridModel

 Literal: -4  -3  -2  -1  00  01  02  03  04  <= use to calculate buffer position updates from camera
GRID_SIZE = 100
if (direction == LEFT && new_pos > old_pos) {
    distance = (GRID_SIZE - new_pos) + old_pos;
} else if (direction == RIGHT && new_pos < old_pos) {
    distance = (GRID_SIZE - old_pos) + new_pos;
} else {
    distance = abs(new_pos - old_pos)
}

如果没有比你给我们提供的更多的信息,这是不可能的。以从2移动到98为例。您将无法判断用户是否从
2->1->0->99->98
2->3->4->…->97->98

因此,能够确定这一点的关键是知道玩家或物体移动的方向

如果您知道,您可以说(假设0位于栅格的左边界,99位于栅格的右边界):

if((方向==左和新位置>旧位置)| |(方向==右和新位置<旧位置){
//“线”已经越过了。
}否则{
//“线”没有越过。
}
如果您还需要知道行驶的距离,您可以这样分解:

 Actual:  96  97  98  99  00  01  02  03  04  <= display in client & reference hexGridModel

 Literal: -4  -3  -2  -1  00  01  02  03  04  <= use to calculate buffer position updates from camera
GRID_SIZE = 100
if (direction == LEFT && new_pos > old_pos) {
    distance = (GRID_SIZE - new_pos) + old_pos;
} else if (direction == RIGHT && new_pos < old_pos) {
    distance = (GRID_SIZE - old_pos) + new_pos;
} else {
    distance = abs(new_pos - old_pos)
}
GRID\u SIZE=100
如果(方向==左侧和新位置>旧位置){
距离=(网格大小-新位置)+旧位置;
}否则如果(方向==右侧和新位置<旧位置){
距离=(网格大小-旧网格位置)+新网格位置;
}否则{
距离=abs(新位置-旧位置)
}

如果没有比您提供的更多的信息,我想说这是不可能的。以从2移动到98为例。您将无法判断用户是从
2->1->0->99->98
还是
2->3->4->…->97->98

因此,能够确定这一点的关键是知道玩家或物体移动的方向

如果您知道,您可以说(假设0位于栅格的左边界,99位于栅格的右边界):

if((方向==左和新位置>旧位置)| |(方向==右和新位置<旧位置){
//“线”已经越过了。
}否则{
//“线”没有越过。
}
如果您还需要知道行驶的距离,您可以这样分解:

 Actual:  96  97  98  99  00  01  02  03  04  <= display in client & reference hexGridModel

 Literal: -4  -3  -2  -1  00  01  02  03  04  <= use to calculate buffer position updates from camera
GRID_SIZE = 100
if (direction == LEFT && new_pos > old_pos) {
    distance = (GRID_SIZE - new_pos) + old_pos;
} else if (direction == RIGHT && new_pos < old_pos) {
    distance = (GRID_SIZE - old_pos) + new_pos;
} else {
    distance = abs(new_pos - old_pos)
}
GRID\u SIZE=100
如果(方向==左侧和新位置>旧位置){
距离=(网格大小-新位置)+旧位置;
}否则如果(方向==右侧和新位置<旧位置){
距离=(网格大小-旧网格位置)+新网格位置;
}否则{
距离=abs(新位置-旧位置)
}
注意:在看到问题的更新之前,我发布了以下内容。这是我猜您在原始问题中可能的意思。也许它仍然会有所帮助

我真的不理解所表达的要求,所以我猜测它是“找到两个位置之间的最短距离,如果较短,允许跨越99/0边界”

这个函数返回一个具有三个属性的对象:距离、方向和环绕(true/false)。为了便于测试,我加入了一个
toString()

function shortestDistance(startPosition, endPosition) {
   var difference = startPosition > endPosition
                  ? startPosition - endPosition
                  : endPosition - startPosition,
       wrapped = difference > 50;


   return {
      distance      : wrapped ? 100 - difference : difference,
      direction     : (startPosition < endPosition
                      ? (wrapped ? "left" : "right")
                      : (wrapped ? "right" : "left")),
      wrappedAround : wrapped,
      toString      : function() {
                         return (
                             this.distance === 0
                             ? "Didn't move"
                             : "Travelled " + this.distance 
                               + " to the " + this.direction + ", and "
                               + (this.wrappedAround ? "did" : "didn't")
                               + " wrap around.");
                      }
   };
}

var result = shortestDistance(2,98);
alert(result.distance);      // 4
alert(result.direction);     // "left"
alert(result.wrappedAround); // true

alert(shortestDistance(2,98));
  // Travelled 4 to the left, and did wrap around.
alert(shortestDistance(98,2));
  // Travelled 4 to the right, and did wrap around.
alert(shortestDistance(96,98));
  // Travelled 2 to the right, and didn't wrap around.
alert(shortestDistance(98,96));
  // Travelled 2 to the left, and didn't wrap around.
alert(shortestDistance(43, 43));
  // Didn't move
alert(shortestDistance(1, 50));
  // Travelled 49 to the right, and didn't wrap around.
alert(shortestDistance(1, 51));
  // Travelled 50 to the right, and didn't wrap around.
alert(shortestDistance(1, 52));
  // Travelled 49 to the left, and did wrap around.
alert(shortestDistance(50, 1));
  // Travelled 49 to the left, and didn't wrap around.
alert(shortestDistance(51, 1));
  // Travelled 50 to the left, and didn't wrap around.
alert(shortestDistance(52, 1));
  // Travelled 49 to the right, and did wrap around.    
功能最短距离(起始位置、结束位置){
变量差异=起始位置>结束位置
?起始位置-结束位置
:结束位置-开始位置,
包裹=差异>50;
返回{
距离:包裹?100-差异:差异,
方向:(起始位置<结束位置
?(包裹?“左”:“右”)
:(包装?“右”:“左”),
包裹的,
toString:function(){
返回(
这个距离===0
“没动”
:“已行驶”+此距离
+到“+this.direction+”和
+(this.wrappedAround?“did”:“not”)
+"环绕";;
}
};
}
var结果=最短距离(2,98);
警报(结果.距离);//4
警报(结果方向);/“左”
警报(result.wrappedAround);//真
警报(最短距离(2,98));
//向左走了4圈,绕了一圈。
警报(最短距离(98,2));
//向右转了4圈,绕了一圈。
警报(最短距离(96,98));
//向右转了2圈,没有绕过去。
警报(最短距离(98,96));
//向左走了2圈,没有绕过去。
警惕(最短距离(43,43));
//没有动
警报(最短距离(1,50));
//向右转了49圈,没有绕过去。
警报(最短距离(1,51));
//向右行驶了50英里,但没有绕行。
警报(最短距离(1,52));
//向左走了49英里,确实绕了一圈。
警报(最短距离(50,1));
//向左行驶了49英里,但没有绕行。
警报(最短距离(51,1));
//T