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);
|
}
|
|
|
}
|