[opt3.0]下沉工具类到mogo-core-utils模块
This commit is contained in:
@@ -1,156 +0,0 @@
|
||||
package com.mogo.commons.utils;
|
||||
|
||||
import static java.lang.Math.PI;
|
||||
|
||||
import android.util.Pair;
|
||||
|
||||
/**
|
||||
* author : donghongyu
|
||||
* e-mail : 1358506549@qq.com
|
||||
* date : 2020/4/14 1:02 PM
|
||||
* desc : 计算车辆驾驶方向的工具类
|
||||
* version: 1.0
|
||||
*/
|
||||
public class DrivingDirectionUtils {
|
||||
|
||||
public static long getDegreeOfCar2Poi2(double x1, double y1, double x2, double y2, double x1_angle) {
|
||||
Pair<Double, Double> newPoint = calculateNewPoint(x1, y1, x1_angle, 10);
|
||||
if (newPoint != null) {
|
||||
double angle = getAngle(x1, y1, newPoint.first, newPoint.second, x2, y2);
|
||||
return Math.round(angle + 0.5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static double distance(double x1, double y1, double x2, double y2) {
|
||||
return Math.sqrt(Math.pow(x1 - x2, 2.0) + Math.pow(y1 - y2, 2.0));
|
||||
}
|
||||
|
||||
public static double getAngle(double sx, double sy, double x1, double y1, double x2, double y2) {
|
||||
x1 = x1 - sx;
|
||||
y1 = y1 - sy;
|
||||
x2 = x2 - sx;
|
||||
y2 = y2 - sy;
|
||||
double product = x1 * x2 + y1 * y2;
|
||||
double radians = Math.acos(product / (Math.sqrt(Math.pow(x1, 2.0) + Math.pow(y1, 2.0)) * Math.sqrt(Math.pow(x2, 2.0) + Math.pow(y2, 2.0))));
|
||||
return Math.toDegrees(radians);
|
||||
}
|
||||
|
||||
public static Pair<Double, Double> calculateNewPoint(double x, double y, double angle, double distance) {
|
||||
if (distance == 0) {
|
||||
return null;
|
||||
}
|
||||
double radian = Math.toRadians(angle);
|
||||
double radianCandle = Math.toRadians(90.0 - angle);
|
||||
double nX = x + distance * Math.sin(radian) / 100000.0;
|
||||
double nY = y + distance * Math.sin(radianCandle) / 100000.0;
|
||||
return Pair.create(nX, nY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算车辆行驶方向 与 poi点到车辆的连线 间的夹角
|
||||
*
|
||||
* @param carLon 车辆位置 lon
|
||||
* @param carLat 车辆位置 lat
|
||||
* @param poiLon poi 位置 lon
|
||||
* @param poiLat poi 位置 lat
|
||||
* @param carAngle 车辆行驶方向
|
||||
* @return
|
||||
*/
|
||||
public static int getDegreeOfCar2Poi(double carLon, double carLat, double poiLon, double poiLat, int carAngle) {
|
||||
int poiAngle = 0;
|
||||
// 以子午线作为y轴 计算两点的余切 再将余切值转化为角度
|
||||
double _angle = Math.atan2(Math.abs(carLon - poiLon), Math.abs(carLat - poiLat)) * (180 / PI);
|
||||
if (poiLon > carLon) {
|
||||
// poi 在 车辆位置的第1象限
|
||||
if (poiLat > carLat) {
|
||||
poiAngle = (int) _angle;
|
||||
}
|
||||
// poi 在 车辆位置的第2象限
|
||||
else {
|
||||
poiAngle = 180 - (int) _angle;
|
||||
}
|
||||
} else {
|
||||
// poi 在 车辆位置的第3象限
|
||||
if (poiLat < carLat) {
|
||||
poiAngle = (int) _angle + 180;
|
||||
}
|
||||
// poi 在 车辆位置的第4象限
|
||||
else {
|
||||
poiAngle = 360 - (int) _angle;
|
||||
}
|
||||
}
|
||||
return calculationAngle(poiAngle, carAngle);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个行驶方向间的夹角 计算结果小于180度
|
||||
*
|
||||
* @param angle0
|
||||
* @param angle1
|
||||
* @return
|
||||
*/
|
||||
public static int calculationAngle(int angle0, int angle1) {
|
||||
// 获取两方向间夹角
|
||||
int angle = Math.abs(angle0 - angle1);
|
||||
if (angle > 180) {
|
||||
int minAngle = Math.min(angle0, angle1);
|
||||
int maxAngle = Math.max(angle0, angle1);
|
||||
return 180 - Math.abs(minAngle + 180 - maxAngle);
|
||||
} else {
|
||||
return angle;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算车辆行驶方向角度,起点&终点经纬度
|
||||
*
|
||||
* @param carLat 车辆位置 lat
|
||||
* @param carLon 车辆位置 lon
|
||||
* @param poiLat poi 位置 lat
|
||||
* @param poiLon poi 位置 lon
|
||||
*/
|
||||
public static int getCarAngle(double carLat, double carLon, double poiLat, double poiLon) {
|
||||
int poiAngle = 0;
|
||||
// 以子午线作为y轴 计算两点的余切 再将余切值转化为角度
|
||||
double _angle = Math.atan2(Math.abs(carLon - poiLon), Math.abs(carLat - poiLat)) * (180 / PI);
|
||||
|
||||
if (poiLon > carLon) {
|
||||
// poi 在 车辆位置的第1象限
|
||||
if (poiLat > carLat) {
|
||||
poiAngle = (int) _angle;
|
||||
}
|
||||
// poi 在 车辆位置的第2象限
|
||||
else {
|
||||
poiAngle = 180 - (int) _angle;
|
||||
}
|
||||
} else {
|
||||
// poi 在 车辆位置的第3象限
|
||||
if (poiLat < carLat) {
|
||||
poiAngle = (int) _angle + 180;
|
||||
}
|
||||
// poi 在 车辆位置的第4象限
|
||||
else {
|
||||
poiAngle = 360 - (int) _angle;
|
||||
}
|
||||
}
|
||||
|
||||
if (poiAngle >= 355) {
|
||||
poiAngle = 0;
|
||||
}
|
||||
return poiAngle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算连两个角度差值
|
||||
*
|
||||
* @param angle1 角度1
|
||||
* @param angle2 角度2
|
||||
* @return 差值
|
||||
*/
|
||||
public static double getAngleDiff(double angle1, double angle2) {
|
||||
// 两个角度差值较小
|
||||
return 180 - Math.abs(Math.abs(angle1 - angle2) - 180);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user