我在这里介绍了我的2D算法(带有一些源代码和一个难看但方便的插图)
它比MBo在四分之一之一的圆的原点和边缘之间的计数点快3.4倍。
您只是想像一个刻有正方形的正方形,并且只计算该圆圈内该正方形之外的东西的八分之一。
public static int gaussCircleProblem(int radius) { int allPoints=0; //holds the sum of points double y=0; //will hold the precise y coordinate of a point on the circle edge for a given x coordinate. long inscribedSquare=(long) Math.sqrt(radius*radius/2); //the length of the side of an inscribed square in the upper right quarter of the circle int x=(int)inscribedSquare; //will hold x coordinate - starts on the edge of the inscribed square while(x<=radius){ allPoints+=(long) y; //returns floor of y, which is initially 0 x++; //because we need to start behind the inscribed square and move outwards from there y=Math.sqrt(radius*radius-x*x); // Pythagorean equation - returns how many points there are vertically between the X axis and the edge of the circle for given x } allPoints*=8; //because we were counting points in the right half of the upper right corner of that circle, so we had just one-eightth allPoints+=(4*inscribedSquare*inscribedSquare); //how many points there are in the inscribed square allPoints+=(4*radius+1); //the loop and the inscribed square calculations did not touch the points on the axis and in the center return allPoints;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)