在JavaScript阵列分散号码 [英] Dispersing numbers in a javascript array

查看:101
本文介绍了在JavaScript阵列分散号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

10 + 数字数组。他们重新在一个圆圈present坐标 - 以度数,即每个号码在 0 359.999999 ...

我试图解决的问题是,当我画的圆(通过HTML5画布API)我的项目,有时他们都聚集在一起,在项目结果被绘制到对方。

所以,我想创建一个项目分散均匀围绕他们的初始聚类位置的算法。比方说,(我想这是一个配置选项)两个项目之间的最小距离为 5 度。

所以,如果最初的数组是 [5,41,97,101,103,158,201,214,216,217,320] 那么我想算法拿出类似[5,41, 95 100 105 ,158,201, 211,216,221 ,320]
(加粗带物品分散在他们最初的重心那些不管是2个或更多的项目)。

还有什么是neccessary是,该算法可以识别0和359为仅有1单位(度)分开,以及S周围均匀$ P $垫等物品。

有没有人曾创造了这样的算法还是有一个好主意,它如何实现?即使是一些总体思路是受欢迎的。
我相信我能做到这一点,用大量的试验和错误,但我想听到一些猜测,如果你愿意,首先


解决方案

  VAR VAL = [5,41,96,101,103,158,201,214,216,217, 320,1201,1213,1214,1216,1217,1320]
    三角洲= Array.apply(空,{长度:val.length})。图(函数(){返回0})
    结果,
    阈值= 5,
    融合= FALSE;文件撰写(VAL:'+ VAL +'< BR>');
而(!融合){
    融合= TRUE;
    三角洲= delta.map(功能(D,I){
        如果(ⅰ&下; delta.length - 1和;&放大器; delta.length→1){
            如果(VAL第[i + 1] +增量第[i + 1] - VAL [Ⅰ] - D&下;阈值){
                融合= FALSE;
                增量[I + 1] + = 1;
                返回ð - 1;
            }
        }
        返回D组;
    });
    文件撰写(三角洲:'+三角洲+'< BR>');
}结果= val.map(函数(V,I){
    返回V +三角洲[I]
});
文件撰写(结果:+结果+'< BR>');//尽量减少差异
融合= FALSE;
而(!融合){
    融合= TRUE;
    三角洲= delta.map(功能(D,I){
        如果(ⅰ&下; delta.length - 2){
            变种空间= VAL第[i + 1] +增量第[i + 1] - VAL [Ⅰ] - D组;
            如果(D&℃,放大器;&放大器;空间>阈值){
                融合= FALSE;
                返回D +空格 - 阈值;
            }
        }
        返回D组;
    });
    文件撰写(增量分:'+三角洲+'< BR>');
}结果= val.map(函数(V,I){
    返回V +三角洲[I]
});
文件撰写(结果:+结果+'< BR>');

在code推2太近夫妇,一边一个APPART。这是对称地并且有时导致向远推值可以被校正。

[未实现!]
如果你的价值观的空间是不够的,[0..360 [或为5的差异更多的则72元的while循环可能不会走到尽头。

编辑:最小化块应迭代,直到所有值都correted

I have an array of 10+ numbers. They represent coordinates on a circle - in degrees, i.e. each number is in between 0 and 359.999999...

The problem I am trying to solve is that when I draw my items on the circle (via html5 canvas api), sometimes they are clustered together and that results in items being drawn onto each other.

So I would like to create an algorithm which disperses items evenly around their initial cluster position. Let's say (and I'd like this to be a configurable option) the minimal distance between two items is 5 degrees.

So if the initial array is [5, 41, 97, 101, 103, 158, 201, 214, 216, 217, 320] then I would like the algorithm come up with something like [5, 41, 95, 100, 105, 158, 201, 211, 216, 221, 320] (with bolded items being dispersed around their initial "gravity center" regardless whether those are 2 or more items).

Also what would be neccessary is that the algorithm recognizes 0 and 359 being just 1 unit (degree) apart and also spread such items evenly around.

Has anyone ever created such algorithm or have a good idea how it could be achieved? Even some general thoughts are welcome. I'm sure I could achieve that with plenty of trial and error, but I'd like to hear some educated guesses, if you will, first.

解决方案

var val = [5, 41, 96, 101, 103, 158, 201, 214, 216, 217, 320, 1201, 1213, 1214, 1216, 1217, 1320],
    delta = Array.apply(null, { length: val.length }).map(function () { return 0 }),
    result,
    threshold = 5,
    converged = false;

document.write('val: ' + val + '<br>');
while (!converged) {
    converged = true;
    delta = delta.map(function (d, i) {
        if (i < delta.length - 1 && delta.length > 1) {
            if (val[i + 1] + delta[i + 1] - val[i] - d < threshold) {
                converged = false;
                delta[i + 1] += 1;
                return d - 1;
            }
        }
        return d;
    });
    document.write('delta: ' + delta + '<br>');
}

result = val.map(function (v, i) {
    return v + delta[i];
});
document.write('result: ' + result + '<br>');

// try to minimise difference
converged = false;
while (!converged) {
    converged = true;
    delta = delta.map(function (d, i) {
        if (i < delta.length - 2) {
            var space = val[i + 1] + delta[i + 1] - val[i] - d;
            if (d < 0 && space > threshold) {
                converged = false;
                return d + space - threshold;
            }
        }
        return d;
    });
    document.write('delta min: ' + delta + '<br>');
}

result = val.map(function (v, i) {
    return v + delta[i];
});
document.write('result: ' + result + '<br>');

the code pushes two too close couples appart with one on each side. this is symetrically and results in sometimes to far pushed values, which can be corrected.

[not implemented!] if the space of your values is not sufficient, [0..360[ or by more then 72 elements with a difference of 5 the the while loop may not come to an end.

edit: the minimise block should iterate until all values are correted.

这篇关于在JavaScript阵列分散号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆