亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python 實現德洛內三角剖分的操作

瀏覽:15日期:2022-06-21 15:54:33

我也不知道這玩意主要是干啥用的

實現如下

我用剖分的三角形的三個頂點到中心點的距離和作為顏色, 結果顯示: 點越密集的地方, 圖片上的顏色越深。

from scipy.spatial import Delaunayimport numpy as npimport matplotlib.pyplot as pltwidth = 80height = 40 pointNumber = 50points = np.zeros((pointNumber, 2)) points[:, 0] = np.random.randint(0, width, pointNumber) points[:, 1] = np.random.randint(0, height, pointNumber)tri = Delaunay(points)center = np.sum(points[tri.simplices], axis=1)/3.0 ’’’color = []for sim in points[tri.simplices]: x1, y1 = sim[0][0], sim[0][1] x2, y2 = sim[1][0], sim[1][1] x3, y3 = sim[2][0], sim[2][1]s = ((x1-x2)**2+(y1-y2)**2)**0.5 + ((x1-x3)**2+(y1-y3)**2)**0.5 + ((x3-x2)**2+(y3-y2)**2)**0.5 color.append(s)color = np.array(color)’’’color = []for index, sim in enumerate(points[tri.simplices]): cx, cy = center[index][0], center[index][1] x1, y1 = sim[0][0], sim[0][1] x2, y2 = sim[1][0], sim[1][1] x3, y3 = sim[2][0], sim[2][1]s = ((x1-cx)**2+(y1-cy)**2)**0.5 + ((cx-x3)**2+(cy-y3)**2)**0.5 + ((cx-x2)**2+(cy-y2)**2)**0.5 color.append(s)color = np.array(color)plt.figure(figsize=(20, 10)) plt.tripcolor(points[:, 0], points[:, 1], tri.simplices.copy(), facecolors=color, edgecolors=’k’) plt.tick_params(labelbottom=’off’, labelleft=’off’, left=’off’, right=’off’, bottom=’off’, top=’off’) ax = plt.gca() plt.scatter(points[:,0],points[:,1], color=’r’)#plt.grid()plt.savefig(’Delaunay.png’, transparent=True, dpi=600)

python 實現德洛內三角剖分的操作

補充:生長算法實現點集的三角剖分( Python(Tkinter模塊))

關于三角剖分

假設V是二維實數域上的有限點集,邊e是由點集中的點作為端點構成的封閉線段, E為e的集合。那么該點集V的一個三角剖分T=(V,E)是一個平面圖G,該平面圖滿足條件:

1.除了端點,平面圖中的邊不包含點集中的任何點。

2.沒有相交邊。

3.平面圖中所有的面都是三角面,且所有三角面的合集是散點集V的凸包。

在實際中運用的最多的三角剖分是Delaunay三角剖分,它是一種特殊的三角剖分。

【定義】Delaunay邊:假設E中的一條邊e(兩個端點為a,b),e若滿足下列條件,則稱之為Delaunay邊:

存在一個圓經過a,b兩點,圓內(注意是圓內,圓上最多三點共圓)不含點集V中任何其他的點,這一特性又稱空圓特性。

【定義】Delaunay三角剖分:如果點集V的一個三角剖分T只包含Delaunay邊,那么該三角剖分稱為Delaunay三角剖分。

python 實現德洛內三角剖分的操作

關于Delaunay三角剖分算法可以參考百度百科Delaunay三角剖分算法

我做三角剖分的目的——構建TIN,不規則三角網

不規則三角網(TIN)是DEM的重要形式之一,相較于規則格網,其具有數據冗余小、細節丟失少的特點。

在分布不規則的高程點之間構建出三角網,其關鍵技術就是三角剖分

python 實現德洛內三角剖分的操作

算法步驟

1、首先任選一點,在點集中找出距離改點最近的點連成一條線,以該線為基線。

2、在所有點中尋找能與該基線構成具有空圓性三角形的點,并構成三角形。

3、以新生成的邊為基線,重復第二步,直至點集構網完成。

具體代碼如下

所使用的python版本為python3.6,編輯器為Pycharm2018.3.1

#-*- coding:utf-8 -*-import tkinterfrom tkinter import filedialogimport csv#根據兩點坐標計算距離def caldis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5#輸入三角形三個頂點,計算外接圓圓心及半徑def calcenter(x1,y1,x2,y2,x3,y3): y1=-y1 #計算公式是根據平面直角坐標推算的,原點在左下角,但是計算機屏幕坐標原點在右上角,所以計算式y坐標取負 y2=-y2 y3=-y3 if (y1 != y3 and y1 != y2 and y2 != y3): #判斷是否有y坐標相等,即三角形某邊斜率為0的情況,避免出現墳分母為0的錯誤if(((x3-x1)/(y3-y1))-((x2-x1)/(y2-y1)))==0: x2=x2+1x=(((y1+y3)/2)+((x1+x3)/2)*((x3-x1)/(y3-y1))-((y1+y2)/2)-((x1+x2)/2)*((x2-x1)/(y2-y1)))/(((x3-x1)/(y3-y1))-((x2-x1)/(y2-y1)))y=-((x3-x1)/(y3-y1))*x+((y1+y3)/2)+(((x1+x3)/2)*((x3-x1)/(y3-y1)))return (x, -y, caldis(x, y, x1, y1)) elif (y1 == y3 and y1 != y2 and y2 != y3):#若存在斜率為0的邊則計算可簡化x=(x1+x3)/2y=-((x2-x1)/(y2-y1))*x+((y1+y2)/2)+((x2-x1)/(y2-y1))*((x1+x2)/2)return (x, -y, caldis(x, y, x1, y1)) #返回值為元組(圓心橫坐標x,圓心縱坐標y,外接圓半徑r),計算出來的y值要返回屏幕坐標所以再次取負 elif (y1 != y3 and y1 == y2 and y2 != y3):x = (x1 + x2) / 2y = -((x3 - x1) / (y3 - y1)) * x + ((y1 + y3) / 2) + ((x3 - x1) / (y3 - y1)) * ((x1 + x3) / 2)return (x, -y, caldis(x, y, x1, y1)) elif (y1 != y3 and y1 != y2 and y2 == y3):x = (x3 + x2) / 2y = -((x3 - x1) / (y3 - y1)) * x + ((y1 + y3) / 2) + ((x3 - x1) / (y3 - y1)) * ((x1 + x3) / 2)return (x, -y, caldis(x, y, x1, y1)) else:return Noneclass getTIN: #定義窗口及操作類 def __init__(self):self.path=str() #坐標文件路徑self.pointlist=[] #存放所有點坐標的列表self.linelist=[] #存放線的列表,每條線用兩個點號表示連線self.tk=tkinter.Tk() #定義主窗口self.tk.title(’MyTIN’)self.tk.geometry(’1200x720’)self.shengzhang=tkinter.Button(self.tk,text=’生長算法’,width=15,command=self.drawTIN_shengzhang)self.shengzhang.place(x=1050,y=100) #定義按鈕,關聯到生長算法計算TIN的的函數self.readin=tkinter.Button(self.tk,text=’讀入坐標文件’,width=15,command=self.getfile)self.readin.place(x=1050,y=50)self.can=tkinter.Canvas(self.tk,width=950,height=620,bg=’white’)self.can.place(x=50,y=50)self.tk.mainloop() def getfile(self): #選擇坐標文件(*.csv),從文件中讀入坐標存入pointlist列表并在繪圖區展示出來self.path=filedialog.askopenfilename()f=open(self.path,’r’)fd=csv.reader(f)self.pointlist=list(fd)for i in range(0,len(self.pointlist)): self.can.create_oval(int(self.pointlist[i][0])-2,int(self.pointlist[i][1])-2,int(self.pointlist[i][0])+2,int(self.pointlist[i][1])+2,fill=’black’) self.can.create_text(int(self.pointlist[i][0])+7,int(self.pointlist[i][1])-7,text=str(i)) def drawTIN_shengzhang(self):j = 1k = 0mindis = ((int(self.pointlist[0][0]) - int(self.pointlist[1][0])) ** 2 + (int(self.pointlist[0][1]) - int(self.pointlist[1][1])) ** 2) ** 0.5x = len(self.pointlist)for i in range(1, x): dis = ((int(self.pointlist[0][0]) - int(self.pointlist[i][0])) ** 2 + (int(self.pointlist[0][1]) - int(self.pointlist[i][1])) ** 2) ** 0.5 if dis < mindis:mindis = disj = iself.linelist.append((k,j)) #首先計算出距起始點(點號為0)距離最短的點,以這兩點的連線作為基線開始生長self.shengzhangjixian(k,j) def drawTIN(self): #根據線文件在繪圖區繪制出TINfor i in self.linelist: self.can.create_line(self.pointlist[i[0]][0], self.pointlist[i[0]][1], self.pointlist[i[1]][0], self.pointlist[i[1]][1]) def shengzhangjixian(self,i,j): #根據某一基線開始生長的函數x = len(self.pointlist)for k in range(0,x): #遍歷沒一個點,判斷是否與基線構成D三角形 n = 0 #n用于統計外接圓內的點數 if ((k,i) not in self.linelist) and ((i,k) not in self.linelist) and ((j,k) not in self.linelist) and ((k,j) not in self.linelist):for y in range(0,x): #遍歷每一個點,判斷 if y==i or y==j or y==k:continue if(calcenter(int(self.pointlist[i][0]),int(self.pointlist[i][1]),int(self.pointlist[j][0]),int(self.pointlist[j][1]),int(self.pointlist[k][0]),int(self.pointlist[k][1]))==None):continue else:xyr=calcenter(int(self.pointlist[i][0]),int(self.pointlist[i][1]),int(self.pointlist[j][0]),int(self.pointlist[j][1]),int(self.pointlist[k][0]),int(self.pointlist[k][1])) if caldis(xyr[0],xyr[1],int(self.pointlist[y][0]),int(self.pointlist[y][1])) < xyr[2]: #判斷點是否在外接圓內n=n+1 else:continue else:continue if n == 0: #判斷是否為D三角形self.linelist.append((k,i)) #將新生成的邊的端點號加入線列表self.drawTIN() #調用繪制函數繪制TINself.shengzhangjixian(k,i) #以生成的新邊作為基線,迭代計算self.linelist.append((k,j))self.drawTIN()self.shengzhangjixian(k,j) else:continueif __name__ == ’__main__’: MyTIN=getTIN()

通過如下代碼生成一組隨機的點并存入文件

import randomimport csvfrom tkinter import filedialogpath=filedialog.askopenfilename()OutAddress=open(path,’a’,newline=’’)csv_write = csv.writer(OutAddress,dialect=’excel’)for i in range(0,20): x=random.randint(30,920) y=random.randint(30,590) out=(x,y) print(out) csv_write.writerow(out)

通過上面的程序我們得到一組坐標如下

550,43281,334517,265842,408369,123502,169271,425213,482588,24894,295344,350500,385912,527801,491838,455104,479760,160706,77227,314764,576

將以上的點在界面中展示出來

python 實現德洛內三角剖分的操作

點擊生長算法運行得到結果

python 實現德洛內三角剖分的操作

小結

生長算法在三角剖分算法中并不是什么高效的算法,其特點在于算法簡單易行,但是計算量大,并且對于新插入的點無法更新,必須重新計算。

相比之下,逐點插入算法雖然計算量仍然較大(似乎三角剖分計算量都不?。?,但是能實現對新插入點的更新而不用重頭計算。

注:文中部分圖片及介紹來自百度百科。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 一本一本大道香蕉久在线精品 | 99视频在线永久免费观看 | 亚洲图片一区 | 欧美一级毛片aaaaa | 任我鲁精品视频精品 | 免费播放国产性色生活片 | 成人午夜爽爽爽免费视频 | 久青草国产观看在线视频 | 欧美日韩亚洲综合久久久 | 欧美性高清在线 | 丁香六月纪婷婷激情综合 | 十六以下岁女子毛片免费 | 欧日韩视频 | 国产一区二区高清视频 | 91探花福利精品国产自产在线 | 国产夜色 | www.成人网.com | 亚洲aⅴ在线| 免费a级毛片网站 | 国产福利视频一区 | 国外b2b网站毛片 | 成人理论 | 嫩草视频在线观看免费 | 一区二区三区在线 | 欧 | 婷婷综合影院 | 亚洲欧美综合日韩字幕v在线 | 国产精品秋霞午夜 | 精品国产香蕉伊思人在线又爽又黄 | 日本a及毛片免费视频 | 日本精品视频一区二区三区 | 日韩不卡一级毛片免费 | 亚洲精品国产精品一区二区 | 91无毒不卡 | 一级日韩片 | 欧美精品亚洲一区二区在线播放 | 欧美毛片aaaaa片久久久久 | 国产日韩欧美亚洲综合首页 | 成人αv在线视频高清 | 欧美三级手机在线 | japanese亚洲人妖 | 成人不卡在线 |