64,649
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <cfloat>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
bool pointInPolygon(float x, float y,vector<string> location) {
bool oddNodes=false ;
string delim = ",";
string lon_s,lat_s,lon_e,lat_e;
int subnum = 0;
float lonStart,latEnd,lonMStart,latMEnd;
vector< std::string >* ret;
int i,j=location.size()-1 ;
for (i=0;i<location.size(); i++) {
subnum = location[i].find_first_of(delim) ;
lon_s = location[i].substr(0,subnum);
lat_s = location[i].substr(subnum + 1,-1);
subnum = location[j].find_first_of(delim) ;
lon_e = location[j].substr(0,subnum);
lat_e = location[j].substr(subnum + 1,-1);
lonStart = atof(lon_s.c_str());
latEnd = atof(lat_s.c_str());
lonMStart = atof(lon_e.c_str());
latMEnd = atof(lat_e.c_str());
if((latEnd< y && latMEnd>=y || latMEnd<y && latEnd>=y) && (lonStart<=x || lonMStart<=x)) {
oddNodes^=(lonStart+(y-latEnd)/(latMEnd-latEnd)*(lonMStart-lonStart)<x);
}
j=i;
}
return oddNodes;
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t start = clock();
char input[] = "104.047272,30.672264|104.071778,30.672481|104.072281,30.663411|104.072281,30.663411|104.072281,30.663411|104.072281,30.663411|104.072281,30.663411|104.072281,30.663411|104.072281,30.663411|104.072281,30.663411 ";
char *token = strtok(input, "|");
vector<string> location;
while(token) {
location.push_back(token);
token = strtok(NULL, "|");
}
int i ;
bool InPolygon;
for (i=0;i<100; i++) {
InPolygon = pointInPolygon(104.070269,30.668909,location);
}
if(InPolygon){
printf ("%s\n","在里面");
}else{
printf ("%s\n","不在里面");
}
clock_t ends = clock();
cout <<"Running Time : "<<(double)(ends - start)/ CLOCKS_PER_SEC << endl;
system("pause");
return 0;
}