android圆坐标计算公式,Android GPS定位轨迹抽稀之道格拉斯-普克(Douglas-Peuker)算法详解...

一、抽稀

通俗点讲,直接举个栗子吧:咱们知道运动轨迹其实是由不少个经纬度坐标链接而成。那么咱们是否须要将全部运动时记录下来的经纬度坐标都用来绘制轨迹呢?实际上是不必的,不少数据实际上是多余的,实际上将这些多余的数据剔除仍然能保证轨迹曲线形状大体不变,并且还能让曲线更平滑更节省存储空间,相似这样的过程咱们就称之为抽稀。抽稀的算法不少,这里将介绍一种经典的算法:道格拉斯-普克(Douglas-Peuker)算法。html

二、道格拉斯-普克(Douglas-Peuker)算法

仍是举个栗子吧,假设在平面坐标系上有一条由N个坐标点组成的曲线,已设定一个阈值epsilon。

(1)首先,将起始点与结束点用直线链接, 再找出到该直线的距离最大,同时又大于阈值epsilon的点并记录下该点的位置(这里暂且称其为最大阈值点),如图所示:git

(2)接着,以该点为分界点,将整条曲线分割成两段(这里暂且称之为左曲线和右曲线),将这两段曲线想象成独立的曲线而后重复操做(1),找出两边的最大阈值点,如图所示:github

(3)最后,重复操做(2)(1)直至再也找不到最大阈值点为止,而后将全部最大阈值点按顺序链接起来即可以获得一条更简化的,更平滑的,与原曲线十分近似的曲线,如图所示:算法

三、如何实现?

OK,终于到代码登场了,不废话,上代码:

Point类:函数

public class Point {

double x;

double y;

public Point(int x, int y) {

this.x = x;

this.y = y;

System.out.print("(" + x + "," + y + ") ");

}

public static Point instance(int x, int y) {

return new Point(x, y);

}

}

DouglasPeuckerUtil 类:this

public class DouglasPeuckerUtil {

public static void main(String[] args) {

System.out.print("原始坐标:");

List points = new ArrayList<>();

List result = new ArrayList<>();

points.add(Point.instance(1, 1));

points.add(Point.instance(2, 2));

points.add(Point.instance(3, 4));

points.add(Point.instance(4, 1));

points.add(Point.instance(5, 0));

points.add(Point.instance(6, 3));

points.add(Point.instance(7, 5));

points.add(Point.instance(8, 2));

points.add(Point.instance(9, 1));

points.add(Point.instance(10, 6));

System.out.println("");

System.out

.println("=====================================================================");

System.out.print("抽稀坐标:");

result = DouglasPeucker(points, 1);

for (Point p : result) {

System.out.print("(" + p.x + "," + p.y + ") ");

}

}

public static List DouglasPeucker(List points, int epsilon) {

// 找到最大阈值点,即操做(1)

double maxH = 0;

int index = 0;

int end = points.size();

for (int i = 1; i < end - 1; i++) {

double h = H(points.get(i), points.get(0), points.get(end - 1));

if (h > maxH) {

maxH = h;

index = i;

}

}

// 若是存在最大阈值点,就进行递归遍历出全部最大阈值点

List result = new ArrayList<>();

if (maxH > epsilon) {

List leftPoints = new ArrayList<>();// 左曲线

List rightPoints = new ArrayList<>();// 右曲线

// 分别提取出左曲线和右曲线的坐标点

for (int i = 0; i < end; i++) {

if (i <= index) {

leftPoints.add(points.get(i));

if (i == index)

rightPoints.add(points.get(i));

} else {

rightPoints.add(points.get(i));

}

}

// 分别保存两边遍历的结果

List leftResult = new ArrayList<>();

List rightResult = new ArrayList<>();

leftResult = DouglasPeucker(leftPoints, epsilon);

rightResult = DouglasPeucker(rightPoints, epsilon);

// 将两边的结果整合

rightResult.remove(0);//移除重复点

leftResult.addAll(rightResult);

result = leftResult;

} else {// 若是不存在最大阈值点则返回当前遍历的子曲线的起始点

result.add(points.get(0));

result.add(points.get(end - 1));

}

return result;

}

/**

* 计算点到直线的距离

*

* @param p

* @param s

* @param e

* @return

*/

public static double H(Point p, Point s, Point e) {

double AB = distance(s, e);

double CB = distance(p, s);

double CA = distance(p, e);

double S = helen(CB, CA, AB);

double H = 2 * S / AB;

return H;

}

/**

* 计算两点之间的距离

*

* @param p1

* @param p2

* @return

*/

public static double distance(Point p1, Point p2) {

double x1 = p1.x;

double y1 = p1.y;

double x2 = p2.x;

double y2 = p2.y;

double xy = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

return xy;

}

/**

* 海伦公式,已知三边求三角形面积

*

* @param cB

* @param cA

* @param aB

* @return 面积

*/

public static double helen(double CB, double CA, double AB) {

double p = (CB + CA + AB) / 2;

double S = Math.sqrt(p * (p - CB) * (p - CA) * (p - AB));

return S;

}

输出结果:spa

OK,平面坐标上的Douglas-Peuker算法已经基本实现了!可是若是换成经纬度呢?其实不用担忧,地图API通常都会提供计算两个经纬度坐标之间距离的函数,因此万变不离其宗,思路仍是同样的,大胆点,代码啪啪啪的敲起来吧!.net

四、参考示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Douglas-Peucker算法可以用C语言实现,以下是其中一个可能的实现: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct { double x; double y; } Point; // 计算p1和p2之间的欧几里得距离 double distance(Point p1, Point p2) { double dx = p1.x - p2.x; double dy = p1.y - p2.y; return sqrt(dx*dx + dy*dy); } // 计算p到线段(p1,p2)的距离 double perpendicularDistance(Point p, Point p1, Point p2) { double area = fabs((p2.x-p1.x)*(p1.y-p.y)-(p1.x-p.x)*(p2.y-p1.y)); double lineLength = distance(p1, p2); return area / lineLength; } // 递归调用Douglas-Peucker算法 void douglasPeucker(Point* points, int start, int end, double epsilon, int* keep) { // 如果只有两个,直接保留 if (end - start == 1) { keep[start] = 1; keep[end] = 1; return; } // 找出距离直线最远的 double maxDistance = 0; int maxIndex = 0; Point p1 = points[start]; Point p2 = points[end]; for (int i = start+1; i < end; i++) { double d = perpendicularDistance(points[i], p1, p2); if (d > maxDistance) { maxDistance = d; maxIndex = i; } } // 如果最远距离大于epsilon,则继续递归,否则保留 if (maxDistance > epsilon) { douglasPeucker(points, start, maxIndex, epsilon, keep); douglasPeucker(points, maxIndex, end, epsilon, keep); } else { for (int i = start; i <= end; i++) { keep[i] = 1; } } } // Douglas-Peucker算法的入口函数 void simplify(Point* points, int n, double epsilon, Point** result, int* m) { // 先分配一个足够大的数组保留结果 int* keep = (int*)malloc(n * sizeof(int)); // 递归调用Douglas-Peucker算法 douglasPeucker(points, 0, n-1, epsilon, keep); // 统计结果 *m = 0; for (int i = 0; i < n; i++) { if (keep[i]) { (*m)++; } } *result = (Point*)malloc((*m) * sizeof(Point)); int j = 0; for (int i = 0; i < n; i++) { if (keep[i]) { (*result)[j++] = points[i]; } } free(keep); } int main() { Point points[] = { {0, 0}, {1, 1}, {2, 0}, {3, 1}, {4, 0}, {5, 1}, {6, 0}, {7, 1}, {8, 0}, {9, 1} }; int n = sizeof(points) / sizeof(Point); Point* simplifiedPoints; int m; simplify(points, n, 0.5, &simplifiedPoints, &m); printf("Original points:\n"); for (int i = 0; i < n; i++) { printf("%.1f, %.1f\n", points[i].x, points[i].y); } printf("Simplified points:\n"); for (int i = 0; i < m; i++) { printf("%.1f, %.1f\n", simplifiedPoints[i].x, simplifiedPoints[i].y); } free(simplifiedPoints); return 0; } ``` 该实现中,首先定义了一个结构体 `Point` 表示二维空间中的一个,然后实现了 `distance` 函数用于计算之间的距离,以及 `perpendicularDistance` 函数用于计算到线段的垂直距离。 接下来是Douglas-Peucker算法的核心部分,实现了一个 `douglasPeucker` 函数用于递归调用算法,并实现了一个 `simplify` 函数作为入口函数。输入参数为原始数组 `points`,数量 `n`,阈值 `epsilon`,输出参数为简化后的数组 `result` 和数量 `m`。算法先分配一个 `keep` 数组用于标记需要保留的,然后调用 `douglasPeucker` 函数进行递归计算。最后统计保留的数量并返回简化后的数组即可。 在 `main` 函数中,我们定义了一个包含10个的测试数组,并调用 `simplify` 函数进行简化,然后打印出原始和简化后的供查看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值