python射线法判断一个点在图形区域内外
用python实现的代码:判断一个点在图形区域内外,供大家参考,具体内容如下
#-*-encoding:utf-8-*-
#file:class.py
#
"""
信息楼
0123.425658,41.774177
1123.425843,41.774166
2123.425847,41.774119
3123.42693,41.774062
4123.426943,41.774099
5123.427118,41.774089
6123.427066,41.773548
7123.426896,41.773544
8123.426916,41.773920
9123.425838,41.773965
10123.425804,41.773585
11123.425611,41.773595
图书馆
0123.425649,41.77303
1123.426656,41.772993
2123.426611,41.772398
3123.425605,41.772445
"""
classPoint:
lat=''
lng=''
def__init__(self,lat,lng):
self.lat=lat#纬度
self.lng=lng#经度
defshow(self):
printself.lat,"",self.lng
#将信息楼的边界点实例化并存储到points1里
point0=Point(123.425658,41.774177)
point1=Point(123.425843,41.774166)
point2=Point(123.425847,41.774119)
point3=Point(123.42693,41.774062)
point4=Point(123.426943,41.774099)
point5=Point(123.427118,41.774089)
point6=Point(123.427066,41.773548)
point7=Point(123.426896,41.773544)
point8=Point(123.426916,41.773920)
point9=Point(123.425838,41.773961)
point10=Point(123.425804,41.773585)
point11=Point(123.425611,41.773595)
points1=[point0,point1,point2,point3,
point4,point5,point6,point7,
point8,point9,point10,point11,
]
#将图书馆的边界点实例化并存储到points2里
point0=Point(123.425649,41.77303)
point1=Point(123.426656,41.772993)
point2=Point(123.426611,41.772398)
point3=Point(123.425605,41.772445)
points2=[point0,point1,point2,point3]
'''
将points1和points2存储到points里,
points可以作为参数传入
'''
points=[points1,points2]
'''
输入一个测试点,这个点通过GPS产生
建议输入三个点测试
在信息学馆内的点:123.4263790000,41.7740520000123.42699,41.773592
在图书馆内的点:123.4261550000,41.7726740000123.42571,41.772499123.425984,41.772919
不在二者内的点:123.4246270000,41.7738130000
在信息学馆外包矩形内,但不在信息学馆中的点:123.4264060000,41.7737860000
'''
#lat=raw_input(pleaseinputlat)
#lng=raw_input(pleaseinputlng)
lat=123.42699
lng=41.773592
point=Point(lat,lng)
debug=raw_input("请输入debug")
ifdebug=='1':
debug=True
else:
debug=False
#求外包矩形
defgetPolygonBounds(points):
length=len(points)
#topdownleftright都是point类型
top=down=left=right=points[0]
foriinrange(1,length):
ifpoints[i].lng>top.lng:
top=points[i]
elifpoints[i].lngright.lat:
right=points[i]
elifpoints[i].lat=polygonBounds[3].lngand\
point.lng<=polygonBounds[0].lngand\
point.lat>=polygonBounds[3].latand\
point.lat<=polygonBounds[2].lat:\
returnTrue
else:
returnFalse
#测试是否在外包矩形外的代码
ifdebug:
if(isPointInRect(point,poly1)):
print"在信息外包矩形内"
else:
print"在信息外包矩形外"
if(isPointInRect(point,poly2)):
print"在图书馆外包矩形内"
else:
print"在图书馆外包矩形外"
#采用射线法,计算测试点是否任意一个建筑内
defisPointInPolygon(point,points):
#定义在边界上或者在顶点都建筑内
Bound=Vertex=True
count=0
precision=2e-10
#首先求外包矩形
polygonBounds=getPolygonBounds(points)
#然后判断是否在外包矩形内,如果不在,直接返回false
ifnotisPointInRect(point,polygonBounds):
ifdebug:
print"在外包矩形外"
returnFalse
else:
ifdebug:
print"在外包矩形内"
length=len(points)
p=point
p1=points[0]
foriinrange(1,length):
ifp.lng==p1.lngandp.lat==p1.lat:
ifdebug:
print"Vertex1"
returnVertex
p2=points[i%length]
ifp.lng==p2.lngandp.lat==p2.lat:
ifdubug:
print"Vertex2"
returnVertex
ifdebug:
printi-1,i
print"p:"
p.show()
print"p1:"
p1.show()
print"p2:"
p2.show()
ifp.lngmax(p1.lng,p2.lng)or\
p.lat>max(p1.lat,p2.lat):
p1=p2
ifdebug:
print"Outside"
continue
elifp.lng>min(p1.lng,p2.lng)and\
p.lngmin(p1.lng,p2.lng)and\
p.lngp2.lator\
p1.lng0:
p1=p2
continue
elifabs(p.lng-d)0:
count=count+1
ifdebug:
print"count3:",count
elifabs(p.lng-d)min(p1.lat,p2.lat)and\
p.latmax(p1.lng,p3.lng):
count=count+2
ifdebug:
print"count4:",count
else:
count=count+1
ifdebug:
print"count5:",count
p1=p2
ifcount%2==0:
returnFalse
else:
returnTrue
length=len(points)
flag=0
foriinrange(length):
ifisPointInPolygon(point,points[i]):
print"你刚才输入的点在第%d个建筑里"%(i+1)
print"然后根据i值,可以读出建筑名,或者修改传入的points参数"
break
else:
flag=flag+1
ifflag==length:
print"在头%d建筑外"%(i+1)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。