我需要一种将X和Y坐标转换为角度的方法。我有这样的代码,但我需要这个布局:将X,Y转换为角度
180
90 270
360
private double getAngle(double xTouch, double yTouch) {
double x = xTouch - (slideBtn.getWidth()/2d);
double y = slideBtn.getHeight() - yTouch - (slideBtn.getHeight()/2d);
switch (getQuadrant(x, y)) {
case 1:
return Math.asin(y/Math.hypot(x, y)) * 180/Math.PI;
case 2:
return 180 - Math.asin(y/Math.hypot(x, y)) * 180/Math.PI;
case 3:
return 180 + (-1 * Math.asin(y/Math.hypot(x, y)) * 180/Math.PI);
case 4:
return 360 + Math.asin(y/Math.hypot(x, y)) * 180/Math.PI;
default:
return 0;
}
}
/**
* @return The selected quadrant.
*/
private static int getQuadrant(double x, double y) {
if (x >= 0) {
return y >= 0 ? 1 : 4;
} else {
return y >= 0 ? 2 : 3;
}
}
编辑,澄清问题:我的问题是,我得到了错误的角度x,y角度。例如:0/360度是圆的顶边,180度是圆的底边。我试图使用上面的代码修复它(使用不同的象限,但那不起作用)。接受的答案为我做了诀窍。
当我做的方法getAngle()我得到了错误的角度布局。所以我需要上面指定的角度布局。 atan2没有我想要的东西...顶部180度而不是90度。所以旋转整个角度90度,但我不知道该怎么做 – matthijs2704
当你得到atan2的结果时,你应该只是添加PI/2逆时针旋转角度90度,如果这是你的问题。但问题是我没有得到它在你的布局0度,角度增加顺时针或逆时针? –
0度与360度相同,但我会尝试atan2!谢谢,我会在工作时接受你的答案 – matthijs2704