我需要计算2D空间中两点之间的角度。使用直角三角计算它是个好主意吗?请给我用Java代码编写的公式。谢谢!
这是我的代码:
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
double angle = Math.atan2(
pTouchAreaLocalX - boundSpriteCenterX,
boundSpriteCenterY - pTouchAreaLocalY);
angle = Math.toDegrees(angle);
Log.i("egor", "angle " + angle);
return true;
};
当我在精灵周围旋转手指时,这就是我所得到的:
07-21 16:07:00.736: INFO/egor(12600): angle -11.401802094139539
07-21 16:07:00.786: INFO/egor(12600): angle -11.349729213136412
07-21 16:07:00.826: INFO/egor(12600): angle -11.422536712363058
07-21 16:07:00.956: INFO/egor(12600): angle -11.234933754467884
07-21 16:07:00.986: INFO/egor(12600): angle -10.762776313908919
07-21 16:07:01.026: INFO/egor(12600): angle -10.18502866163197
07-21 16:07:01.086: INFO/egor(12600): angle -8.791364033967513
07-21 16:07:01.126: INFO/egor(12600): angle -7.51789397098733
07-21 16:07:01.266: INFO/egor(12600): angle -5.964822509364536
07-21 16:07:01.266: INFO/egor(12600): angle -3.8833834235199327
07-21 16:07:01.316: INFO/egor(12600): angle -3.518004982991794
07-21 16:07:01.316: INFO/egor(12600): angle -3.2335041547308747
07-21 16:07:01.356: INFO/egor(12600): angle -2.8893793853454013
07-21 16:07:01.366: INFO/egor(12600): angle -2.591166578194567
07-21 16:07:01.396: INFO/egor(12600): angle -2.4890755982704613
07-21 16:07:01.426: INFO/egor(12600): angle -2.4041628132172432
07-21 16:07:01.466: INFO/egor(12600): angle -2.606754218788734
07-21 16:07:01.486: INFO/egor(12600): angle -2.9585030905373477
07-21 16:07:01.516: INFO/egor(12600): angle -3.203094764102693
07-21 16:07:01.546: INFO/egor(12600): angle -4.143849229994
07-21 16:07:01.576: INFO/egor(12600): angle -4.833402961115934
07-21 16:07:01.596: INFO/egor(12600): angle -5.201363076921709
07-21 16:07:01.626: INFO/egor(12600): angle -7.182189581196999
07-21 16:07:01.666: INFO/egor(12600): angle -8.30009901770206
07-21 16:07:01.706: INFO/egor(12600): angle -10.305907456087617
07-21 16:07:01.746: INFO/egor(12600): angle -11.845396664651743
07-21 16:07:01.776: INFO/egor(12600): angle -13.486956315730428
07-21 16:07:01.796: INFO/egor(12600): angle -14.928485252180119
07-21 16:07:01.826: INFO/egor(12600): angle -15.930527466185383
07-21 16:07:01.856: INFO/egor(12600): angle -17.523214080867536
07-21 16:07:01.876: INFO/egor(12600): angle -18.670097079838413
07-21 16:07:01.906: INFO/egor(12600): angle -19.784066963586145
07-21 16:07:01.946: INFO/egor(12600): angle -20.967697211428263
07-21 16:07:01.966: INFO/egor(12600): angle -22.839177072269344
07-21 16:07:01.986: INFO/egor(12600): angle -23.995902815112903
07-21 16:07:02.026: INFO/egor(12600): angle -24.94729858380699
07-21 16:07:02.046: INFO/egor(12600): angle -25.824795978977953
07-21 16:07:02.066: INFO/egor(12600): angle -26.163619752371858
07-21 16:07:02.106: INFO/egor(12600): angle -27.036651116353283
07-21 16:07:02.126: INFO/egor(12600): angle -27.392238974828167
07-21 16:07:02.266: INFO/egor(12600): angle -28.076676644069305
07-21 16:07:02.276: INFO/egor(12600): angle -28.49774854622218
07-21 16:07:02.316: INFO/egor(12600): angle -28.408406568602896
07-21 16:07:02.316: INFO/egor(12600): angle -28.323322872571392
07-21 16:07:02.356: INFO/egor(12600): angle -28.24684157963425
07-21 16:07:02.366: INFO/egor(12600): angle -28.115274003009183
07-21 16:07:02.406: INFO/egor(12600): angle -27.97540955255425
07-21 16:07:02.426: INFO/egor(12600): angle -27.947947494965728
07-21 16:07:02.446: INFO/egor(12600): angle -27.908408445343735
07-21 16:07:02.486: INFO/egor(12600): angle -27.621472392453533
07-21 16:07:02.506: INFO/egor(12600): angle -27.31775291690124
07-21 16:07:02.526: INFO/egor(12600): angle -26.84707894871403
07-21 16:07:02.556: INFO/egor(12600): angle -26.573497423338885
07-21 16:07:02.586: INFO/egor(12600): angle -25.553344674326492
07-21 16:07:02.606: INFO/egor(12600): angle -24.681035396816615
07-21 16:07:02.646: INFO/egor(12600): angle -23.164315738891876
07-21 16:07:02.686: INFO/egor(12600): angle -22.001937944957152
07-21 16:07:02.696: INFO/egor(12600): angle -21.108728821882377
07-21 16:07:02.726: INFO/egor(12600): angle -20.777362892583056
07-21 16:07:02.756: INFO/egor(12600): angle -20.331914563591184
07-21 16:07:02.776: INFO/egor(12600): angle -19.842422968714942
07-21 16:07:02.796: INFO/egor(12600): angle -18.943202330016586
07-21 16:07:02.836: INFO/egor(12600): angle -17.900217339627066
07-21 16:07:02.886: INFO/egor(12600): angle -16.38438849186955
07-21 16:07:02.896: INFO/egor(12600): angle -15.49309440885704
07-21 16:07:02.916: INFO/egor(12600): angle -14.663361857753415
07-21 16:07:02.956: INFO/egor(12600): angle -14.06291160310325
07-21 16:07:02.976: INFO/egor(12600): angle -13.743513115207124
07-21 16:07:02.996: INFO/egor(12600): angle -13.436847871994882
07-21 16:07:03.076: INFO/egor(12600): angle -13.436847871994882
答案 0 :(得分:9)
在屏幕坐标(原点位于左上方,Y向下增加)中,如果图像的中心是(x0,y0)而“手指”位于(xa,ya),那么你需要的角度是
Math.atan2(xa-x0,y0-ya)
这将为您提供以弧度表示的角度,您必须从其起始位置顺时针旋转图像。
答案 1 :(得分:0)
我同意Ben的上述解决方案,这里有一些代码在所有4个象限和边缘提供示例
public class CalculateAngle {
public static void main(String[] args) {
System.out.println(angleInDegrees(100, 100, 100, 0)); // directly above centre = 0.0
System.out.println(angleInDegrees(100, 100, 200, 100)); // directly to the right = 90.0
System.out.println(angleInDegrees(100, 100, 0, 100)); // directly to the left = -90.0
System.out.println(angleInDegrees(100, 100, 100, 200)); // directly below centre = 180.0
System.out.println(angleInDegrees(100, 100, 200, 0)); // 45 degrees to right and above = 45.0
System.out.println(angleInDegrees(100, 100, 0, 0)); // 45 degrees to left and above = -45.0
System.out.println(angleInDegrees(100, 100, 0, 200)); // 45 degrees to left and below = -135.0
System.out.println(angleInDegrees(100, 100, 200, 200)); // 45 degrees to the right and below = 135.0
}
public static double angleInDegrees(double centreX, double centreY, double pointX, double pointY) {
return Math.atan2(pointX - centreX, centreY - pointY) * 180.0F / Math.PI;
}
}
只需删除* 180.0F / Math.PI
即可返回以弧度表示的角度,以便在旋转中使用。