This is the web version of EJS interface
EJS version 4.1
  1. Author=Fu-Kwun Hwang - Dept. of Physics, National Taiwan Normal Univ.
  2. Abstract=hwang@phy.ntnu.edu.tw
  3. CaptureTools=true
  4. DataTools=true
  5. LanguageTools=true
    NameValueTypeDimension
    NameValueTypeDimension
    NameValueTypeDimension

    Init Page

    dc=2*pi/n; if(n%2==0)ci=dc; else ci=pi2; ci+=rca/c2a; for(int i=0;i<n0;i++){ if(i<n){ c=ci-i*dc; px[i]=R*Math.cos(c); py[i]=R*Math.sin(c); // if(i>0 &&py[i]==py[i-1])py[i]=py[i-1]+min; }else{ px[i]=px[i-1]; py[i]=py[i-1]; } } sizep=size/Math.sqrt(n);

    calculateRays

    xs1=xs+dxs; ys1=ys+dys; for(int i=0;i<nt0;i++){// setup beam cx[i]=xs+dxs;// ray traces cy[i]=ys+dys; I[i]=0; dx[i]=0;//reflected rays dy[i]=0; clr1[i]=getColor(0); clr[i]=getColor(0); dx2[i]=dy2[i]=0; } c=Math.atan2(dys,dxs); m=Math.tan(c); b=ys-m*xs; np=0; in=false; cid=nt0-1; I[cid]=1.; clr1[cid]=getColor(0);// none existing ray clr[cid-1]=clr[cid]=getColor(1);// incident ray cid--;//n0-2 in=false; if(!in){// check contact points for(int i=0;i<n;i++){ checkit(i); } if(np>0){ id=0; r=rmax; for(int i=0;i<np;i++){//find shortest point r1=(x[i]-xs1)*(x[i]-xs1)+(y[i]-ys1)*(y[i]-ys1); if(r1<r){ cx[cid]=x[i]; cy[cid]=y[i]; dx2[cid]=cx[cid+1]-cx[cid]; dy2[cid]=cy[cid+1]-cy[cid]; id=ids[i]; r=r1; if(id==n-1)id1=0; else id1=id+1; }// if }// for cp=Math.atan2(py[id1]-py[id],px[id1]-px[id]); if(np>1){ ci=Math.atan2(cy[cid]-cy[cid+1],cx[cid]-cx[cid+1]); // cp=Math.atan2(py[id1]-py[id],px[id1]-px[id]); c=pi2-(cp-ci);// %twopi;//incident angle cr=cp+pi2-c; setdydx(cid,cr); c1=Math.asin(Math.sin(c)/index);//refracted angle dv=I[cid+1]*getreflections(c,c1);// reflected intensity I[cid]=I[cid+1]-dv;// first refracted ray intensity clr1[cid]=getColor(dv); clr[cid-1]=getColor(I[cid]); c2=(cp-pi2+c1);//%twopi; }else{// inside ci=c; //cp=Math.atan2(py[id1]-py[id],px[id1]-px[id]); c=pi2-(ci-cp);//incident angle pi2-(cp-ci);// //c2=cp-pi2+c;//reflected ray if(Math.abs(Math.sin(c)*index)<1){ c1=Math.asin(Math.sin(c)*index);//refracted angle cr=cp+pi2-c1;//cp-pi2-ci; setdydx(cid,cr); dv=getreflections(c,c1);// reflected intensity }else dv=1;// total reflection I[cid]=dv;// first reflected ray intensity clr1[cid]=getColor(I[cid+1]-dv); clr[cid-1]=getColor(I[cid]); c2=cp-pi2+c; } cid--;// cid=n0-3; in=true; }// np } if(in){// inside ntc=0; for(int i=cid;i>-1;i--){ if(ntc<maxnt){ if(test2)c2=nextreflection2(i,c2); else c2=nextreflection(i,c2);//%twopi;// find next reflection angle dx2[i]=cx[i+1]-cx[i]; dy2[i]=cy[i+1]-cy[i]; }else{ cx[i]=cx[i+1]; cy[i]=cy[i+1]; } ntc++; } }else{ if(Math.abs(Math.sin(c))<min){ //cy[cid]=cy[cid+1]; if(dxs>0)cx[cid]=xmax*2; else cx[cid]=xmin*2; cy[cid]=cx[cid]*Math.tan(c); }else{ if(Math.sin(c)>0)cy[cid]=ytop; else cy[cid]=-ytop; cx[cid]=cx[cid+1]+(cy[cid]-cy[cid+1])/Math.tan(c); } }
    public void checkit (int i) { if(i==n-1)id=0; else id=i+1; if(px[id]==px[i]){ xt=px[id]; yt=m*xt+b; }else{ m1=(py[id]-py[i])/(px[id]-px[i]); if(m==m1)return; b1=py[i]-m1*px[i]; xt=(b1-b)/(m-m1); yt=m*xt+b; } // what if slope equal zero? if( (xt-xs1)*dxs+(yt-ys1)*dys>0 && (xt-px[i])*(xt-px[id])+(yt-py[i])*(yt-py[id])<0){//intersect point x[np]=xt; y[np]=yt; ids[np]=i; in=true; np++; } }
    public java.awt.Color getColor (double v) { iv=(int)(v*255); if(iv<0){ //System.out.println("**iv="+iv+",cid="+cid); iv=0; }else if(iv>255){ //System.out.println("**iv="+iv+",cid="+cid); iv=255; } if(showall)return new java.awt.Color(0,0,255); return new java.awt.Color(0,0,iv,iv); } public double getreflections(double c1,double c2){ if(Math.abs(Math.sin(c1+c2))<min)dv1=(index-1)/(index+1); else dv1=Math.sin(c1-c2)/Math.sin(c1+c2); return dv1*dv1; } public double getreflectionp(double c1,double c2){ if(Math.abs(Math.sin(c1+c2))<min)dv1=(index-1)/(index+1); else dv=Math.tan(c1-c2)/Math.tan(c1+c2); return dv1*dv1; } public void setdydx(int i,double c){ if(Math.abs(Math.sin(c))<min){ if(Math.cos(c)>0)dx[i]=2*xmax-dx[i]; else dx[i]=2*xmin-dx[i]; dy[i]=dx[i]*Math.tan(c); }else{ if(Math.sin(c)>0)dy[i]=ytop-cy[i]; else dy[i]=-ytop-cy[i]; dx[i]=dy[i]/Math.tan(c); } }
    public double nextreflection2 (int j,double c) {// global id th side np=0; for(int i=0;i<n;i++){ if(i==id)continue; // itself m=Math.tan(c); b=cy[j+1]-m*cx[j+1]; if(i==n-1)id1=0; else id1=i+1; if(px[id1]==px[i]){ xt=px[i]; yt=m*xt+b; cp=pi2; }else{ cp=Math.atan2(py[id1]-py[i],px[id1]-px[i]); m1=Math.tan(cp); b1=py[i]-m1*px[i]; if(m==m1)continue; xt=(b-b1)/(m1-m); yt=m*xt+b; } if( (xt-px[i])*(xt-px[id1])+(yt-py[i])*(yt-py[id1])<0){//intersect point (xt-cx[j])*Math.cos(c)+(yt-cy[j])*Math.sin(c)>0 && cx[j]=xt; cy[j]=yt; ci=pi2-(c-cp);// incident angle if(Math.abs(index*Math.sin(ci))<1){ c1=Math.asin(index*Math.sin(ci));// refracted angle cr=cp+pi2-c1; if(Math.abs(Math.sin(cr))<min){// horizontal ray if(Math.cos(cr)>0)dx[j]=2*xmax-xt; else dx[j]=2*xmin-xt; dy[j]=dx[j]*Math.tan(cr); }else{ if(Math.sin(cr)>0)dy[j]=ytop-yt; else dy[j]=-ytop-yt; dx[j]=xt+dy[j]/Math.tan(cr); } dv=I[j+1]*getreflections(ci,c1); }else {// total reflection dv=I[j+1]; } // if(dv>1)dv=0;// impossible ,error? if(j>0)clr[j-1]=getColor(dv); I[j]=dv; clr1[j]=getColor(I[j+1]-I[j]);// refracted out intensity id=i; return cp-pi2+ci; } } return 0; }
    public double nextreflection1 (int j,double c) {// global id th side np=0; m=Math.tan(c); b=cy[j+1]-m*cx[j+1]; for(int i=0;i<n;i++){ if(i==id)continue; // itself if(i==n-1)id1=0; else id1=i+1; if(px[id1]==px[i]){ xt=px[i]; yt=m*xt+b; cp=pi2; }else{ cp=Math.atan2(py[id1]-py[i],px[id1]-px[i]); m1=Math.tan(cp); b1=py[i]-m1*px[i]; if(m==m1)continue; xt=(b-b1)/(m1-m); yt=m*xt+b; } if( (xt-px[i])*(xt-px[id1])+(yt-py[i])*(yt-py[id1])<0){//intersect point (xt-cx[j])*Math.cos(c)+(yt-cy[j])*Math.sin(c)>0 && x[np]=xt; y[np]=yt; ids[np]=i; cps[np]=cp; np++; } } r=rmax; if(np==0)return 0; for(int i=0;i<np;i++){// find shortest one r1=(cx[j+1]-x[i])*(cx[j+1]-x[i])+(cy[j+1]-y[i])*(cy[j+1]-y[i]); if(r1<r){ id2=ids[i]; xt=x[i]; yt=y[i]; cp=cps[i]; r=r1; } } cx[j]=xt; cy[j]=yt; ci=pi2-(c-cp);// incident angle if(Math.abs(index*Math.sin(ci))<1){ c1=Math.asin(index*Math.sin(ci));// refracted angle cr=cp+pi2-c1; if(Math.abs(Math.sin(cr))<min){ if(Math.cos(cr)>0)dx[j]=2*xmax-xt; else dx[j]=2*xmin-xt; dy[j]=dx[j]*Math.tan(cr); }else{ if(Math.sin(cr)>0)dy[j]=ytop-yt; else dy[j]=-ytop-yt; dx[j]=xt+dy[j]/Math.tan(cr); } dv=I[j+1]*getreflections(ci,c1); }else { dv=I[j+1]; } // if(dv>1)dv=0;// impossible ,error? if(j>0)clr[j-1]=getColor(dv); I[j]=dv; clr1[j]=getColor(I[j+1]-I[j]);// refracted out intensity id=id2; np=0; return cp-pi2+ci; }
    public void findnext(int j,double c){ np=0; m=Math.tan(c); b=cy[j+1]-m*cx[j+1]; for(int i=0;i<n;i++){ if(i==id)continue; // itself if(i==n-1)id1=0; else id1=i+1; if(px[id1]==px[i]){ xt=px[i]; yt=m*xt+b; cp=pi2; }else{ cp=Math.atan2(py[id1]-py[i],px[id1]-px[i]); if(px[id1]==px[i]){ xt=px[i]; }else{ m1=Math.tan(cp); b1=py[i]-m1*px[i]; if(m==m1)continue; xt=(b-b1)/(m1-m); } yt=m*xt+b; } if( (xt-px[i])*(xt-px[id1])+(yt-py[i])*(yt-py[id1])<0){//intersect point (xt-cx[j])*Math.cos(c)+(yt-cy[j])*Math.sin(c)>0 && x[np]=xt; y[np]=yt; ids[np]=i; cps[np]=cp; np++; } } r=rmax; if(np==0)return ; id2=-1; for(int i=0;i<np;i++){// find shortest one r1=(cx[j+1]-x[i])*(cx[j+1]-x[i])+(cy[j+1]-y[i])*(cy[j+1]-y[i]); if(r1<r){ //if((x[i]-x[j+1])+m*(y[i]-y[j+1])>0){ id2=ids[i]; xt=x[i]; yt=y[i]; cp=cps[i]; r=r1; //} } } } public double nextreflection (int j,double c) {// global id th side findnext(j,c);// find netx contact point , global id, xt,yt,cp cx[j]=xt; cy[j]=yt; ci=pi2-(c-cp);// incident angle if(Math.abs(index*Math.sin(ci))<1){ c1=Math.asin(index*Math.sin(ci));// refracted angle cr=cp+pi2-c1; if(Math.abs(Math.sin(cr))<min){ if(Math.cos(cr)>0)dx[j]=2*xmax-xt; else dx[j]=2*xmin-xt; dy[j]=dx[j]*Math.tan(cr); }else{ if(Math.sin(cr)>0)dy[j]=ytop-yt; else dy[j]=-ytop-yt; dx[j]=xt+dy[j]/Math.tan(cr); } dv=I[j+1]*getreflections(ci,c1); }else { dv=I[j+1]; } // if(dv>1)dv=0;// impossible ,error? if(j>0)clr[j-1]=getColor(dv); I[j]=dv; clr1[j]=getColor(I[j+1]-I[j]);// refracted out intensity id=id2; np=0; return cp-pi2+ci; }
    public void dragit(){ if(checkit)for(int i=0;i<n;i++){ checkdrag(i); } if(dragid==n-1)id1=0; else id1=dragid+1; if(dragid==0)id=n-1; else id=dragid-1; m=(px[id]-px[dragid])*(px[id1]-px[dragid])+(py[id]-py[dragid])*(py[id1]-py[dragid]); m=m/Math.sqrt(((px[id]-px[dragid]))*((px[id]-px[dragid]))+(py[id]-py[dragid])*(py[id]-py[dragid]))/Math.sqrt((px[id1]-px[dragid])*(px[id1]-px[dragid])+(py[id1]-py[dragid])*(py[id1]-py[dragid])); msg="angle="+((int)(Math.acos(m)*c2a*10+0.5)/10.); } public void checkdrag (int dragid) { if(dragid==n-1)id1=0; else id1=dragid+1; if(dragid==0)id=n-1; else id=dragid-1; c=Math.atan2(py[id1]-py[dragid],px[id1]-px[dragid]); c1=Math.atan2(py[id1]-py[id],px[id1]-px[id]); if(c>c1 &&c-c1<pi){ System.out.println("c="+c+",c1="+c1); r=(py[id1]-py[id])*(py[id1]-py[id])+(px[id1]-px[id])*(px[id1]-px[id]); m=((py[id1]-py[dragid])*(py[id1]-py[id])+(px[id1]-px[dragid])*(px[id1]-px[id]))/r; xt=m*(px[id1]-px[id]);//b yt=m*(py[id1]-py[id]); px[dragid]-=2*(xt-(px[id1]-px[dragid]));// 2(a-b) py[dragid]-=2*(yt-(py[id1]-py[dragid])); } if(dragid==n-1)for(int i=n;i<n0;i++){ px[i]=px[dragid]; py[i]=py[dragid]; } }
    public void setit () { for(int i=n;i<n0;i++){ px[i]=px[0]; py[i]=py[0]; } sizep=size/Math.sqrt(n); checkit=false; _step(); }
    EJSVIEW: Click link to view it's content
    Control variables:(testing)
    double,int,double,int
    title="Frame"
    layout=border
    visible=true
    location="26,4"
    size="542,571"
    position=center
    autoscaleX=false
    autoscaleY=false
    minimumX=xmin
    maximumX=xmax
    minimumY=ymin
    maximumY=ymax
    square=true
    numberOfElements=n
    x=px
    y=py
    sizeX=sizep
    sizeY=sizep
    enabledPosition=true
    elementSelected=dragid
    dragAction=dragit ()
    xData=px
    yData=py
    x=pdx
    y=pdy
    enabledPosition=drag
    dragAction=if(pdx*pdx+pdy*pdy>0){ for(int i=0;i<n0;i++){ px[i]=px[i]+pdx; py[i]=py[i]+pdy; } pdx=pdy=0; }
    closed=true
    lineColor=BLACK
    fillColor=LIGHTGRAY
    drawingLines=true
    drawingFill=false
    x=xs
    y=ys
    sizeX=dxs
    sizeY=dys
    enabledPosition=true
    enabledSize=true
    dragAction=c=Math.atan2(dys,dxs); dxs=d*Math.cos(c); dys=d*Math.sin(c);
    lineColor=BLUE
    fillColor=BLUE
    lineWidth=stroke
    numberOfElements=nt0
    x=cx
    y=cy
    sizeX=dx2
    sizeY=dy2
    lineColor=clr
    numberOfElements=nt0
    x=cx
    y=cy
    sizeX=dx
    sizeY=dy
    lineColor=clr1
    xData=cx
    yData=cy
    visible=false
    closed=false
    lineColor=clr
    y=ymax
    pixelSize=true
    text=%msg%
    elementposition=NORTH
    position=south
    layout=HBOX
    borderType=LOWERED_ETCHED
    layout=HBOX
    variable=rca
    minimum=0.0
    maximum=180
    format="rotate=0.00"
    ticks=37
    closest=true
    dragaction=_initialize()
    variable=n
    minimum=3
    maximum=n0
    format="N=0"
    dragaction=_initialize()
    size="80,0"
    variable=index
    minimum=1.05
    maximum=2.5
    format="index of refraction=0.000"
    layout=GRID:0,1,0,0
    variable=showall
    selected=false
    text="show all"
    variable=drag
    text="drag mode"
    layout=GRID:0,1,0,0
    variable=checkit
    text="check"
    image=/org/opensourcephysics/resources/controls/images/reset.gif
    action=_reset()
    position=east
    variable=maxnt
    minimum=1
    maximum=nt0-2
    orientation=VERTICAL
    enabled=showall