我需要一種將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