var nodes = d3.selectAll("circle.node"); var squares = []; var intersections = []; nodes.each(function(node){ // var r = this.r.baseVal.value; var s = 3.14159*r*r; squares.push({node: node, square: s, r: r}); // nodes.each(function(node2){ // var center_dist = Math.sqrt(Math.pow(node.x-node2.x, 2)+(Math.pow(node.y-node2.y, 2))); var radius_sum = r + this.r.baseVal.value; if(center_dist <= radius_sum && node.index != node2.index){ // . , node.r = r; node2.r = this.r.baseVal.value; if(isNewIntersection(intersections, node, node2)) intersections.push({node1: node, node2: node2, center_dist: center_dist}); } }); });
var areaCalculator = { intersections: [], // , frame: {}, // circles: [], // figureArea: 0, // monteCarlo: function(p){ // var circles = []; var x1_, y1_, x2_, y2_; // var inCirclesArr = function(node){ for(var j=0; j<circles.length; j++){ if(circles[j].index==node.index){ return true; } } return false; }; for(var i=0; i<this.intersections.length; i++){ if(!inCirclesArr(this.intersections[i].node1)){ circles.push(this.intersections[i].node1); } if(!inCirclesArr(this.intersections[i].node2)){ circles.push(this.intersections[i].node2); } } this.circles = circles; circles.sort(function(a,b){ return ax-ar > bx-br ? 1 : -1; }); x1_ = circles[0].x-circles[0].r; circles.sort(function(a,b){ return a.x+ar < b.x+br ? 1 : -1; }); x2_ = circles[0].x+circles[0].r; circles.sort(function(a,b){ return ay-ar > by-br ? 1 : -1; }); y1_ = circles[0].y-circles[0].r; circles.sort(function(a,b){ return a.y+ar < b.y+br ? 1 : -1; }); y2_ = circles[0].y+circles[0].r; this.frame.x1 = x1_; this.frame.x2 = x2_; this.frame.y1 = y1_; this.frame.y2 = y2_; this.frame.area = (x2_-x1_)*(y2_-y1_); // paintRect(this.frame); // p - . 100.000, var p_positive = 0; // // p for(var i=0; i<p; i++){ var x_rand = Math.random()*(x2_-x1_)+x1_; var y_rand = Math.random()*(y2_-y1_)+y1_; var yes = false; for(var j=0; j<circles.length; j++) { if(!yes && ( (circles[j].x-circles[j].r) <= x_rand && (circles[j].x+circles[j].r) >= x_rand && (circles[j].y-circles[j].r) <= y_rand && (circles[j].y+circles[j].r) >= y_rand ) ){ yes = true; p_positive++; } } } // = *- / - this.figureArea = this.frame.area*p_positive/p; } };
Source: https://habr.com/ru/post/192272/