JavaScript:动态创建新数组

时间:2011-10-11 05:56:12

标签: javascript arrays geometry algebra

工作代码: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);

请帮忙解决一些问题。谢谢!

1 个答案:

答案 0 :(得分:1)

尝试:

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