确定一个数组索引是否距离另一个索引不超过n个位置?

huangapple go评论71阅读模式
英文:

How to determine if an array index is within n positions from another index?

问题

我正在处理一个循环式的旋转木马。给定一个包含 n 个项目的数组,其中 n>6,我想计算数组中所有距离特定索引(currentIndex)不超过 3 个位置的项目,以循环方式进行计算,例如:

// currentIndex = 0
// i       0    1    2    3    4    5    6    7    8    9   
// --------------------------------------------------------
// offset   0   +1   +2   +3   +3   +3   +3   -3   -2   -1

在上面的示例中,0 是 currentIndex。索引 1、2、3 位于 currentIndex + 3 个位置内。同样,索引 7、8、9 也在 currentIndex - 3 个位置内,以循环方式计算。所有其他超出此范围的索引都被视为值为 3。这些正负值最终将映射到屏幕上的位置。

我编写了以下函数来计算给定索引相对于 currentIndex 的偏移位置:

function getOffset(currentIndex: number, index: number, length: number) {
  const diff = index - currentIndex;

  if (diff === 0) {
    return 0;
  } else if (diff < 0) {
    if (diff < -3) {
      return Math.min(length - currentIndex + index, 3);
    } else {
      return Math.min(diff, 3);
    }
  } else {
    if (diff < length - 3) {
      return Math.min(diff, 3);
    } else {
      return Math.max(diff - length, -3);
    }
  }
}

// getOffset(0, 0, 10) -> 0
// getOffset(0, 1, 10) -> 1
// getOffset(0, 9, 10) -> -1
// getOffset(0, 6, 10) -> 3

这个算法可以工作,但有点冗长。是否有更简单的方法来做这个?

英文:

I am working on a circular carousel. Given an array of n items, n>6 in my case, i want to calculate all the items within the array that are less than or equal to 3 positions away from a particular index (currentIndex) in a circular fashion e.g.

//  currentIndex = 0
//  i       0    1    2    3    4    5    6    7    8    9   
// --------------------------------------------------------
// offset   0   +1   +2   +3   +3   +3   +3   -3   -2   -1

In the above example, 0 is the currentIndex. Indices 1,2,3 are within currentIndex + 3 places. Also, indices 7, 8, 9 are also within currentIndex - 3 positions in a circular fashion. All other indices that lie out side the range are considered to be of value 3. These positive and negative values will ultimately map to positions on the screen.

I have written the following function to calculate the offset position of a given index relative to a currentIndex

function getOffset(currentIndex: number, index: number, length: number) {
  const diff = index - currentIndex;

  if (diff === 0) {
    return 0;
  } else if (diff &lt; 0) {
    if (diff &lt; -3) {
      return Math.min(length - currentIndex + index, 3);
    } else {
      return Math.min(diff, 3);
    }
  } else {
    if (diff &lt; length - 3) {
      return Math.min(diff, 3);
    } else {
      return Math.max(diff - length, -3);
    }
  }
}

// getOffset(0, 0, 10) -&gt; 0
// getOffset(0, 1, 10) -&gt; 1
// getOffset(0, 9, 10) -&gt; -1
// getOffset(0, 6, 10) -&gt; 3

This algorithm works but is verbose. Is there a simpler way to do this?

答案1

得分: 1

尝试这个:

function getOffset(currentIndex, index, length) {
  const diff = (index - currentIndex + length) % length;
  if (diff <= 3) {
    return diff;
  } else if (diff >= length - 3) {
    return diff - length;
  } else {
    return 3;
  }
}
英文:

Try this :

function getOffset(currentIndex, index, length) {
  const diff = (index - currentIndex + length) % length;
  if (diff &lt;= 3) {
    return diff;
  } else if (diff &gt;= length - 3) {
    return diff - length;
  } else {
    return 3;
  }
}

huangapple
  • 本文由 发表于 2023年5月24日 23:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定