`
linuxstuding
  • 浏览: 1239340 次
文章分类
社区版块
存档分类
最新评论

判断一个点是否落在多边形内

 
阅读更多

一、背景:

如何判断一个指定的经纬度点是否落在一个多边形区域内?

二、实现代码(delphi)


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->Type
TMyPoint
=packedrecord
X:double;
Y:double;
end;

{*------------------------------------------------------------------------------
判断指定的经纬度坐标点是否落在指定的多边形区域内
@paramALon指定点的经度
@paramALat指定点的纬度
@paramAPoints指定多边形区域各个节点坐标
@returnTrue落在范围内False不在范围内
------------------------------------------------------------------------------*
}
functionIsPtInPoly(ALon,ALat:double;APoints:arrayofTMyPoint):Boolean;
var
iSum,iCount,iIndex:Integer;
dLon1,dLon2,dLat1,dLat2,dLon:double;
begin
Result:
=False;
if(Length(APoints)<3)then
begin
Result:
=False;
Exit;
end;
iSum:
=0;
iCount:
=Length(APoints);
foriIndex:=0toiCount-1do
begin
if(iIndex=iCount-1)then
begin
dLon1:
=APoints[iIndex].X;
dLat1:
=APoints[iIndex].Y;
dLon2:
=APoints[0].X;
dLat2:
=APoints[0].Y;
end
else
begin
dLon1:
=APoints[iIndex].X;
dLat1:
=APoints[iIndex].Y;
dLon2:
=APoints[iIndex+1].X;
dLat2:
=APoints[iIndex+1].Y;
end;
if((ALat>=dLat1)and(ALat<dLat2))or((ALat>=dLat2)and(ALat<dLat1))then
begin
if(abs(dLat1-dLat2)>0)then
begin
dLon:
=dLon1-((dLon1-dLon2)*(dLat1-ALat))/(dLat1-dLat2);
if(dLon<ALon)then
Inc(iSum);
end;
end;

end;
if(iSummod2<>0)then
Result:
=True;
end;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics