侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   350

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

# -*-encoding:utf-8 -*-# file:class.py# """信息楼0 123.425658,41.7741771 123.425843,41.7741662 123.425847,41.7741193 123.42693,41.7740624 123.426943,41.7740995 123.427118,41.7740896 123.427066,41.7735487 123.426896,41.7735448 123.426916,41.7739209 123.425838,41.77396510 123.425804,41.77358511 123.425611,41.773595图书馆0 123.425649,41.773031 123.426656,41.7729932 123.426611,41.7723983 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.42699lng = 41.773592point = Point(lat,lng) debug = raw_input("请输入debug")if debug == '1': debug = Trueelse: 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 = 0for 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 OpenCV之图片缩放的实现(cv2.resize)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客