This article was born as a logical del monte recipes continuation of Friday's post about the bootstrap method, and especially its comments. Not protecting method bootstrap is to pay attention Monte Carlo methods. Here I want to share my experience in using Monte Carlo in one of his practical problems, as well as justification for the legality of the application. del monte recipes So my challenge was the need to calculate the area of a figure, which is the intersection of the circles, and then implement the language JavaScript. The area under the graph - is integral. Integration of Monte Carlo is widely known, but how many true notice, its application requires some justification. For details, del monte recipes ask a cat. Justification problem of calculating the area of intersection of two circles is trivial geometrical problem (coordinates of the centers of the circles and their radii are known to us). Area of intersection of two circles - is the sum of squares of the corresponding segments of the circles. There are solutions for calculating the intersection area of two, three, four circles in different special cases. But the decision of the general case for the intersection of three circles is even not so trivial. In the process of searching, I found even research on the calculation of the area of the intersection of N circles, but they are as interesting as it is complex. Here comes into play the Monte Carlo method. Thanks to modern computing power, this method allows a large number of statistical del monte recipes tests on the basis of which to make generalizations. Thus, the algorithm for calculating the area of any shape Monte Carlo method is the following: Figure fits into a rectangle. Coordinates of the rectangle are known, then it is known area. Pseudo-randomly generated within a rectangle of a large number of points. For each point is determined whether the point got into the original shape or not. As a result, the area of the original figure is calculated del monte recipes from the usual ratio: ratio of the number of points in the figure, the total number del monte recipes of points is generated del monte recipes with respect to the area of the figure area of the bounding rectangle. The last problem to be solved is that somehow it is necessary to determine whether the point fell into the original shape. In my case, this problem can be solved simply because my figure consists of circles, the coordinates of the centers and radii are known. del monte recipes Implementation tasks for JavaScript Drawing circles done a wonderful library facilities D3.js. Algorithm initial mutual arrangement of circles is beyond this article, so we will start as a given location. del monte recipes
var nodes = d3.selectAll ("circle.node"); var squares = []; var intersections = []; nodes.each (function (node) {/ / assume the radius and area of a circle var r = this.r.baseVal.value; var s = 3.14159 * r * r; squares.push ({node: node, square: s, r : r}); / / looking for the intersection of pairs of circles nodes.each (function (node2) {/ / the distance between the centers and the sum of the radii 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 && del monte recipes node.index! = node2.index) {/ / circles intersect. verify that this intersection is found for the first time 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: del monte recipes [], / / array of intersections, installed outside the frame: {}, / / frame around the figure circles: [], / / array of circles figureArea: 0, / / required area figures monteCarlo: function (p) {/ / get an array of circles of intersection var circles = []; var x1_, y1_, x2_, y2_; / / Coordinates del monte recipes of the rectangle del monte recipes 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)) del monte recipes {circles.push del monte recipes (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 del monte recipes [0]. r; circles.sort (function (a, b) {return a.y + ar <b.y + br? 1: -1;}); del monte recipes 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_); / / Draw a rectangle paintRect (this.frame); / / P - the number of generated points. In the example used 100,000, which was enough for acceptable accuracy var p_positive = 0; / / Number of points del monte recipes caught in f
No comments:
Post a Comment