00001 #include <iostream>
00002 #include <math.h>
00003
00004 using namespace std;
00005
00006 struct Point{
00007 double x, y;
00008 };
00009
00010 double dPointToLine(Point L1, Point L2, Point P){
00011 double dist;
00012 dist = (L1.y-L2.y)*P.x + (L2.x-L1.x)*P.y + (L1.x*L2.y - L2.x*L1.y);
00013 dist = dist / sqrt(pow((L2.x-L1.x),2) + pow((L2.y-L1.y),2));
00014 if(L1.x==L2.x && L1.y==L2.y)
00015 dist = sqrt(pow((L1.x-P.x),2) + pow((L1.y-P.y),2));
00016 return (dist>0?dist:(-dist));
00017 }
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 void simplifyPolyline(Point *poly, int start, int end, double tolerance, int *marker){
00050
00051 if (end < start+1)
00052 return;
00053
00054
00055
00056
00057 int maxIndex = start;
00058 double maxDistance = 0;
00059
00060 for (int i=start+1; i<end; i++){
00061 double dist = dPointToLine(poly[start], poly[end], poly[i]);
00062 if(dist > maxDistance){
00063 maxDistance = dist;
00064 maxIndex = i;
00065 }
00066 }
00067
00068 if(maxDistance>tolerance){
00069
00070 marker[maxIndex]=1;
00071
00072
00073
00074 simplifyPolyline(poly, start, maxIndex, tolerance, marker);
00075 simplifyPolyline(poly, maxIndex, end, tolerance, marker);
00076 }
00077 return;
00078 }
00079