!function(){ tdad3 = {}; tdad3.rips2d = _update; // data: array of points // radius: limiting radius of balls function _update(data, _radius) { var radius = _radius || -1; var delaunay = d3.geom.delaunay(data); var triangles = []; // Delete delaunay triangles whose edges would not exceed our // given radius, and return the rest of the triangles. delaunayLength = delaunay.length; for (var i = 0; i < delaunayLength; i++) { var triple = delaunay[i]; var a = distanceBetween(triple[0], triple[1]); var b = distanceBetween(triple[0], triple[2]); var c = distanceBetween(triple[1], triple[2]); var max = Math.max(a, Math.max(b, c)); if (radius == -1 || max < radius*2) { triangles.push(triple); } } return triangles; } function distanceBetween(a, b) { return Math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1])); } }();