python射線法判斷一個(gè)點(diǎn)在圖形區(qū)域內(nèi)外
更新時(shí)間:2019年06月28日 10:29:07 作者:兩鬢已不能斑白
這篇文章主要為大家詳細(xì)介紹了python射線法判斷一個(gè)點(diǎn)在圖形區(qū)域內(nèi)外,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
用python 實(shí)現(xiàn)的代碼:判斷一個(gè)點(diǎn)在圖形區(qū)域內(nèi)外,供大家參考,具體內(nèi)容如下
# -*-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 #經(jīng)度 def show(self): print self.lat," ",self.lng #將信息樓的邊界點(diǎn)實(shí)例化并存儲(chǔ)到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, ] #將圖書館的邊界點(diǎn)實(shí)例化并存儲(chǔ)到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存儲(chǔ)到points里, points可以作為參數(shù)傳入 ''' points = [points1,points2] ''' 輸入一個(gè)測(cè)試點(diǎn),這個(gè)點(diǎn)通過(guò)GPS產(chǎn)生 建議輸入三個(gè)點(diǎn)測(cè)試 在信息學(xué)館內(nèi)的點(diǎn):123.4263790000,41.7740520000 123.42699,41.773592 在圖書館內(nèi)的點(diǎn): 123.4261550000,41.7726740000 123.42571,41.772499 123.425984,41.772919 不在二者內(nèi)的點(diǎn): 123.4246270000,41.7738130000 在信息學(xué)館外包矩形內(nèi),但不在信息學(xué)館中的點(diǎn):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("請(qǐng)輸入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 #測(cè)試求外包矩形的一段函數(shù) if debug: poly1 = getPolygonBounds(points[0]) print "第一個(gè)建筑的外包是:" for i in range(0,len(poly1)): poly1[i].show() poly2 = getPolygonBounds(points[1]) print "第二個(gè)建筑的外包是:" for i in range(0,len(poly2)): poly2[i].show() #判斷點(diǎn)是否在外包矩形外 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 #測(cè)試是否在外包矩形外的代碼 if debug: if(isPointInRect(point,poly1)): print "在信息外包矩形內(nèi)" else: print "在信息外包矩形外" if(isPointInRect(point,poly2)): print "在圖書館外包矩形內(nèi)" else: print "在圖書館外包矩形外" #采用射線法,計(jì)算測(cè)試點(diǎn)是否任意一個(gè)建筑內(nèi) def isPointInPolygon(point,points): #定義在邊界上或者在頂點(diǎn)都建筑內(nèi) Bound = Vertex = True count = 0 precision = 2e-10 #首先求外包矩形 polygonBounds = getPolygonBounds(points) #然后判斷是否在外包矩形內(nèi),如果不在,直接返回false if not isPointInRect(point, polygonBounds): if debug: print "在外包矩形外" return False else: if debug: print "在外包矩形內(nèi)" 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 "你剛才輸入的點(diǎn)在第 %d 個(gè)建筑里" % (i+1) print "然后根據(jù)i值,可以讀出建筑名,或者修改傳入的points參數(shù)" break else: flag = flag + 1 if flag == length: print "在頭 %d 建筑外" % (i+1)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實(shí)現(xiàn)計(jì)算納什均衡的示例詳解
納什均衡是一種博弈論中的概念,它描述了一種平衡狀態(tài),其中每個(gè)參與者都不能通過(guò)獨(dú)立改變其決策來(lái)提高自己的利益。本文就來(lái)用Python中的Nashpy和PuLP實(shí)現(xiàn)計(jì)算納什均衡,感興趣的可以了解一下2023-02-02python中round函數(shù)保留兩位小數(shù)的方法
在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python中round函數(shù)保留兩位小數(shù)的方法及相關(guān)知識(shí)點(diǎn),有興趣的朋友們可以學(xué)習(xí)下。2020-12-12OpenCV實(shí)戰(zhàn)之實(shí)現(xiàn)手勢(shì)虛擬縮放效果
本篇將會(huì)以HandTrackingModule為模塊,實(shí)現(xiàn)通過(guò)手勢(shì)對(duì)本人的博客海報(bào)進(jìn)行縮放。文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-11-11Appium自動(dòng)化測(cè)試中獲取Toast信息操作
本文主要介紹了Appium自動(dòng)化測(cè)試中獲取Toast信息操作,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02如何利用python實(shí)現(xiàn)kmeans聚類
K-Means是聚類算法的一種,以距離來(lái)判斷數(shù)據(jù)點(diǎn)間的相似度并對(duì)數(shù)據(jù)進(jìn)行聚類,下面這篇文章主要給大家介紹了關(guān)于如何利用python實(shí)現(xiàn)kmeans聚類的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05使用Pyhton集合set()實(shí)現(xiàn)成果查漏的例子
今天小編就為大家分享一篇使用Pyhton集合set()實(shí)現(xiàn)成果查漏的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11