算法的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()+",");}}
}
}