直角坐标系用三角函数判断旋转方向和旋转角度

时间:2023-01-22 10:23:18

坐标原点知道

两个点知道

相对于坐标原点的旋转的角度可以通过三角函数算出来

因为知道了三个边的长度,

Math.acos((bb*bb+cc*cc-aa*aa)/(2*bb*cc))
主要就是这个方法
至于旋转x1*y2-x2*y1大于0就是顺时针,小于0就是逆时针,考虑原点的位置
 1 public class DirectionClass {
 2 
 3     public double x0=LambdaTest01.InterSectionLat;
 4     public double y0=LambdaTest01.InterSectionLng;
 5     
 6     //给定两个点的方向,判断怎么转向的
 7     //首先确定大方向就是用路口的定点作为基准点,两个点作为偏置点,制作到一个三角形然后用三个边判断两个点的角度。关键是如何判断顺时针还是逆时针。
 8     public static double directionTurned(double x1,double y1,double x2,double y2) {
 9         double result=0;
10         double bb=lengthConvert.getDistance(x1, y1, LambdaTest01.InterSectionLat, LambdaTest01.InterSectionLng);
11         double cc=lengthConvert.getDistance(x2, y2, LambdaTest01.InterSectionLat, LambdaTest01.InterSectionLng);
12         double aa=lengthConvert.getDistance(x1, y1, x2, y2);
13         result=Math.acos((bb*bb+cc*cc-aa*aa)/(2*bb*cc));
14         double fix=180/Math.PI;
15         return result*fix;
16     }
17     public static double directionTurned_bd(double x1,double y1,double x2,double y2) {
18         double result=0;
19         double bb=lengthConvert.getDistance(x1, y1, LambdaTest01.testInterSectionLat, LambdaTest01.testInterSectionLng);
20         double cc=lengthConvert.getDistance(x2, y2, LambdaTest01.testInterSectionLat, LambdaTest01.testInterSectionLng);
21         double aa=lengthConvert.getDistance(x1, y1, x2, y2);
22         result=Math.acos((bb*bb+cc*cc-aa*aa)/(2*bb*cc));
23         double fix=180/Math.PI;
24         return result*fix;
25     }
26     public static double directionTurned2(double x1,double y1,double x2,double y2) {
27         double result=0;
28         double bb=lengthConvert.getDistance2(x1, y1, 0, 0);
29         double cc=lengthConvert.getDistance2(x2, y2, 0, 0);
30         double aa=lengthConvert.getDistance2(x1, y1, x2, y2);
31         System.out.println(bb);
32         System.out.println(cc);
33         System.out.println(aa);
34         double fix=180/Math.PI;
35         result=Math.acos((bb*bb+cc*cc-aa*aa)/(2*bb*cc));
36         return result*fix;
37     }
38     public static boolean wise_anti(double x1,double y1,double x2,double y2) {
39         double result=(x1-LambdaTest01.InterSectionLat)*(y2-LambdaTest01.InterSectionLng)-(x2-LambdaTest01.InterSectionLat)*(y1-LambdaTest01.InterSectionLng);
40         if (result>=0) {
41             System.out.println("顺时针");
42             return true;
43         }else {
44             System.out.println("逆时针");
45             return false;    
46         }
47     }
48     public static boolean wise_anti_bd(double x1,double y1,double x2,double y2) {
49         double result=(x1-LambdaTest01.testInterSectionLat)*(y2-LambdaTest01.testInterSectionLng)-(x2-LambdaTest01.testInterSectionLat)*(y1-LambdaTest01.testInterSectionLng);
50         if (result>=0) {
51             System.out.println("顺时针");
52             return true;
53         }else {
54             System.out.println("逆时针");
55             return false;    
56         }
57     }
58     
59     
60     public static void main(String[] args) {
61         // TODO Auto-generated method stub
62 
63         double result=directionTurned2(1, 0, 0, 1);
64         System.out.println(result);
65     }
66 
67 }