Python光學仿真wxpython透鏡演示系統計算與繪圖
發(fā)布日期:2021-12-23 07:10 | 文章來源:gibhub
計算與繪圖
這里的計算主要包括兩個部分,分別是通過滾動條的參數得到光學器件的特征,這一點此前已經備述。其二則是光在傳播過程中所產生的各種行為,反射折射函數也都已經講過了,需要注意的就是確定邊界。
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)
至此,一個簡易的光學透鏡模擬系統就搭建完成了。同時,我們也學會了python的幾乎所有功能。
最后,再將源代碼的鏈接獻上:透鏡演示系統。
以上就是Python光學仿真wxpython透鏡演示系統計算與繪圖的詳細內容,更多關于wxpython透鏡演示系統計算與繪圖的資料請關注本站其它相關文章!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯系alex-e#qq.com處理。
相關文章
關注官方微信