【问题标题】:Javascript. Tricky: Are all array values consistent? (ie: does a = [a,a,a,a]) for any size arrayJavascript。棘手:所有数组值是否一致? (即:a = [a,a,a,a])对于任何大小的数组
【发布时间】:2012-12-27 07:24:27
【问题描述】:

这是相当概念性的,我希望有人能提供帮助。 我有一个脚本,当您在其中插入坐标(x,y,从 0 -> 800)时,它将返回 3 个单独范围内的预绘制坐标列表。

ie:我输入 200,200。我收到以下各项的列表:半径为 25、半径为 60、半径为 150 内的地块。

如果您对上下文感兴趣,请查看代码:

        display(coords[n][1] + ", " + coords[n][2]);

    if(n==0){

    for(var i=0; i < data.length; i++) {
        var xs = 0;
        var xy = 0;
        xs = xPlot - data[i][0];
        ys = yPlot - data[i][1];
        xs = xs * xs;
        ys = ys * ys;

        distance = Math.sqrt(xs + ys);

        if (distance <= 25){
            display2(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if(distance <= 60){
            display3(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if(distance <= 150){
            display4(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
    }

很简单。

现在,当我输入另一组坐标时,会创建另一个多维数组,我想检查两个(嗯,6 个)圆内的相交点。

我想使用一个数组来进行检查。如果输入了 2 个坐标,并且在两个内(绿色)圆内都找到了一个点,则 a = [g,g]。 如果在一个的内部找到一个点,但在另一个的中间(蓝色),则设置 a = [g,b] 你明白了。红色是外圈。

当输入 3 个坐标时,事情变得更加棘手。假设一个点在两个内线和一个中线之内。那么 a = [g,g,b]。 3 个列表的目的是将返回的值按从最佳到最差的组进行组织。因此,在具有 4 个输入的示例中,列表将按以下方式组织:

列表 1 / 列表 2 / 列表 3
gggg / gggb / bbbr
------- /ggbb / bbrr
------- /gbbb / brrr
-------/bbbb/rrrr

我将如何以可扩展的方式构建检查我的数组,以便如果我有 5 个输入,我就能够将我的结果放在适当的列中?

到目前为止,我已经开始这样做了:

else if(n>0){

    for(var i=0; i < data.length; i++) {
        for(var j=0; j < coords.length; j++) {
            var xs = 0;
            var ys = 0;
            xs = coords[j][1] - data[i][0];
            ys = coords[j][2] - data[i][1];
            xs = xs * xs;
            ys = ys * ys;
            distance = Math.sqrt(xs + ys);

            if (distance <= 25){
                a[j] = [,[g]];
                }
            else if (distance <= 60){
                a[j] = [,[b]];
                }
            else if (distance <= 150){
                a[j] = [,[r]];
                }
        }
        if($.inArray('g', a) && (!($.inArray('b', a)))){
            display2(data[i][0] + ", " + data[i][1] + " - " + alliance);

}

如果 a 包含 g 而不是 b,那么最后一行会执行吗? (jquery) 我在正确的轨道上吗?

【问题讨论】:

  • 似乎this guy's repos 中的一些可能有用,尤其是 es5-shim 和 Montage Collections。我敢肯定有人有一个array.intersect。

标签: javascript math iteration


【解决方案1】:

不要将出现在一个圆圈内作为一个数组中的一项,而是计算一个长度与圆圈数相同的数组中出现的次数。 IE。而不是[g,g,g,g,b,b,r,r,r,r,r,r,r],使用[4,2,7]。这样,您在添加项目时不必保持数组排序,您只需递增正确的项目。

这也使得根据相关性对结果进行排序变得容易。您可以通过将数组视为一个数字来为数组创建一个数值,其底数与数据坐标的数量相同。即:

var n = arr[0] * data.length * data.length + arr[1] * data.length + arr[2];

如果有 10 个数据坐标,数组 [4,2,7] 将得到值 427,因此很容易将它与得到值 350 的数组 [3,5,0] 进行比较。

您可以将数组计算为:

for(var i=0; i < coords.length; i++) {
  var arr = [0, 0, 0];
  for(var j=0; j < data.length; j++) {
    var xs = coords[i][1] - data[j][0];
    var ys = coords[i][2] - data[j][1];
    distance = Math.sqrt(xs * xs + ys * ys);
    if (distance <= 25) {
      arr[0]++;
    } else if (distance <= 60) {
      arr[1]++;
    } else if (distance <= 150) {
      arr[2]++;
    }
  }
  coords[i].arr = arr;
  coords[i].value = arr[0] * data.length * data.length + arr[1] * data.length + arr[2];
}

现在您可以对值的坐标进行排序:

coords.sort(function(a, b){
  return a.value - b.value;
});

【讨论】:

  • 虽然这是一种非常聪明的方法,但它并没有完全利用我需要的东西,因为最后我想打印出找到的坐标。我用一点酒和一些布尔值解决了我的问题。
【解决方案2】:

这是我想出的解决方案。它完全符合我的需要,同时显示在适当位置找到的坐标。

http://pastehtml.com/view/cowibn4bs.html 看看我的劳动成果。

if(n==0){

    for(var i=0; i < data.length; i++) {
        var xs = 0;
        var xy = 0;
        xs = xPlot - data[i][0];
        ys = yPlot - data[i][1];
        xs = xs * xs;
        ys = ys * ys;

        distance = Math.sqrt(xs + ys);

        if (distance <= 25){
            display2(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if(distance <= 60){
            display3(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if(distance <= 150){
            display4(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
    }
    }

    else if(n>0){

    $('#msgs2').html('');
    $('#msgs3').html('');
    $('#msgs4').html('');


    for(var i=0; i < data.length; i++) {
        a = [];
        for(var j=0; j < coords.length; j++) {
            var xs = 0;
            var ys = 0;
            xs = coords[j][1] - data[i][0];
            ys = coords[j][2] - data[i][1];
            xs = xs * xs;
            ys = ys * ys;
            distance = Math.sqrt(xs + ys);

            if (distance <= 25){
                a[j] = 'g';
                }
            else if (distance <= 60){
                a[j] = 'b';
                }
            else if (distance <= 150){
                a[j] = 'r';
                }
            else{
                a[j] = 0;
            }

        }
        if($.inArray(0, a) > -1){
        }
        else if($.inArray("g", a) > -1 && $.inArray("b", a) === -1){
            if($.inArray("r", a) === -1){
                display2(data[i][0] + ", " + data[i][1] + " - " + alliance);
            }
        }
        else if($.inArray("b", a) > -1 && $.inArray("r", a) === -1){
            display3(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
        else if($.inArray("r", a) > -1){
            display4(data[i][0] + ", " + data[i][1] + " - " + alliance);
        }
    }               

    }
    n++;

【讨论】:

  • 我找不到你在那里做任何排序?这些点只是按照您最初放入数组中的顺序显示。
  • pastehtml.com/view/cowibn4bs.html 看看我的劳动成果。 DISPLAY 函数将坐标放在 div 中。
  • 如果列表只包含一个点,是看不到排序的。
  • 你看链接了吗?它完全取决于在网络表单中输入的值,并检查绘制圆圈内的点。
  • 是的,我做到了,我试过了。它只处理一个点。如果您输入一个新点,它会删除第一个点的结果,只显示第二个点的结果。
猜你喜欢
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
相关资源
最近更新 更多