JavaScript:动态创建新数组

发布于 2024-12-09 04:32:27 字数 1916 浏览 0 评论 0原文

工作代码: http://jsfiddle.net/sXbRK/

我有各种线段,每个线段都有自己的 ID 。我知道哪些是相互交叉的。

现在,我需要将这些重叠线段的 ID 推入新数组中。

我不关心不互相交叉的线段。

如何将彼此交叉的线段 ID 推送到自己的数组中?

// New arrays of overlapping IDs
e.g. Array A = [1,2,3];
     Array B = [7,8,12];
     Array C = [14,15];

这是我到目前为止所得到的,不要忘记检查 jsFiddle 上正在进行的代码:

function cross( ls )
{
    var len = ls.length;

    for(var a=0; a < len - 1; a++)
    {
        for(var b=a+1; b < len; b++)
        {
            var A = ls[a],
                B = ls[b],
                combine = [],
                overlappers = [];

            if
            (
                (A.start <= B.start && (A.start + A.end) >= B.start)
                ||
                (A.start <= A.start && (B.start + B.end) >= A.start)
            )
            {
                // Add overlapping elements to arrays
                overlappers.push(A.id,B.id);

                // Create arrays for line segments that overlap each other
                combine[a] = new Array(overlappers);
                document.write('Array' + [a] + ' ==> ' + combine[a] + '<br />');
            }
        }
    }
}


// Test data
var lineSegments = [
    {id:'1',  start:0,    end:50},
    {id:'2',  start:0,    end:50},
    {id:'3',  start:0,    end:50},
    {id:'4',  start:100,  end:50},
    {id:'5',  start:200,  end:50},
    {id:'6',  start:300,  end:50},
    {id:'7',  start:900,  end:50},
    {id:'8',  start:900,  end:50},
    {id:'9',  start:600,  end:50},
    {id:'10', start:700,  end:50},
    {id:'11', start:800,  end:50},
    {id:'12', start:900,  end:50},
    {id:'13', start:1000,  end:50},
    {id:'14', start:1100,  end:50},
    {id:'15', start:1100,  end:50}
];

// Execute function
cross(lineSegments);

请帮助提供一些想法。谢谢你!

Working Code: http://jsfiddle.net/sXbRK/

I have various line segments and each has its own ID. I know which ones cross each other.

Now, I need to push the ID of those overlapping line segments into new arrays.

I don't care for the line segments that don't cross each other.

How do I do push the line segment IDs that cross each other into their own array?

// New arrays of overlapping IDs
e.g. Array A = [1,2,3];
     Array B = [7,8,12];
     Array C = [14,15];

Here's what I have so far and don't forget to check the work-in-progress code on jsFiddle:

function cross( ls )
{
    var len = ls.length;

    for(var a=0; a < len - 1; a++)
    {
        for(var b=a+1; b < len; b++)
        {
            var A = ls[a],
                B = ls[b],
                combine = [],
                overlappers = [];

            if
            (
                (A.start <= B.start && (A.start + A.end) >= B.start)
                ||
                (A.start <= A.start && (B.start + B.end) >= A.start)
            )
            {
                // Add overlapping elements to arrays
                overlappers.push(A.id,B.id);

                // Create arrays for line segments that overlap each other
                combine[a] = new Array(overlappers);
                document.write('Array' + [a] + ' ==> ' + combine[a] + '<br />');
            }
        }
    }
}


// Test data
var lineSegments = [
    {id:'1',  start:0,    end:50},
    {id:'2',  start:0,    end:50},
    {id:'3',  start:0,    end:50},
    {id:'4',  start:100,  end:50},
    {id:'5',  start:200,  end:50},
    {id:'6',  start:300,  end:50},
    {id:'7',  start:900,  end:50},
    {id:'8',  start:900,  end:50},
    {id:'9',  start:600,  end:50},
    {id:'10', start:700,  end:50},
    {id:'11', start:800,  end:50},
    {id:'12', start:900,  end:50},
    {id:'13', start:1000,  end:50},
    {id:'14', start:1100,  end:50},
    {id:'15', start:1100,  end:50}
];

// Execute function
cross(lineSegments);

Please help with some ideas. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-12-16 04:32:27

尝试:

var lineSegments = [
    {id:'1',  start:0,    end:50},
    {id:'2',  start:0,    end:50},
    {id:'3',  start:0,    end:50},
    {id:'4',  start:100,  end:50},
    {id:'5',  start:200,  end:50},
    {id:'6',  start:300,  end:50},
    {id:'7',  start:900,  end:50},
    {id:'8',  start:900,  end:50},
    {id:'9',  start:600,  end:50},
    {id:'10', start:700,  end:50},
    {id:'11', start:800,  end:50},
    {id:'12', start:900,  end:50},
    {id:'13', start:1000,  end:50},
    {id:'14', start:1100,  end:50},
    {id:'15', start:1100,  end:50}
];


function fixSense(line) {
   if (line.start > line.end) {
     var t = line.start;
     line.start = line.end;
     line.end = t;
   }
   return line;
}

function getCrossingLines(lines) {
  var a, b;
  var overlappers = [], combined = [];

  // for each line
  for (var i=0, iLen=lines.length - 1; i<iLen; i++) {
    a = fixSense(lines[i]);

    // for every other line
    for (var j=i+1, jLen=lines.length; j<jLen; j++) {
      b = fixSense(lines[j]);

      if (  (a.start <= b.start && a.end >= b.end) ||
            (a.start <= b.start && a.end >= b.end) ||
            (a.start <= b.end && a.end >= b.end) ) {
        overlappers.push(a.id, b.id);
        combined.push([a.id, b.id]);
      }
    }
  }

  console.log('combined: ' + combined);

}

getCrossingLines(lineSegments);

Try:

var lineSegments = [
    {id:'1',  start:0,    end:50},
    {id:'2',  start:0,    end:50},
    {id:'3',  start:0,    end:50},
    {id:'4',  start:100,  end:50},
    {id:'5',  start:200,  end:50},
    {id:'6',  start:300,  end:50},
    {id:'7',  start:900,  end:50},
    {id:'8',  start:900,  end:50},
    {id:'9',  start:600,  end:50},
    {id:'10', start:700,  end:50},
    {id:'11', start:800,  end:50},
    {id:'12', start:900,  end:50},
    {id:'13', start:1000,  end:50},
    {id:'14', start:1100,  end:50},
    {id:'15', start:1100,  end:50}
];


function fixSense(line) {
   if (line.start > line.end) {
     var t = line.start;
     line.start = line.end;
     line.end = t;
   }
   return line;
}

function getCrossingLines(lines) {
  var a, b;
  var overlappers = [], combined = [];

  // for each line
  for (var i=0, iLen=lines.length - 1; i<iLen; i++) {
    a = fixSense(lines[i]);

    // for every other line
    for (var j=i+1, jLen=lines.length; j<jLen; j++) {
      b = fixSense(lines[j]);

      if (  (a.start <= b.start && a.end >= b.end) ||
            (a.start <= b.start && a.end >= b.end) ||
            (a.start <= b.end && a.end >= b.end) ) {
        overlappers.push(a.id, b.id);
        combined.push([a.id, b.id]);
      }
    }
  }

  console.log('combined: ' + combined);

}

getCrossingLines(lineSegments);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文