核心算法工具类

* <h1>轨迹平滑所需要的工具方法</h1> Date: 2016-10-27 Created by mxdl
*/
public class TrackMoveUtil {
private static double DISTANCE = 0.0001;

/**
* 根据两点算斜率
*/
public static double getSlope(LatLng fromPoint, LatLng toPoint) {
if (fromPoint == null || toPoint == null) {
return 0;
}
if (toPoint.longitude == fromPoint.longitude) {
return Double.MAX_VALUE;
}
double slope =
((toPoint.latitude - fromPoint.latitude) / (toPoint.longitude - fromPoint.longitude));
return slope;

}

/**
* 根据两点算取图标转的角度
*/
public static double getAngle(LatLng fromPoint, LatLng toPoint) {
if (fromPoint == null || toPoint == null) {
return 0;
}
double slope = getSlope(fromPoint, toPoint);
if (slope == Double.MAX_VALUE) {
if (toPoint.latitude > fromPoint.latitude) {
return 0;
} else {
return 180;
}
}
float deltAngle = 0;
if ((toPoint.latitude - fromPoint.latitude) * slope < 0) {
deltAngle = 180;
}
double radio = Math.atan(slope);
double angle = 180 * (radio / Math.PI) + deltAngle - 90;
return angle;
}

/**
* 根据点和斜率算取截距
*/
public static double getInterception(double slope, LatLng point) {
if (point == null) {
return 0;
}
return point.latitude - slope * point.longitude;
}

/**
* 计算x方向每次移动的距离
*/
public static double getXMoveDistance(double slope) {
if (slope == Double.MAX_VALUE) {
return DISTANCE;
}
return Math.abs((DISTANCE * slope) / Math.sqrt(1 + slope * slope));
}

/**
* 根据轨迹线段计算小车走了多少步
*
* @param latLngList
* @return
*/
public static int getStep(List<LatLng> latLngList) {
int step = 0;
if (latLngList != null && latLngList.size() > 1) {
for (int i = 0; i < latLngList.size() - 1; i++) {
try {
LatLng startPoint = latLngList.get(i);
LatLng endPoint = latLngList.get(i + 1);
double slope = getSlope(startPoint, endPoint);
// 是不是正向的标示(向上设为正向)
boolean isReverse = (startPoint.latitude > endPoint.latitude);
double xMoveDistance = isReverse ? getXMoveDistance(slope) : -1 * getXMoveDistance(slope);
// 应该对经纬度同时处理
for (double j = startPoint.latitude; !((j >= endPoint.latitude) ^ isReverse); j =
j - xMoveDistance) {
step++;
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
return step;
}

/**
* 根据总距离和步数计算运动时间
*
* @param distance
* @param step
* @return
*/
public static double getMoveTime(float distance, int step) {
double timeInterval = 0;
if (distance > 0) {
float totalDistance = distance * 1000;
if (totalDistance <= 500) {
timeInterval = 1000.0 / step;
} else if (totalDistance > 500 && totalDistance <= 7500) {
timeInterval = 2.0 * totalDistance / step;
} else {
timeInterval = 15000.0 / step;
}
}
return timeInterval;
}

/**
* 根据轨迹点集合计算总距离
*
* @param latLngList
* @return
*/
public static float getDistance(List<LatLng> latLngList) {
float distance = 0;
if (latLngList != null && latLngList.size() > 1) {
for (int i = 0; i < latLngList.size() - 1; i++) {
try {
distance += AMapUtils.calculateLineDistance(latLngList.get(i), latLngList.get(i + 1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
return distance;
}

// latitude - 地点的纬度,在-90 与90 之间的double 型数值。
// longitude - 地点的经度,在-180 与180 之间的double 型数值。
/**
* 根据一个经纬度字符串求一个经纬度集合a|b|c|d;
*
* @param latlonStr
* @return
*/
public static List<LatLng> getListLatLng(String latlonStr) {
if (!TextUtils.isEmpty(latlonStr)) {
String[] trackArr = latlonStr.split("\\|");
if (trackArr != null && trackArr.length > 0) {
List<LatLng> latLngList = new ArrayList<LatLng>();
for (int i = 0; i < trackArr.length - 1; i = i + 2) {
try {
String lat = trackArr[i + 1];
String lng = trackArr[i];
// Logger.v(TAG,"trackArr index:" + i);
// Logger.v(TAG,"trackArr lat:" + lat);
// Logger.v(TAG,"trackArr lng:" + lng);
if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lng)) {
Double dLat = Double.valueOf(lat);
Double dLng = Double.valueOf(lng);
if (dLat >= -90 && dLat <= 90 && dLng >= -180 && dLng <= 180
&& !(dLat == 0 && dLng == 0)) {
LatLng latLng = new LatLng(dLat, dLng);
latLngList.add(latLng);
}
}
} catch (Exception e) {
e.printStackTrace(http://www.amjmh.com/v/BIBRGZ_558768/);
}
}
return latLngList;
}
}
return null;
}
}

原文地址:https://www.cnblogs.com/ly570/p/11369965.html

时间: 2024-08-30 04:46:36

核心算法工具类的相关文章

排序算法工具类

/** * 排序算法工具类 */ public class GeneratedArray { /** * * 生成随机长度数组[min,max) * * @param min 最小值 * @param max 最大值 * @param num * @return */ public static int[] randomGeneratedArray(int min, int max, int num) { //断言判断 assert max>min:"数组生成范围指定有误"; i

测试排序算法工具类

为了方便测试排序算法,下面展示了一些工具类,在之后的写的排序算法中会用到这些类. 一.随机数组生成类 package sort.util; import java.util.*; public class RandomArrayGenerator { private static int[] array; //校验number是否在数组中 private static boolean isInArray(int number) { boolean isInArray = false; for(i

非对称加密——RSA算法工具类

关于RSA算法的介绍网上一大堆,一句话就是牛B. package com.demo; import org.springframework.util.StringUtils; import javax.crypto.Cipher; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.secur

非专业码农 JAVA学习笔记 6java工具类和算法-string

续<非专业码农 JAVA学习笔记 5 java工具类和算法> 五.字符串string 字符串和字符的差别:字符串双引号括起来”n”,字符用单引号括起来,表示一种符号’\n’ 1.string的主要方法和属性 类 方法或者属性 备注 定义string Stirng s=new string(“值”),string s=”值” 属性 string.length:string的长度为字节 方法startswith,endswith s.startwith(“值”)-以值为开头,s.endswith(

封装各种生成唯一性ID算法的工具类

/** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.minxinloan.common.utils; import java.security.SecureRandom; import java.util.UUID; /** * 封装各种生成唯一性ID算法的工具类. * @aut

Hibernate工具类_抽取重复核心代码

问题:在Hibernate中每次执行一次操作总是需要加载核心配置文件,获取连接池等等都是重复动作,所以抽取出来 解决: package com.xxx.utils; /** *Hibernate的工具类 *@author cxh */ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg

学习StringUtils工具类—核心API

在JAVA中我们用的最多的类应该就是String了.对于String的处理说简单也简单,但是有的时候要自己去实现一些功能还是要浪费一点时间的.一年之前接触了StringUtils这个工具类,就猛然爱上了它,日复一日心里始终觉得这东西实在太好了.不敢独享,所以决定要总结一下个人使用StringUtils的一些心得. 1.StringUtils.isEmpty(String str)  经常需要去判断一个字符串是否为空,null,"".使用该方法可以轻松得到结论: StringUtils.

ThinkPHP Http工具类(用于远程采集 远程下载) phpSimpleHtmlDom采集类库_Jquery筛选方式 使用phpQuery轻松采集网页内容

[php]代码库 view sourceprint? <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 200

【Android】详解7.0带来的新工具类:DiffUtil

一 概述 DiffUtil是support-v7:24.2.0中的新工具类,它用来比较两个数据集,寻找出旧数据集->新数据集的最小变化量. 说到数据集,相信大家知道它是和谁相关的了,就是我的最爱,RecyclerView. 就我使用的这几天来看,它最大的用处就是在RecyclerView刷新时,不再无脑mAdapter.notifyDataSetChanged(). 以前无脑mAdapter.notifyDataSetChanged()有两个缺点: 不会触发RecyclerView的动画(删除.