Http://www.cnblogs.com/xdlwd086/p/5100425.html
The seniors made up the Java version, so in reference to the idea of seniors, based on the implementation of C + + to share.
#include <stdio.h>//define input/Output functions # include <stdlib.h>//define miscellaneous functions and memory allocation functions # include <ctime> #include <CST dlib> #include <string> #include <sstream> #include <iostream> #include <fstream> #include <iomanip> #include <cmath>using namespace std, #define M_PI 3.14159265358979323846#define DMax 30.0long Double Gps[3150][2];int gps_visit[3150] = {0};/*the Code of Geodistance function:Input:Two Coordination {LATITUDE1, Longi Tude1, Latitude2, Longitude2} (type:double) output:distance (unit:m) (type:double) */long double Rad (long double D) { return d * m_pi/180.0;} Longitude longitude latitude Latitudelong double geodist (long double lon1, long double lat1, long double lon2, long double lat 2) {long double radLat1 = Rad (LAT1); Long double radLat2 = Rad (LAT2); Long double Delta_lon = Rad (Lon2-lon1); Long double top_1 = cos (radLat2) * sin (Delta_lon); Long double top_2 = cos (radLat1) * sin (radLat2)-Sin (radlat1) * COS (RADLAT2) * cos (Delta_lon); Long double top = sqrt (top_1 * top_1 + top_2 * top_2); long double bottom = sin (radLat1) * sin (radLat2) +cos (RADLAT1) * cos (RADLAT2) * cos (Delta_lon); Long double delta_sigma = atan2 (top, bottom); Long double distance = Delta_sigma * 6378137.0; return distance;} The GPS data in the 2007-10-14-gps.log file is extracted and saved up after conversion to void Init1 () {Ifstream in1; Ofstream Out1;in1.open ("f:\\ research one on \ \ Drop algorithm competition \ \ City Calculation \\Task\\Data\\task 1-compression\\2007-10-14-gps.log"); Out1.open ("F : \ \ r \ n a drop algorithm contest \ \ City calculates \\Task\\Data\\task 1-compression\\gps.txt "); for (int i=0;i<3150;i++) {string temp; Getline (in1, temp); String gps_e = Temp.substr (20,10); String gps_n = Temp.substr (33,9); out1<<gps_e<< "" <<gps_n<<endl;} Out1.close (); In1.close ();} void Init2 () {ifstream in2; Ofstream Out2; In2.open ("f:\\ research one on \ \ Drop algorithm competition \ \ City Calculation \\Task\\Data\\task 1-compression\\gps.txt"); Out2.open ("f:\\ on the first \ \ Drop algorithm competition \ \ CityThe city calculates \\Task\\Data\\task 1-compression\\realgps1.txt "); for (int i=0;i<3150;i++) {long double gps_e,gps_n; in2>>gps_e>>gps_n; Gps_e = (gps_e-11600.0) *1.0/60+116.0; Gps_n = (gps_n-3900) *1.0/60+39.0; Out2 <<setiosflags (ios::fixed) <<setprecision (6) <<gps_E<< "<<gps_N<<" "< <i+1<< Endl; Gps[i][0] = Gps_e; GPS[I][1] = Gps_n; } out2.close (); In2.close ();} Long double get_d (int point_a,int point_b,int point_c) {long double A = ABS (Geodist (GPS[POINT_B][0],GPS[POINT_B][1), GPS[POINT_C][0],GPS[POINT_C][1])); Long double b = ABS (Geodist (gps[point_a][0],gps[point_a][1],gps[point_c][0],gps[point_c][1)); Long double c = ABS (Geodist (gps[point_a][0],gps[point_a][1],gps[point_b][0],gps[point_b][1)); Long double p = (a+b+c)/2.0; Long double s = SQRTL (ABS (p* (P-A) * (p-b) * (P-C))); Long double d = s*2.0/c; return D;} void Dp_gps (int point_start,iNT Point_end) {if (point_start<point_end) {//Recursive condition long double maxDist = 0; Maximum distance int mid = 0; The maximum distance corresponds to the subscript for (int i=point_start+1;i<point_end;i++) {long Double temp = get_d (Point_start,point_end, i); if (temp>maxdist) {maxDist = temp; mid = i; }//to find the maximum distance and the maximum distance corresponding to the point subscript} if (Maxdist>=dmax) {Gps_visit[mid] = 1;//record the current point join//The original segment to the current The center is divided into two segments, and the recursive processing Dp_gps (POINT_START,MID) is carried out respectively. Dp_gps (Mid,point_end); }}}int Main () {int count = 0; Record the number of output points long double mean_distance_error; Average distance error long double compression_rate; Compression rate init1 (); Init2 (); Gps_visit[0] = 1; GPS_VISIT[3149] = 1; Dp_gps (0,3149); Ofstream out3; Out3.open ("f:\\ research one on \ \ Drop algorithm competition \ \ City Calculation \\Task\\Data\\task 1-compression\\pointid.txt"); for (int i=0;i<3150;i++) {if (gps_visit[i]==1) {out3<<i+1<<endl; count++; }} out3.close (); Long double sum_notvisit_d = 0; int start = 0,end; for (int i=0;i<3150;) {if (start = = 3149) break; If the start point is a tail point, end for (int j=start+1;j<3150;j++) {//Find the next compression node if (gps_visit[j]==1) {End = J; Break }} for (int k=start+1;k<end;k++) {if (gps_visit[k]==0) {sum_notvisit_d+= get_d ( START,END,K); }} start = end; } mean_distance_error = sum_notvisit_d/3150.0; Compression_rate = count/3150.0; cout<<count<<endl; Number of points after output compression cout<<setiosflags (ios::fixed) <<setprecision (6) <<mean_distance_error <<endl; Cout<<setiosflags (ios::fixed) <<setprecision (4) <<Compression_rate*100<< "%" <<endl; System ("pause"); return 0;}
C + + implementation of the Douglas-peucker algorithm for trajectory compression