這裏有幾個答案試圖解釋「屏幕」問題,其中top left
是0,0
和bottom right
是(正數)screen width, screen height
。大多數電網的Y
軸爲X
以上不低於。
以下方法將使用屏幕值而不是「網格」值。與例外的答案唯一的區別是Y
的值是倒置的。
/**
* Work out the angle from the x horizontal winding anti-clockwise
* in screen space.
*
* The value returned from the following should be 315.
* <pre>
* x,y -------------
* | 1,1
* | \
* | \
* | 2,2
* </pre>
* @param p1
* @param p2
* @return - a double from 0 to 360
*/
public static double angleOf(PointF p1, PointF p2) {
// NOTE: Remember that most math has the Y axis as positive above the X.
// However, for screens we have Y as positive below. For this reason,
// the Y values are inverted to get the expected results.
final double deltaY = (p1.y - p2.y);
final double deltaX = (p2.x - p1.x);
final double result = Math.toDegrees(Math.atan2(deltaY, deltaX));
return (result < 0) ? (360d + result) : result;
}
技術上你不能得到兩個*點*之間的角度。雖然你可以得到兩個*向量*之間的角度。 – ChrisF 2010-04-20 16:13:23
很確定他的意思是「兩點之間畫出的線條與水平軸線之間的角度」 – 2010-04-20 16:23:03
對不起,讓我改述我的標題,如何獲得由這兩點定義的線與水平線之間的角度通過我的對象在屏幕中心切入? – kingrichard2005 2010-04-20 16:24:29