读书人

哪位大神解释一上 这段程序啥意思? 求

发布时间: 2012-10-11 10:16:10 作者: rapoo

哪位大神解释一下 这段程序啥意思? 求多边形的
这是求多边形 顶点的,java做的, 循环里面是什么意思啊?

List<Point> getPoints(int n) {
ArrayList<Point> ps = new ArrayList<Point>();
int ox = getWidth()/2;
int oy = getHeight()/2;
double r = (ox>oy ? oy : ox)*0.9;
double angle = 2*Math.PI/n;
double startAngle = (Math.PI-angle)/2;
for(int i=0; i<n; i++) {
int x = (int)(ox+r*Math.cos(startAngle+i*angle));
int y = (int)(oy+r*Math.sin(startAngle+i*angle));
ps.add(new Point(x,y));
}

[解决办法]
好吧,我讲详细点。

double angle = 2*Math.PI/n;
angle = 2*PI/n,意思是多边形相邻的两个顶点和中心点的连线的夹角,即360/n.
即增量。

double startAngle = (Math.PI-angle)/2;
startAngle 是起始的角度,即第一个顶点到中心点的连线与水平线的夹角。

for(int i=0; i<n; i++) {
int x = (int)(ox+r*Math.cos(startAngle+i*angle));
r*cos(startAngle+i*angle)为第i个点在横轴上的投影。
int y = (int)(oy+r*Math.sin(startAngle+i*angle));
r*Math.sin(startAngle+i*angle)为第i个点在纵轴上的投影。

图示:

读书人网 >软件架构设计

热点推荐