Javascript 圆形阵列中两个元素之间的最小距离

Javascript 圆形阵列中两个元素之间的最小距离,javascript,math,Javascript,Math,给定一个圆形阵列,确定两个元素之间最小距离的有效方法是什么 比如从这里搬家 [0,0,0,0,0,1] [0,0,0,0,0,1] 到这里 [1,0,0,0,0,0] 从外部边界移动时更方便 [0,0,0,0,0,1] [0,0,0,0,0,1] 到这里 [0,0,0,1,0,0] 内部更方便 我最初的想法是: sourceLocation = rotatorElements["pos"].indexOf(1); targetLocation = rotatorElements["p

给定一个圆形阵列,确定两个元素之间最小距离的有效方法是什么

比如从这里搬家

[0,0,0,0,0,1]
[0,0,0,0,0,1]
到这里

[1,0,0,0,0,0]
从外部边界移动时更方便

[0,0,0,0,0,1]
[0,0,0,0,0,1]
到这里

[0,0,0,1,0,0]
内部更方便

我最初的想法是:

sourceLocation = rotatorElements["pos"].indexOf(1);
targetLocation = rotatorElements["pos"].rotate(delta).indexOf(1);

var D = Math.abs(targetLocation - sourceLocation);
var O = Math.abs(rotatorElements["pos"].length - D);

if ((D - O == 0)) direction = 1;
else {
    if (D < O) direction = -1;
    else direction = 1;
}
sourceLocation=rotatoreelements[“pos”].indexOf(1);
targetLocation=旋转元素[“位置”]。旋转(增量)。索引(1);
var D=Math.abs(targetLocation-sourceLocation);
var O=数学绝对值(旋转元素[“位置”]。长度-D);
如果((D-O==0))方向=1;
否则{
如果(D

注意:
旋转
将圆形阵列旋转定义的位置数。

计算元素之间的绝对距离非常有用:

var dist = Math.abs(targetLocation - sourceLocation);
然后,您可以检查它是否大于或小于数组长度的一半。如果距离超过长度的一半,则更接近环绕:

if (dist < rotatorElements["pos"].length / 2) {
  // move internally
} else {
  // wrap around
}

演示:

编辑:我想我回答了标题中的问题,但你想要的是方向而不是距离。然后:

function getDirection(from, to, array) {
    if (from === to) {
        return 0;
    }
    var internal = (Math.max(from, to) - Math.min(from, to) < array.length/2) ? true : false;
    if (internal && from < to
       ||
       !internal && from > to
    ) {
       return 1;
    } else {
       return -1;
    }
}
函数getDirection(从、到、数组){ 如果(从===到){ 返回0; } var internal=(Math.max(from,to)-Math.min(from,to)到(&F) ) { 返回1; }否则{ 返回-1; } }
这里是我的解决方案(在groovy中),它提供了方向和距离(如果输出为负,则移动为逆时针方向):


只是测试一下,给我一点时间。。。我把它发布在github上:在第258行附近,元素应该沿着与最近的对象相关的方向旋转。我把你的密码放进去了it@user2347441:如果目标始终位于源的左侧(反之亦然),则内部移动仅意味着您在一个特定的方向上移动,但您不需要使用
Math.abs
首先获得它们之间的距离。您可以使用
(sourceLocation
计算内部移动时的方向,如果环绕,则方向会反转。只需尝试该方向,但仍无法正确运行。。。我将尝试将其发布到JSFIDLE上。不,不能放在那里。。。需要Modernizer和插件…@user2347441:我不知道你对代码做了什么。我把它放在上面的一个演示中。好的,你们可以看看github的258行
def move(def start, def target, def length){
    def clockwise
    def counterclockwise
    if (start > target){
        clockwise = length - start + target
        counterclockwise = start - target
    } else {
        clockwise = target - start
        counterclockwise = length - target + start
    }
    if (counterclockwise < clockwise){
        return counterclockwise * -1
    }
    return clockwise
}
groovy:000> move(0,0,0)
===> 0
groovy:000> move(0,0,4)
===> 0
groovy:000> move(0,1,4)
===> 1
groovy:000> move(0,2,4)
===> 2
groovy:000> move(0,3,4)
===> -1
groovy:000> move(1,0,4)
===> -1
groovy:000> move(1,1,4)
===> 0
groovy:000> move(1,2,4)
===> 1
groovy:000> move(1,3,4)
===> 2
groovy:000> move(2,0,4)
===> 2
groovy:000> move(2,1,4)
===> -1
groovy:000> move(2,2,4)
===> 0
groovy:000> move(2,3,4)
===> 1
groovy:000> move(3,0,4)
===> 1
groovy:000> move(3,1,4)
===> 2
groovy:000> move(3,2,4)
===> -1
groovy:000> move(3,3,4)
===> 0