python射线法判断一个点在图形区域内外


Posted in Python onJune 28, 2019

用python 实现的代码:判断一个点在图形区域内外,供大家参考,具体内容如下

# -*-encoding:utf-8 -*-
# file:class.py
#
 
"""
信息楼
0 123.425658,41.774177
1 123.425843,41.774166
2 123.425847,41.774119
3 123.42693,41.774062
4 123.426943,41.774099
5 123.427118,41.774089
6 123.427066,41.773548
7 123.426896,41.773544
8 123.426916,41.773920
9 123.425838,41.773965
10 123.425804,41.773585
11 123.425611,41.773595
图书馆
0 123.425649,41.77303
1 123.426656,41.772993
2 123.426611,41.772398
3 123.425605,41.772445
"""
 
 
class Point:
 lat = ''
 lng = ''
 
 def __init__(self,lat,lng):
 self.lat = lat #纬度
 self.lng = lng #经度
 
 def show(self):
 print self.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.7740520000 123.42699,41.773592 
在图书馆内的点: 123.4261550000,41.7726740000 123.42571,41.772499 123.425984,41.772919 
不在二者内的点: 123.4246270000,41.7738130000
在信息学馆外包矩形内,但不在信息学馆中的点:123.4264060000,41.7737860000
'''
#lat = raw_input(please input lat)
#lng = raw_input(please input lng)
lat = 123.42699
lng = 41.773592
point = Point(lat,lng)
 
debug = raw_input("请输入debug")
if debug == '1':
 debug = True
else:
 debug = False
 
#求外包矩形
def getPolygonBounds(points):
 length = len(points)
 #top down left right 都是point类型
 top = down = left = right = points[0]
 for i in range(1,length):
 if points[i].lng > top.lng:
  top = points[i]
 elif points[i].lng < down.lng:
  down = points[i]
 else:
  pass
 if points[i].lat > right.lat:
  right = points[i]
 elif points[i].lat < left.lat:
  left = points[i]
   else:
   pass
 
 point0 = Point(left.lat,top.lng)
 point1 = Point(right.lat,top.lng)
 point2 = Point(right.lat,down.lng)
 point3 = Point(left.lat,down.lng)
 polygonBounds = [point0,point1,point2,point3]
 return polygonBounds
 
#测试求外包矩形的一段函数
if debug:
 poly1 = getPolygonBounds(points[0])
 print "第一个建筑的外包是:"
 for i in range(0,len(poly1)):
 poly1[i].show() 
 poly2 = getPolygonBounds(points[1])
 print "第二个建筑的外包是:"
 for i in range(0,len(poly2)):
 poly2[i].show() 
 
 
#判断点是否在外包矩形外
def isPointInRect(point,polygonBounds):
 if point.lng >= polygonBounds[3].lng and \
  point.lng <= polygonBounds[0].lng and \
  point.lat >= polygonBounds[3].lat and \
  point.lat <= polygonBounds[2].lat:\
  return True
 else:
 return False
 
#测试是否在外包矩形外的代码
if debug:
 if(isPointInRect(point,poly1)):
 print "在信息外包矩形内"
 else:
 print "在信息外包矩形外"
 
 if(isPointInRect(point,poly2)):
 print "在图书馆外包矩形内"
 else:
 print "在图书馆外包矩形外"
 
 
 
#采用射线法,计算测试点是否任意一个建筑内
def isPointInPolygon(point,points):
 #定义在边界上或者在顶点都建筑内
 Bound = Vertex = True
 count = 0
 precision = 2e-10
 
 #首先求外包矩形
 polygonBounds = getPolygonBounds(points)
 
 #然后判断是否在外包矩形内,如果不在,直接返回false
 if not isPointInRect(point, polygonBounds):
 if debug:
  print "在外包矩形外"
 return False
 else:
 if debug:
  print "在外包矩形内"
 
 length = len(points)
 p = point
 p1 = points[0]
 for i in range(1,length):
 if p.lng == p1.lng and p.lat == p1.lat:
  if debug:
  print "Vertex1"
  return Vertex
 
 p2 = points[i % length]
 if p.lng == p2.lng and p.lat == p2.lat:
  if dubug:
  print "Vertex2"
  return Vertex
 
 if debug: 
  print i-1,i
  print "p:"
  p.show()
  print "p1:"
  p1.show()
  print "p2:"
  p2.show()
 
 if p.lng < min(p1.lng,p2.lng) or \
  p.lng > max(p1.lng,p2.lng) or \
  p.lat > max(p1.lat,p2.lat): 
  p1 = p2
  if debug:
  print "Outside"
  continue
 
 elif p.lng > min(p1.lng,p2.lng) and \
  p.lng < max(p1.lng,p2.lng):
  if p1.lat == p2.lat:
  if p.lat == p1.lat and \
   p.lng > min(p1.lng,p2.lng) and \
   p.lng < max(p1.lng,p2.lng):
   return Bound
  else:
   count = count + 1
   if debug:
   print "count1:",count
   continue
  if debug:
  print "into left or right"   
 
  a = p2.lng - p1.lng
  b = p1.lat - p2.lat
  c = p2.lat * p1.lng - p1.lat * p2.lng
  d = a * p.lat + b * p.lng + c
  if p1.lng < p2.lng and p1.lat > p2.lat or \
   p1.lng < p2.lng and p1.lat < p2.lat: 
  if d < 0:
   count = count + 1
   if debug:
   print "count2:",count
  elif d > 0:
   p1 = p2
   continue
  elif abs(p.lng-d) < precision :
   return Bound
  else :    
  if d < 0:
   p1 = p2
   continue
  elif d > 0:
   count = count + 1
   if debug:
   print "count3:",count
  elif abs(p.lng-d) < precision :
   return Bound
 else:
  if p1.lng == p2.lng:
  if p.lng == p1.lng and \
   p.lat > min(p1.lat,p2.lat) and \
   p.lat < max(p1.lat,p2.lat):
    return Bound
  else:
  p3 = points[(i+1) % length]
  if p.lng < min(p1.lng,p3.lng) or \
   p.lng > max(p1.lng,p3.lng):
   count = count + 2
   if debug:
   print "count4:",count
  else:
   count = count + 1
   if debug:
   print "count5:",count 
 p1 = p2
 if count % 2 == 0 :
 return False
 else :
 return True
 
 
 
length = len(points)
flag = 0
for i in range(length):
 if isPointInPolygon(point,points[i]):
 print "你刚才输入的点在第 %d 个建筑里" % (i+1)
 print "然后根据i值,可以读出建筑名,或者修改传入的points参数"
 break
 else:
 flag = flag + 1
 
if flag == length:
 print "在头 %d 建筑外" % (i+1)

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现去除下载电影和电视剧文件名中的多余字符的方法
Sep 23 Python
分析Python的Django框架的运行方式及处理流程
Apr 08 Python
Python爬虫模拟登录带验证码网站
Jan 22 Python
Python自动化运维之IP地址处理模块详解
Dec 10 Python
matplotlib savefig 保存图片大小的实例
May 24 Python
Python3爬虫爬取百姓网列表并保存为json功能示例【基于request、lxml和json模块】
Dec 05 Python
python随机在一张图像上截取任意大小图片的方法
Jan 24 Python
python实现海螺图片的方法示例
May 12 Python
浅谈在JupyterNotebook下导入自己的模块的问题
Apr 16 Python
python使用Word2Vec进行情感分析解析
Jul 31 Python
python 密码学示例——理解哈希(Hash)算法
Sep 21 Python
Python读取图像并显示灰度图的实现
Dec 01 Python
Python OpenCV之图片缩放的实现(cv2.resize)
Jun 28 #Python
如何使用Python 打印各种三角形
Jun 28 #Python
python射线法判断检测点是否位于区域外接矩形内
Jun 28 #Python
python 列表转为字典的两个小方法(小结)
Jun 28 #Python
numpy和pandas中数组的合并、拉直和重塑实例
Jun 28 #Python
使用Python画股票的K线图的方法步骤
Jun 28 #Python
连接pandas以及数组转pandas的方法
Jun 28 #Python
You might like
PHP生成带有雪花背景的验证码
2006/10/09 PHP
phpMyAdmin链接MySql错误 个人解决方案
2009/12/28 PHP
PHP使用ffmpeg给视频增加字幕显示的方法
2015/03/12 PHP
深入理解PHP JSON数组与对象
2016/07/19 PHP
php实现的统计字数函数定义与使用示例
2017/07/26 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
2017/07/22 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
JavaScript 学习笔记(五)
2009/12/31 Javascript
Jquery 获得服务器控件值的方法小结
2010/05/11 Javascript
js 火狐下取本地路径实现思路
2013/04/02 Javascript
jQuery修改CSS伪元素属性的方法
2014/07/30 Javascript
JQuery控制div外点击隐藏而div内点击不会隐藏的方法
2015/01/13 Javascript
javascript实现ecshop搜索框键盘上下键切换控制
2015/03/18 Javascript
JavaScript简单下拉菜单实例代码
2015/09/07 Javascript
基于jquery实现动态竖向柱状条特效
2016/02/12 Javascript
JS实现数组去重方法总结(六种方法)
2017/07/14 Javascript
ES6学习教程之块级作用域详解
2017/10/09 Javascript
微信小程序异步处理详解
2017/11/10 Javascript
在vue项目中安装使用Mint-UI的方法
2017/12/27 Javascript
vue实现通讯录功能
2018/07/14 Javascript
JS 数组随机洗牌的实例代码
2018/09/12 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
在项目vue中使用echarts的操作步骤
2020/09/07 Javascript
python使用post提交数据到远程url的方法
2015/04/29 Python
利用Python2下载单张图片与爬取网页图片实例代码
2017/12/25 Python
Python 使用Numpy对矩阵进行转置的方法
2019/01/28 Python
40行Python代码实现天气预报和每日鸡汤推送功能
2020/02/27 Python
python 6.7 编写printTable()函数表格打印(完整代码)
2020/03/25 Python
Python Serial串口基本操作(收发数据)
2020/11/06 Python
使用OpenCV校准鱼眼镜头的方法
2020/11/26 Python
Java的五个基础面试题
2016/02/26 面试题
应届毕业生自荐信
2014/05/28 职场文书
垃圾分类的活动方案
2014/08/15 职场文书
质量保证书怎么写
2015/02/27 职场文书
同意离婚答辩状
2015/05/22 职场文书
winserver2019安装软件一直卡在应用程序正在为首次使用做准备
2022/06/10 Servers