道格拉斯佩克算法c语言,曲线近似——道格拉斯-普克算法

a4c26d1e5885305701be709a3d33442f.png

算法的JAVA源程序:

packagecom.mapbar.jts;

publicclassPoint

{

privatedoublex=0;

privatedoubley=0;

privateintindex=0;

publicdoublegetX() {returnx;}

publicvoidsetX(doublex) {this.x=x;}

publicdoublegetY() {returny;}

publicvoidsetY(doubley) {this.y=y;}

publicintgetIndex() {returnindex;}

publicvoidsetIndex(intindex) {this.index=index;}

publicPoint(doublex,doubley,intindex)

{this.x=x;this.y=y;this.index=index;}

}

packagecom.mapbar.jts;

importjava.util.ArrayList;

importjava.util.List;

importcom.vividsolutions.jts.geom.Coordinate;

importcom.vividsolutions.jts.geom.Geometry;

importcom.vividsolutions.jts.io.ParseException;

importcom.vividsolutions.jts.io.WKTReader;

publicclassDouglas

{

publicListpoints=newArrayList();

privatestaticfinaldoubleD=1;

privateWKTReaderreader;

publicGeometrybuildGeo(Stringstr)

{

try

{

if(reader==null) {reader=newWKTReader();}

returnreader.read(str);

}

catch(ParseExceptione)

{thrownewRuntimeException("buildGeometryError",e);}

}

publicvoidreadPoint()

{

Geometryg=buildGeo("LINESTRING(14,23,42,66,77,86,95,1010)");

Coordinate[]coords=g.getCoordinates();

for(inti = 0;i < coords.length; i++)

{Pointp=newPoint(coords[i].x,coords[i].y,i);points.add(p);}

}

publicvoidcompress(Pointfrom,Pointto)

{

booleanswitchvalue=false;

System.out.println(from.getY());

System.out.println(to.getY());

doubleA=(from.getY()-to.getY())/Math.sqrt(Math.pow((from.getY()-to.getY()),2)+Math.pow((from.getX()-to.getX()),2));

doubleB=(to.getX()-from.getX())/Math.sqrt(Math.pow((from.getY()-to.getY()),2)+Math.pow((from.getX()-to.getX()),2));

doubleC=(from.getX()*to.getY()-to.getX()*from.getY())/Math.sqrt(Math.pow((from.getY()-to.getY()),2)+Math.pow((from.getX()-to.getX()),2));

doubled=0;

doubledmax=0;

intm=points.indexOf(from);

intn=points.indexOf(to);

if(n == m+1) return;

Pointmiddle=null;

Listdistance=newArrayList();

for(inti = m+1;i < n;++)

{

d=Math.abs(A*(points.get(i).getX())+B*(points.get(i).getY())+C)/Math.sqrt(Math.pow(A,2)+Math.pow(B,2));

distance.add(d);

}

dmax=distance.get(0);

for(intj = 1;j < distance.size();j++)

{

if(distance.get(j) > dmax) dmax=distance.get(j);

}

if(dmax>D) switchvalue=true;

else switchvalue=false;

if(!switchvalue)

{

//删除Points(m,n)内的坐标

for(inti = m+1;i < n;i++)

points.get(i).setIndex(-1);

}

else

{

for(inti = m+1;i < n;i++)

{

if((Math.abs(A*(points.get(i).getX())+B*(points.get(i).getY())+C)/Math.sqrt(Math.pow(A,2)+Math.pow(B,2))==dmax))

middle=points.get(i);

}

compress(from,middle);

compress(middle,to);

}

}

publicstaticvoidmain(String[]args)

{

Douglasd=newDouglas();

d.readPoint();

d.compress(d.points.get(0),d.points.get(d.points.size()-1));

for(inti = 0;i < d.points.size();i++)

{

Pointp=d.points.get(i);

if(p.getIndex()>-1){System.out.print(p.getX()+""+p.getY()+",");}}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值