score:7

This is a well studied problem. Before optimizing, though, I'd try the "computational heavy" brute-force approach. Borrowing from the excellent answers here, I've coded up a quick integration with highcharts (fiddle here):

``````get_line_intersection = function(p0,p1,p2,p3)
{
var p0_x = p0.x;
var p0_y = p0.y;
var p1_x = p1.x;
var p1_y = p1.y;
var p2_x = p2.x;
var p2_y = p2.y;
var p3_x = p3.x;
var p3_y = p3.y;

var s1_x, s1_y, s2_x, s2_y;
s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;

var s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
var t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
return [p0_x + (t * s1_x),p0_y + (t * s1_y)];
}

return false;
}

\$(function () {
\$('#container').highcharts({
series: [{
name: 'line1',
data: \$.map(new Array(10), function(){return Math.random() * 10;})
}, {
name: 'line2',
data: \$.map(new Array(10), function(){return Math.random() * 10;})
}, {
name: 'intersect',
data: [],
type: 'scatter'
}
]
}, function(chart){
var s0 = chart.series.points;
var s1 = chart.series.points;
var s2 = chart.series;
var n0 = s0.length;
var n1 = s1.length;
var i,j,isect;
for (i = 1; i < n0; i++){
for (j = 1; j < n1; j++){
if (isect = get_line_intersection(s0[i-1],s0[i],
s1[j-1],s1[j])){

}
}
}
chart.redraw();
});
});
`````` 