基于x和y坐标,我需要实现一个函数
int getArea(double xcoord, double ycoord , double radius)
以获得时钟上的大致时间。粗略时间是指时钟上的整小时时间。所以在这种情况下,1点,2点,等等。。。如果坐标在半径之外,函数应该返回-1。在
或者更形象化一点:想象一下第二部饥饿游戏电影/书。你有12个不同的区域,像一个时钟一样排列。在函数中输入贡品的坐标,以及竞技场的半径,作为回报,你希望得到贡品当前所在的区域。在
所以我设法弄明白了如何检查,位置是否在时钟上
^{pr2}$另外,我有一个小片段计算了扇区:
int sectors = 12;
double angle = 2*Math.PI/sectors;
double x_sectors[] = new double[sectors];
double y_sectors[] = new double[sectors];
for(int i = 0; i<sectors; i++){
x_sectors[i] = Math.cos(i*angle)*radius;
y_sectors[i] = Math.sin(i*angle)*radius;
}
但我一直在想一种方法,如何检查给定坐标在哪个扇区。在
我建议使用Math.atan2方法并改变其角度范围:
See it run with some testcases on ideone.
使用
Math.hypot()
和Math.atan2()
方法通过给定的x和y在极坐标中执行计算:相关问题 更多 >
编程相关推荐