Python光學(xué)仿真wxpython透鏡演示系統(tǒng)計(jì)算與繪圖
計(jì)算與繪圖
這里的計(jì)算主要包括兩個(gè)部分,分別是通過滾動(dòng)條的參數(shù)得到光學(xué)器件的特征,這一點(diǎn)此前已經(jīng)備述。其二則是光在傳播過程中所產(chǎn)生的各種行為,反射折射函數(shù)也都已經(jīng)講過了,需要注意的就是確定邊界。
def getRay(self): self.rays,self.abcs,self.dots = [[],[],[]] sDot = self.source #光源為第一個(gè)點(diǎn) sRay = rp.getABC(self.sourceDict['theta'],sDot) inPoint,outPoint,flec,frac = self.opti.singleReflect(sRay,sDot,1) if inPoint == []: return [] #無交點(diǎn)返回空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) ##求光線與界面邊緣的交點(diǎn) 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 []
從代碼的可讀性來說,繪圖部分邏輯簡(jiǎn)單,需要注意的一點(diǎn)是,DC繪圖默認(rèn)的坐標(biāo)系并不是我們所熟知的那個(gè)坐標(biāo)系,需要進(jìn)行一次翻轉(zhuǎn)。
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) #坐標(biāo)系翻轉(zhuǎn) 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: #此時(shí)為平面 dc.DrawLine(dots[0][0],dots[0][1], dots[1][0],dots[1][1]) elif len(dots)==3: #此時(shí)為曲面 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]) ##繪制光線與物體表面的交點(diǎn) dc.SetPen(wx.Pen('#FF00FF')) for dot in self.dots: dc.DrawCircle(dot[0],dot[1],5)
至此,一個(gè)簡(jiǎn)易的光學(xué)透鏡模擬系統(tǒng)就搭建完成了。同時(shí),我們也學(xué)會(huì)了python的幾乎所有功能。
最后,再將源代碼的鏈接獻(xiàn)上:透鏡演示系統(tǒng)。
以上就是Python光學(xué)仿真wxpython透鏡演示系統(tǒng)計(jì)算與繪圖的詳細(xì)內(nèi)容,更多關(guān)于wxpython透鏡演示系統(tǒng)計(jì)算與繪圖的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。