Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intersecting svg closed paths

What is a good way (both code-wise and performance-wise) to test if two shapes drawn by svg path is intersecting? I am doing this in d3 and is using the "cardinal-closed" line interpolation

More specifically, I am creating convex hulls (more complex than in the image), and I want to merge hulls if they overlap. It is easy to do if I use a "linear-closed" interpolation, because then I can use the vertices to calculate intersections, but the "cardinal-closed" interpolation looks better where I use it.

var v1 = [[100,100],[200,100],[200,200],[100,200]],
v2 = [[210,100],[310,100],[310,200],[210,200]];

var hull1 = d3.geom.hull(v1),
    hull2 = d3.geom.hull(v2);

var svg = d3.select("#foo")
    .append("svg");

var line = d3.svg.line()
    .interpolate("cardinal-closed")
    .x(function(d) {return d[0];})
    .y(function(d) {return d[1];});

svg.append("path")
    .attr("d", line(hull1));
svg.append("path")
    .attr("d", line(hull2));

Output of code

Here is a jsfiddle. How do I test if these shapes are intersecting/overlapping?

like image 383
swenedo Avatar asked Jun 19 '13 22:06

swenedo


1 Answers

As @Phrogz said you should probably use the intersection library.

like image 156
0xcaff Avatar answered Oct 17 '22 06:10

0xcaff