查询点是否在一个多边形范围,性能一般,希望的到优化指点

auspi12341 2017-09-21 05:59:37
#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;
}


第一次尝试使用C++来写,有很多不清楚的地方,虽然经历坎坷弄出来了,但是发现性能一般,主要应该是格式化字符串和格式转化上有多余开销,希望能各位能指点优化下,感激不尽,
学习路上,很多地方看起来很low,也希望能一并提出来,少走点弯路,谢谢
...全文
299 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
auspi12341 2017-09-25
  • 打赏
  • 举报
回复
引用 7 楼 xsklld 的回复:
除非你明确了解float的精度,否则永远不要用float。
谢谢建议
auspi12341 2017-09-25
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
PtInRegion The PtInRegion function determines whether the specified point is inside the specified region. BOOL PtInRegion( HRGN hrgn, // handle to region int X, // x-coordinate of point int Y // y-coordinate of point ); Parameters hrgn Handle to the region to be examined. X Specifies the x-coordinate of the point. Y Specifies the y-coordinate of the point. Return Values If the specified point is in the region, the return value is nonzero. If the specified point is not in the region, the return value is zero. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Regions Overview, Region Functions, RectInRegion
感谢 正是我想要的
weixin_40387382 2017-09-25
  • 打赏
  • 举报
回复
除非你明确了解float的精度,否则永远不要用float。
  • 打赏
  • 举报
回复
引用 2 楼 auspi12341 的回复:
[quote=引用 1 楼 zjq9931 的回复:] 这个问题在于,每判断一次都要进行转换,要改为在判断前完成转换。 判断的时候,不做转换就可以了。
请问需要如何转换呢 用 vector<float> location 替换 vector<string> location 嘛? [/quote] 是啊,你再判断函数里面做了转换,把这个转换挪到判断函数之前,只转换一次,然后每次判断的时候不用转换,就好了。
  • 打赏
  • 举报
回复
这个问题在于,每判断一次都要进行转换,要改为在判断前完成转换。 判断的时候,不做转换就可以了。
auspi12341 2017-09-22
  • 打赏
  • 举报
回复
引用 4 楼 sdghchj 的回复:
都C++了,该考虑设计问题了。 基本的有个 struct Point{float x;float y}; 也行啊。 class Polygon{ }
谢谢建议
sdghchj 2017-09-22
  • 打赏
  • 举报
回复
都C++了,该考虑设计问题了。 基本的有个 struct Point{float x;float y}; 也行啊。 class Polygon{ }
auspi12341 2017-09-22
  • 打赏
  • 举报
回复
引用 1 楼 zjq9931 的回复:
这个问题在于,每判断一次都要进行转换,要改为在判断前完成转换。 判断的时候,不做转换就可以了。
请问需要如何转换呢 用 vector<float> location 替换 vector<string> location 嘛?
xskxzr 2017-09-22
  • 打赏
  • 举报
回复
除非你明确了解float的精度,否则永远不要用float。
赵4老师 2017-09-22
  • 打赏
  • 举报
回复
PtInRegion The PtInRegion function determines whether the specified point is inside the specified region. BOOL PtInRegion( HRGN hrgn, // handle to region int X, // x-coordinate of point int Y // y-coordinate of point ); Parameters hrgn Handle to the region to be examined. X Specifies the x-coordinate of the point. Y Specifies the y-coordinate of the point. Return Values If the specified point is in the region, the return value is nonzero. If the specified point is not in the region, the return value is zero. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Regions Overview, Region Functions, RectInRegion

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧