nilupeng
2022-01-13 e3bd13f6f12aae8a01a61d3368373fa9636549eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.demo.navtogether.utils;
 
 
import android.graphics.Point;
 
/**
 * My father is Object, ites purpose of 三角函数算法
 *
 * @purpose Created by Runt (qingingrunt2010@qq.com) on 2020-12-23.
 */
 
public class TriangleUtils {
 
    public static void main(String[] args) {
        Point pA = new Point(0,11),pB = new Point(11,30),pC = new Point(30,30);
        double a = getLengthOfSide(pA,pB),b = getLengthOfSide(pC,pB),c = getLengthOfSide(pA,pC);
        System.out.println(String.format("边长 a:%s,b:%s,c:%s",a,b,c));
        System.out.println(String.format("角度 坐标计算:a:%s,b:%s,c:%s",getDegrees(pA,pB,pC),getDegrees(pB,pC,pA),getDegrees(pC,pA,pB)));
        System.out.println(String.format("角度 转换为边长计算:a:%s,b:%s,c:%s",getDegrees2(pA,pB,pC),getDegrees2(pB,pC,pA),getDegrees2(pC,pA,pB)));
        System.out.println(String.format("角度 边长计算:a:%s,b:%s,c:%s",getDegrees(a,b,c),getDegrees(b,c,a),getDegrees(c,a,b)));
        System.out.println("边长 c:"+getHypotenuseFromLength(a,b ));
        System.out.println("边长 c:"+getLengthOfSide(a,b ,getDegrees2(pA,pB,pC)));
        System.out.format("%s 度的余弦值为 %s", Math.toRadians(90), Math.cos(Math.toRadians(90)));
    }
 
 
    /************************直角三角算法*******************************/
 
    /**
     * 直角三角获取斜边
     * @param a 直角边长
     * @param b 直角边长
     * @return 斜边边长
     */
    public static Double getHypotenuseFromLength(double a,double b){
        return Math.sqrt(a*a+b*b);
    }
 
    /**
     * 直角三角形 根据角度和直角边长求斜边
     * @param degree    角度
     * @param width     直角边长
     * @return 斜边边长
     */
    public static double getHypotenuseFromDegree(double degree,double width){
        double cos = Math.cos(Math.toRadians(degree));//余弦
        return width / cos;
    }
 
    /**
     * 直角三角形 根据角度和斜边求直角边
     * @param degree    角度
     * @param width     斜边
     * @return 斜边边长
     */
    public static double getRightSideFromDegree(double degree,double width){
        double cos = Math.cos(Math.toRadians(degree));
        return width * cos;
    }
 
    /**
     * 直角三角形 根据直角边和斜边求直角边
     * @param a 直角边
     * @param b 斜边
     * @return 直角边长
     */
    public static double getRightSideFromLength(double a,double b){
        return Math.sqrt(b*b - a*a);
    }
 
 
 
    /************************end 直角三角算法*******************************/
 
    /**
     * 根据 B角和C角的a边长,获取A角-C角的b边长
     * @param A
     * @param B
     * @param a
     * @return
     */
    public static double getSideFromSideAndDegree(double A,double B,double a){
        // a÷sin A == b ÷ sin B
        return a/Math.sin(Math.toRadians(A))*Math.sin(Math.toRadians(B));
    }
 
    /**
     * 获取对边
     * @param a 边长
     * @param b 边长
     * @param degrees 角度
     * @return
     */
    public static Double getLengthOfSide(double a,double b,double degrees){
        System.out.println(String.format("a:%s,b:%s,degrees:%s",a,b,degrees));
        return Math.sqrt(b*b+a*a-2*a*b*Math.cos(Math.toRadians(degrees)));
    }
 
    /**
     * 已知边长  求AC 角度
     * @param a
     * @param b
     * @param c
     * @return
     */
    public static double getDegrees(double a,double b,double c){
        // 计算弧度表示的角
        double degrees = Math.acos((a*a +  b*b -c*c)/(2.0*a*b));
        // 用角度表示的角
        return  Math.toDegrees(degrees);
    }
 
    /**
     * 两点之间的长度
     * @param point0 坐标
     * @param point1 坐标
     * @return
     */
    public static double getLengthOfSide(Point point0, Point point1){
 
        return Math.sqrt(Math.pow((point0.x-point1.x),2)+Math.pow((point0.y-point1.y),2));
 
    }
 
 
    /**
     * 根据坐标 获取角度
     * @param point0  坐标
     * @param degreesPoint  坐标  获取该点的角度
     * @param point2  坐标
     * @return
     */
    public static int getDegrees(Point point0, Point degreesPoint, Point point2) {
        //向量的点乘
        int vector = (point0.x - degreesPoint.x) * (point2.x - degreesPoint.x) + (point0.y - degreesPoint.y) * (point2.y - degreesPoint.y);
        //向量的模乘
        double sqrt = Math.sqrt(
                (Math.abs((point0.x - degreesPoint.x) * (point0.x - degreesPoint.x)) + Math.abs((point0.y - degreesPoint.y) * (point0.y - degreesPoint.y)))
                        * (Math.abs((point2.x - degreesPoint.x) * (point2.x - degreesPoint.x)) + Math.abs((point2.y - degreesPoint.y) * (point2.y - degreesPoint.y)))
        );
        //反余弦计算弧度
        double radian = Math.acos(vector / sqrt);
        //弧度转角度制
        return (int) (180 * radian / Math.PI);
    }
 
    /**
     * 根据坐标 获取角度
     * @param pA  坐标
     * @param degreesPoint  坐标  获取该点的角度
     * @param pC  坐标
     * @return
     */
    public static  double getDegrees2(Point pA, Point degreesPoint, Point pC) {
        double a = getLengthOfSide(pA,degreesPoint),b = getLengthOfSide(pC,degreesPoint),c = getLengthOfSide(pA,pC);
        return getDegrees(a,b,c);
    }
 
 
}