华域联盟 Python Python光学仿真wxpython透镜演示系统计算与绘图

Python光学仿真wxpython透镜演示系统计算与绘图

文章目录[隐藏]

目录

计算与绘图

这里的计算主要包括两个部分,分别是通过滚动条的参数得到光学器件的特征,这一点此前已经备述。其二则是光在传播过程中所产生的各种行为,反射折射函数也都已经讲过了,需要注意的就是确定边界。

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透镜演示系统计算与绘图的资料请关注华域联盟其它相关文章!

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » Python光学仿真wxpython透镜演示系统计算与绘图

转载请保留出处和原文链接:https://www.cnhackhy.com/39356.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部