Python光學仿真wxpython透鏡演示系統(tǒng)計算與繪圖
計算與繪圖
這里的計算主要包括兩個部分,分別是通過滾動條的參數(shù)得到光學器件的特征,這一點此前已經備述。其二則是光在傳播過程中所產生的各種行為,反射折射函數(shù)也都已經講過了,需要注意的就是確定邊界。
def getRay(self): self.rays,self.abcs,self.dots = [[],[],[]] sDot = self.source #光源為第一個點 sRay = rp.getABC(self.sourceDict['theta'],sDot) inPoint,outPoint,flec,frac = self.opti.singleReflect(sRay,sDot,1) if inPoint == []: return [] #無交點返回空list self.dots.append(inPoint) self.rays.append([sDot,inPoint]) crossflec = self.crossRagion(flec,inPoint) if crossflec != []: self.dots.append(crossflec) self.rays.append([inPoint,crossflec]) self.abcs.append(flec) if outPoint == []: return [] self.dots.append(outPoint) self.rays.append([inPoint,outPoint]) if frac == []: return [] crossfrac = self.crossRagion(frac,outPoint) if crossflec != []: self.dots.append(crossfrac) self.rays.append([outPoint,crossfrac]) self.abcs.append(frac) ##求光線與界面邊緣的交點 def crossRagion(self,ray,point): w,h = self.drawPanel.GetSize() edges = [[(0,0),(0,w)],[(0,h/2),(0,-h/2)],[(0,-h/2),(w,-h/2)], [(w,-h/2),(w,h/2)],[(w,h/2),(0,h/2)]] for dots in edges: cross=rp.getCross(ray,dots,point) if cross!=[]: return cross return []
從代碼的可讀性來說,繪圖部分邏輯簡單,需要注意的一點是,DC繪圖默認的坐標系并不是我們所熟知的那個坐標系,需要進行一次翻轉。
def DrawPath(self): w,h = self.drawPanel.GetSize() #獲取畫布尺寸 dc = wx.ClientDC(self.drawPanel) dc.SetPen(wx.Pen('#666666')) dc.DrawRectangle(0,0,w,h) dc.SetDeviceOrigin(0,h/2) dc.SetAxisOrientation(True,True) #坐標系翻轉 dc.SetPen(wx.Pen('#0000FF')) dc.DrawLine(0,0,w,0) dc.SetPen(wx.Pen('#00FF00')) ##繪制透鏡 for edge in self.opti.edges: dots = edge['dots'] if len(dots)==2: #此時為平面 dc.DrawLine(dots[0][0],dots[0][1], dots[1][0],dots[1][1]) elif len(dots)==3: #此時為曲面 x3,y3,_=rp.arc2cir(dots) if dots[1][0]>dots[2][0]: #畫劣弧 dc.DrawArc(dots[0][0],dots[0][1], dots[1][0],dots[1][1],x3,y3) else: dc.DrawArc(dots[1][0],dots[1][1], dots[0][0],dots[0][1],x3,y3) dc.SetPen(wx.Pen('#FF0000')) ##繪制光源 dc.DrawCircle(self.source[0],self.source[1],10) ##繪制光線 for ray in self.rays: dc.DrawLine(ray[0][0],ray[0][1], ray[1][0],ray[1][1]) ##繪制光線與物體表面的交點 dc.SetPen(wx.Pen('#FF00FF')) for dot in self.dots: dc.DrawCircle(dot[0],dot[1],5)
至此,一個簡易的光學透鏡模擬系統(tǒng)就搭建完成了。同時,我們也學會了python的幾乎所有功能。
最后,再將源代碼的鏈接獻上:透鏡演示系統(tǒng)。
以上就是Python光學仿真wxpython透鏡演示系統(tǒng)計算與繪圖的詳細內容,更多關于wxpython透鏡演示系統(tǒng)計算與繪圖的資料請關注腳本之家其它相關文章!
相關文章
Python+Tkinter創(chuàng)建一個簡單的鬧鐘程序
這篇文章主要為大家詳細介紹了如何使用 Python 的 Tkinter 庫創(chuàng)建一個簡單的鬧鐘程序,它可以在指定的時間播放一個聲音來提醒你,感興趣的可以學習一下2023-04-04python調用短信貓控件實現(xiàn)發(fā)短信功能實例
這篇文章主要介紹了python調用短信貓控件實現(xiàn)發(fā)短信功能實例,需要的朋友可以參考下2014-07-07解決python3.x安裝numpy成功但import出錯的問題
這篇文章主要介紹了解決python3.x安裝numpy成功但import出錯的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Python3實現(xiàn)發(fā)送QQ郵件功能(html)
這篇文章主要為大家詳細介紹了Python3實現(xiàn)發(fā)送QQ郵件功能,html格式的qq郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12