栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

计算半径R和尺寸D的球体内的整数点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

计算半径R和尺寸D的球体内的整数点

我在这里介绍了我的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;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号