計算與繪圖
這里的計算主要包括兩個部分,分別是通過滾動條的參數得到光學器件的特征,這一點此前已經備述。其二則是光在傳播過程中所產生的各種行為,反射折射函數也都已經講過了,需要注意的就是確定邊界。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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繪圖默認的坐標系并不是我們所熟知的那個坐標系,需要進行一次翻轉。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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透鏡演示系統計算與繪圖的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/m0_37816922/article/details/100534678