在javascript数组中分散数字

2023-08-02前端开发问题
1

本文介绍了在javascript数组中分散数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 10+ 个数字的数组.它们代表圆上的坐标 - 以度为单位,即每个数字在 0359.999999...

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...

我要解决的问题是,当我在圆圈上绘制项目时(通过 html5 canvas api),有时它们会聚集在一起,导致项目相互绘制.

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.

所以我想创建一个算法,将项目均匀地分布在它们的初始集群位置周围.假设(我希望这是一个可配置的选项)两个项目之间的最小距离是 5 度.

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.

所以如果初始数组是 [5, 41, 97, 101, 103, 158, 201, 214, 216, 217, 320] 那么我希望算法提出类似[5, 41, 95, 100, 105, 158, 201, 211, 216, 221, 320](粗体项目分散在其初始重心"周围,无论是 2 个或更多项目).

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).

此外,算法还需要识别 0 和 359 仅相差 1 个单位(度),并将这些项目均匀分布在周围.

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.

[未实施!]如果您的值的空间不足,[0..360[ 或超过 72 个元素相差 5,while 循环可能不会结束.

[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数组中分散数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437