道格拉斯-普克算法

项目中用到,记录下来。


import java.util.ArrayList;
import java.util.List;

import com.vividsolutions.jts.geom.*;

/**
* 道格拉斯普克抽希
*/
public class Douglas {
private static GeometryFactory factory = new GeometryFactory();

/**
* 对矢量曲线进行压缩
*/
public static LineString compress(LineString line, double precision) {
if(line.getNumPoints() < 3)
return line;

List<Coordinate> points = compress(line, 0, line.getNumPoints()-1, precision);

Coordinate[] coordinates = new Coordinate[points.size()];
points.toArray(coordinates);

return factory.createLineString(coordinates);
}

/**
*
* */
public static List<Coordinate> compress(Coordinate[] coordinates,int fromIndex,int toIndex,double precision){


List<Coordinate> sparse_pts = new ArrayList<>();

Coordinate from = coordinates[fromIndex];
Coordinate to = coordinates[toIndex];

if ((fromIndex + 1) == toIndex) {
sparse_pts.add(from);
sparse_pts.add(to);
return sparse_pts;
}

/**
* 由起始点和终止点构成的直线方程一般式的系数
*/
double A = (from.y - to.y)
/ Math.sqrt(Math.pow((from.y - to.y), 2)
+ Math.pow((from.x - to.x), 2));

/**
* 由起始点和终止点构成的直线方程一般式的系数
*/
double B = (to.x - from.x)
/ Math.sqrt(Math.pow((from.y - to.y), 2)
+ Math.pow((from.x - to.x), 2));

/**
* 由起始点和终止点构成的直线方程一般式的系数
*/
double C = (from.x * to.y - to.x * from.y)
/ Math.sqrt(Math.pow((from.y - to.y), 2)
+ Math.pow((from.x - to.x), 2));


int middleIndex = 0;
double dmax = 0;

for (int i = fromIndex + 1; i < toIndex; i++) {
double dis = Math.abs(A * (coordinates[i].x) + B * (coordinates[i].y) + C)
/ Math.sqrt(Math.pow(A, 2)
+ Math.pow(B, 2));

if(dis < dmax)
continue;

dmax = dis;
middleIndex = i;
}

sparse_pts.add(from);

if (dmax > precision) {
List<Coordinate> fronts = compress(coordinates, fromIndex, middleIndex, precision);
fronts.remove(0);
fronts.remove(fronts.size()-1);
sparse_pts.addAll(fronts);

List<Coordinate> backs = compress(coordinates, middleIndex, toIndex, precision);
backs.remove(backs.size()-1);
sparse_pts.addAll(backs);
}

sparse_pts.add(to);

return sparse_pts;
}

private static List<Coordinate> compress(LineString line, int fromIndex, int toIndex, double precision){
List<Coordinate> sparse_pts = new ArrayList<>();

Coordinate from = line.getCoordinateN(fromIndex);
Coordinate to = line.getCoordinateN(toIndex);

if ((fromIndex + 1) == toIndex) {
sparse_pts.add(from);
sparse_pts.add(to);
return sparse_pts;
}

/**
* 由起始点和终止点构成的直线方程一般式的系数
*/
double A = (from.y - to.y)
/ Math.sqrt(Math.pow((from.y - to.y), 2)
+ Math.pow((from.x - to.x), 2));

/**
* 由起始点和终止点构成的直线方程一般式的系数
*/
double B = (to.x - from.x)
/ Math.sqrt(Math.pow((from.y - to.y), 2)
+ Math.pow((from.x - to.x), 2));

/**
* 由起始点和终止点构成的直线方程一般式的系数
*/
double C = (from.x * to.y - to.x * from.y)
/ Math.sqrt(Math.pow((from.y - to.y), 2)
+ Math.pow((from.x - to.x), 2));


int middleIndex = 0;
double dmax = 0;

for (int i = fromIndex + 1; i < toIndex; i++) {
double dis = Math.abs(A * (line.getCoordinateN(i).x) + B * (line.getCoordinateN(i).y) + C)
/ Math.sqrt(Math.pow(A, 2)
+ Math.pow(B, 2));

if(dis < dmax)
continue;

dmax = dis;
middleIndex = i;
}

sparse_pts.add(from);

if (dmax > precision) {
List<Coordinate> fronts = compress(line, fromIndex, middleIndex, precision);
fronts.remove(0);
fronts.remove(fronts.size()-1);
sparse_pts.addAll(fronts);

List<Coordinate> backs = compress(line, middleIndex, toIndex, precision);
backs.remove(backs.size()-1);
sparse_pts.addAll(backs);
}

sparse_pts.add(to);

return sparse_pts;
}
}


关注微信号 codingworld 码农大世界 获取更多信息和作者互动问答。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值