Runt
2021-01-04 0e8ecae4a1140c5e23475bbd589f591208e9ed65
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
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:"+getLengthOfHypotenuse(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 getLengthOfHypotenuse(double a,double b){
        return Math.sqrt(a*a+b*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);
    }
 
 
}