function prisma(action)
% 1. Skrippt App. Soft. fuer Fortgeschrittene
% Aziz Sinur &  Kortschak Martin 
% 20.03.00 - 26.06.00




global fig1
global axes_1
global axes_2
global kante_1
global kante_2
global Xbasis
global X2basis
global Yle
global c
global cmin
global cmax
global sp
global spa
global aufloes


% Variablen für den roten Strahl.
global licht_ein
global licht_in1_r
global licht_in2_r
global licht_aus_r
global n_r
global Xli1_r
global Yli1_r
global beta1_r
global beta2_r
global alpha2_r
global Pxy_r
global px_r
global py_r
global ap_r
global k_r
global oneline_r
global slider_r
global slider_r_text_a 
global slider_r_text_b 
global slider_r_text_c
global slider_r_text_d

% Variablen für den violeten Strahl.
global licht_in1_v
global licht_in2_v
global licht_aus_v
global n_v
global Xli1_v
global Yli1_v
global beta1_v
global beta2_v
global alpha2_v
global Pxy_v
global px_v
global py_v
global ap_v
global k_v
global oneline_v
global slider_v
global slider_v_text_a 
global slider_v_text_b 
global slider_v_text_c
global slider_v_text_d



if nargin == 0,
  action= 'init';
end


switch action  
  
  
  %Anfangsparameter  
  case 'init'
    fig1=figure(1);clf;
    axes_1 = axes( ...
	'Parent',fig1, ...
	'Units','normalized', ...
	'Position',[0 0 1 1], ...
	'Visible','off' ...
	);
    
    axes_2 = axes( ...
	'Parent',fig1,...
	'Units','normalized',...
	'Position',[0.1 0.3 0.8 0.6],...
	'Visible','on'...
	);
    axis equal;
    axis([-6, 6, -2, 4]);
    title('Brechung am Prisma','fontsize',15); 
    line([-6,6],[0,0],'color','black','linestyle','-  -');
    line([0,0],[0,2],'color','black','linestyle','-  -');
    grid on;
    
    % Seitenkanten v. Prisma
    Xbasis = -1.5;
    kante_1 = line( ...
	'Parent',axes_2, ...
	'ButtonDownFcn','prisma hit_k1', ...
	'EraseMode','xor', ...
	'Color','blue', ...
	'XData',[Xbasis,0],...
	'YData',[0,2]);
    
    X2basis=1.5;
    kante_2 = line( ...
	'Parent',axes_2,	...
	'ButtonDownFcn','prisma hit_k2', ...
	'EraseMode','xor', ...
	'Color','blue', ...
	'XData',[X2basis,0],...
	'YData',[0,2]);
    
    %einfallendes Licht
    Yle=-2;  
    licht_ein = line( ...
	'Parent',axes_2,	...
	'ButtonDownFcn','prisma hit_le', ...
	'EraseMode','xor', ...
	'Color','black', ...
	'XData',[(Xbasis/2),-6],...
	'YData',[1,Yle]);
    
    
    % roter Begrenzungsstrahl:
    
    % Sliderparameter für Brechzahl 
    position   = [0.1 0.1 0.3 0.03];
    min       = 1.01;
    max       = 2;
    n_r         = 1.1;
    sliderstep = [0.01 0.1];
    slider_r = uicontrol( ...
	'Parent',fig1, ...
	'Style','slider', ...
	'String','Clear', ...
	'Min',min, ...
	'Max',max, ...
	'Value',n_r, ...
	'SliderStep',sliderstep, ...
	'Units','normalized', ...
	'Position',position, ...
	'ToolTipString','Slider for n(rot)', 	...
	'Visible','on', 	...
	'Callback','prisma slider_r' );              
        
    textpos  = [position(1),position(2)+position(4)/2+0.045];
    slider_r_text_a = text( ...
	'Position',textpos, ...
	'Parent',axes_1, ...
	'HorizontalAlignment','center', ...
	'VerticalAlignment','middle', ...
	'String',sprintf('%1.2f',min) ...
	);
    
    textpos  = [position(1)+position(3),position(2)+position(4)/2+0.045];                  
    slider_r_text_b = text( ...
	'Position',textpos, ...
	'Parent',axes_1, ...
	'HorizontalAlignment','center', 	...
	'VerticalAlignment','middle', ...
	'String',sprintf('%1.2f',max) ...
	);
    
    textpos  = [position(1)+position(3)/2,position(2)+position(4)/2+0.045];                  
    slider_r_text_c = text( ...
	'Position',textpos, ...
	'Parent',axes_1,	...
	'HorizontalAlignment','center', ...
	'VerticalAlignment','middle', ...
	'String',sprintf('%1.2f',n_r) ...
	);
    
    textpos  = [position(1)+position(3)/2,position(2)+position(4)/2-0.045];                  
    slider_r_text_d = text( ...
	'Position',textpos, ...
	'Parent',axes_1, ...
	'HorizontalAlignment','center', ...
	'VerticalAlignment','middle', ...
	'String','Brechzahl rotes Licht' ...
	);                                  
    
    
    % Berechnung Strahlengang
    
    beta1_r=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_r );
    if beta1_r<0, beta1_r=2*pi-beta1_r;end
    if beta1_r>pi/2, beta1_r = pi-beta1_r;end
    Pxy_r = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
	+[(Xbasis/2);1]+...
	(tan(beta1_r)/sqrt((Xbasis)^2+4))...
	*[(-Xbasis);(2)];
    px_r=Pxy_r(1)-Xbasis/2;
    py_r=Pxy_r(2)-1;
    
    if py_r<0 & Xbasis/2-px_r/py_r<X2basis,   
      Xli1_r=  Xbasis/2-px_r/py_r;
      Yli1_r=0;
      oneline_r=0;
      ap_r=([Xli1_r;Yli1_r]+...
	  (  (2*X2basis-2*Xli1_r-X2basis*Yli1_r)/(2*px_r-X2basis*py_r)  )*...
	  [px_r;-py_r]);
    else Xli1_r=0;Yli1_r=1-(Xbasis*py_r)/(2*px_r);
      ap_r=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_r+py_r*X2basis))*[px_r;py_r];
      oneline_r=1;  
    end
    
    beta2_r=pi/2-winkel(Xli1_r,Yli1_r,ap_r(1),ap_r(2),X2basis,0,0,2);
    alpha2_r=asin(n_r*sin(beta2_r));
    if ((Xli1_r>(ap_r(1)-ap_r(2)*2/X2basis))& oneline_r==0) |...
	  ((Yli1_r<(ap_r(2)-ap_r(1)*X2basis/2))&oneline_r==1),
      k_r=-1;
    else k_r=1; 
    end
    LA_r=(ap_r+[2;X2basis]+tan(alpha2_r)*[X2basis;-2]*k_r);
    rLA_r=LA_r-ap_r;
    Xla_r=6;
    Yla=ap_r(2)+((6-ap_r(1))/rLA_r(1))*rLA_r(2);
    
    %Licht im Prisma
    licht_in1_r = line (...
	'EraseMode','xor',...
	'Color','black',...
	'XData',[Xbasis/2,Xli1_r],...
	'YData',[1,Yli1_r]);
    
    licht_in2_r =line  (...
	'EraseMode','xor',...
	'Color','black',...
	'XData',[Xli1_r,ap_r(1)],...
	'YData',[Yli1_r,ap_r(2)]);
    
    %austretender Lichtstrahl
    licht_aus_r = line (...
	'EraseMode','xor',...
	'Color','black',...
	'XData',[Xla_r,ap_r(1)],...
	'YData',[Yla,ap_r(2)]);
    
    
    %violetter Begrenzungsstrahl:   
    
    %Sliderparameter für Brechzahl
    
    position   = [0.6 0.1 0.3 0.03];
    min       = 1.01;
    max       = 2;
    n_v         = 1.5;
    sliderstep = [0.01 0.1];
    slider_v = uicontrol( ...
	'Parent',fig1, ...
	'Style','slider', ...
	'String','Clear', ...
	'Min',min, ...
	'Max',max, ...
	'Value',n_v, ...
	'SliderStep',sliderstep, ...
	'Units','normalized', ...
	'Position',position, ...
	'ToolTipString','Slider for n(violett)', 	...
	'Visible','on', 	...
	'Callback','prisma slider_v' );              
        
    textpos  = [position(1),position(2)+position(4)/2+0.045];
    slider_v_text_a = text( ...
	'Position',textpos, ...
	'Parent',axes_1, ...
	'HorizontalAlignment','center', ...
	'VerticalAlignment','middle', ...
	'String',sprintf('%1.2f',min) ...
	);
    
    textpos  = [position(1)+position(3),position(2)+position(4)/2+0.045];                  
    slider_v_text_b = text( ...
	'Position',textpos, ...
	'Parent',axes_1, ...
	'HorizontalAlignment','center', 	...
	'VerticalAlignment','middle', ...
	'String',sprintf('%1.2f',max) ...
	);
    
    textpos  = [position(1)+position(3)/2,position(2)+position(4)/2+0.045];                  
    slider_v_text_c = text( ...
	'Position',textpos, ...
	'Parent',axes_1,	...
	'HorizontalAlignment','center', ...
	'VerticalAlignment','middle', ...
	'String',sprintf('%1.2f',n_v) ...
	);
    
    textpos  = [position(1)+position(3)/2,position(2)+position(4)/2-0.045];                  
   slider_v_text_d = text( ...
       'Position',textpos, ...
       'Parent',axes_1, ...
       'HorizontalAlignment','center', ...
       'VerticalAlignment','middle', ...
       'String','Brechzahl violettes Licht' ...
       );                                  
   
   
   % Berechnung Strahlengang
   
   beta1_v=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_v );
   if beta1_v<0, beta1_v=2*pi-beta1_v;end
   if beta1_v>pi/2, beta1_v = pi-beta1_v;end
   Pxy_v = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_v)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_v=Pxy_v(1)-Xbasis/2;
   py_v=Pxy_v(2)-1;
   
   if py_v<0 & Xbasis/2-px_v/py_v<X2basis,   
     Xli1_v=  Xbasis/2-px_v/py_v;
     Yli1_v=0;
     oneline_v=0;
     ap_v=([Xli1_v;Yli1_v]+...
	 (  (2*X2basis-2*Xli1_v-X2basis*Yli1_v)/(2*px_v-X2basis*py_v)  )*...
	 [px_v;-py_v]);
   else Xli1_v=0;Yli1_v=1-(Xbasis*py_v)/(2*px_v);
     ap_v=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_v+py_v*X2basis))*[px_v;py_v];
     oneline_v=1;  
   end
   
   beta2_v=pi/2-winkel(Xli1_v,Yli1_v,ap_v(1),ap_v(2),X2basis,0,0,2);
   alpha2_v=asin(n_v*sin(beta2_v));
   if ((Xli1_v>(ap_v(1)-ap_v(2)*2/X2basis))& oneline_v==0) |...
	 ((Yli1_v<(ap_v(2)-ap_v(1)*X2basis/2))&oneline_v==1),
     k_v=-1;
   else k_v=1; 
   end
   LA_v=(ap_v+[2;X2basis]+tan(alpha2_v)*[X2basis;-2]*k_v);
   rLA_v=LA_v-ap_v;
   Xla_v=6;
   Yla=ap_v(2)+((6-ap_v(1))/rLA_v(1))*rLA_v(2);
   
   
   licht_in1_v = line (...
       'EraseMode','xor',...
       'Color','black',...
       'XData',[Xbasis/2,Xli1_v],...
       'YData',[1,Yli1_v]);
   
   licht_in2_v =line  (...
       'EraseMode','xor',...
       'Color','black',...
       'XData',[Xli1_v,ap_v(1)],...
       'YData',[Yli1_v,ap_v(2)]);
   
   licht_aus_v = line (...
       'EraseMode','xor',...
       'Color','black',...
       'XData',[Xla_v,ap_v(1)],...
       'YData',[Yla,ap_v(2)]);
   
   
   %Dispersion:
   colormap default;   
   cmin=0;
   cmax=1;
   aufloes=200;
   c=[cmin:1/(aufloes-2):cmax];
   %spectrum: generiert die Vektoren für PATCH  
   [x,y,xa,ya] = spectrum(n_r,n_v,Xbasis,X2basis,Yle,aufloes);
      
   %Licht im Prisma
   sp = patch(x,y,c, 'Edgecolor','none');
   %austretendes Licht
   spa=patch(xa,ya,c,'Edgecolor','none');
   caxis([cmin cmax]);
   
   
 %Anklicken der Linien 
 case 'hit_k1'
   axes(axes_2);
   set(fig1,'WindowButtonUpFcn','prisma end_move_k1');
   set(fig1,'WindowButtonMotionFcn','prisma move_k1');   
   set(kante_1,'Color','red');
   set(licht_ein,'visible',' off');
   set(licht_in1_r,'Visible','off');   
   set(licht_in2_r,'Visible','off');
   set(licht_aus_r,'Visible','off');
   set(licht_in1_v,'Visible','off');   
   set(licht_in2_v,'Visible','off');
   set(licht_aus_v,'Visible','off');
   set(sp,'Visible','off');  
   set(spa,'Visible','off');
 
 case 'hit_k2'
   axes(axes_2);
   set(fig1,'WindowButtonUpFcn','prisma end_move_k2');
   set(fig1,'WindowButtonMotionFcn','prisma move_k2');   
   set(kante_2,'Color','red');
   set(licht_ein,'visible',' off');
   set(licht_in1_r,'Visible','off');   
   set(licht_in2_r,'Visible','off');
   set(licht_aus_r,'Visible','off');
   set(licht_in1_v,'Visible','off');   
   set(licht_in2_v,'Visible','off');
   set(licht_aus_v,'Visible','off'); 
   set(sp,'Visible','off');  
   set(spa,'Visible','off');
 
 case 'hit_le'
   axes(axes_2);
   set(fig1,'WindowButtonUpFcn','prisma end_move_le');
   set(fig1,'WindowButtonMotionFcn','prisma move_le');   
   set(licht_ein,'Color','red');
   set(sp,'Visible','off');
   set(spa,'Visible','off');
  
%Verschieben der Linien mit Einschränkungen: 

case 'move_k1'
   cp = get(axes_2,'CurrentPoint');
   Xbasis = (2*cp(1,1)/(2-cp(1,2)));   
   if Xbasis >=-0.1, Xbasis = -0.1; end 
   if Xbasis<=-3, Xbasis=-3;end
   set(kante_1,'XData',[Xbasis, 0]);
   set(licht_ein,'XData',[(Xbasis/2),-6]);
   if Yle <=0, set(licht_ein,'YData',[1,0]); end
   if Yle>=(1+(-3*Xbasis-(Xbasis^2)/4)) | Yle>=6, 
     if (1+(-3*Xbasis-(Xbasis^2)/4)) >6, 
       y=6;   
     else y=(1+(-3*Xbasis-(Xbasis^2)/4)); 
     end
     
     set(licht_ein,'YData',[1,y]);
   end  
 
case 'move_k2'
   cp = get(axes_2,'CurrentPoint');
   X2basis = (2*cp(1,1)/(2-cp(1,2)));   
   if X2basis <=0.1, X2basis = 0.1; end 
   if X2basis>=6, X2basis=6;end
   set(kante_2,'XData',[X2basis, 0]); 
   
case 'move_le'
   cp = get(axes_2,'CurrentPoint');
   if cp(1,1)==Xbasis/2, cp(1,1)=Xbasis/2+eps; end
   Yle = 1+(-6-Xbasis/2)* (1 - cp(1,2)) / ((Xbasis/2) - cp(1,1));   
   if Yle<=(2+(12/Xbasis)) , Yle=(2+(12/Xbasis)); end 
   set(licht_ein,'YData',[1,Yle]);
   if Yle>=(1+(-3*Xbasis-(Xbasis^2)/4)) | (cp(1,1)>Xbasis/2 & cp(1,2)>1),  
     Yle=(1+(-3*Xbasis-(Xbasis^2)/4));
     set(licht_ein,'YData',[1,(1+(-3*Xbasis-(Xbasis^2)/4))]);
   end  
   %beim Veränderung des Einfallenden Lichtes bleiben die 2
   %Begrenzungsstrahlen sichtbar
   
   %roter Begrenzungsstrahl:
   %Berechnung:
   beta1_r=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_r );
   if beta1_r<0, beta1_r=2*pi-beta1_r;end
   Pxy_r = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_r)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_r=Pxy_r(1)-Xbasis/2;
   py_r=Pxy_r(2)-1;
   
   if py_r<0 & Xbasis/2-px_r/py_r<X2basis,   
     Xli1_r=  Xbasis/2-px_r/py_r;
     Yli1_r=0;
     oneline_r=0;
     ap_r=([Xli1_r;Yli1_r]+...
	 (  (2*X2basis-2*Xli1_r-X2basis*Yli1_r)/(2*px_r-X2basis*py_r)  )*...
	 [px_r;-py_r]);
   else Xli1_r=0;Yli1_r=1-(Xbasis*py_r)/(2*px_r);
     ap_r=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_r+py_r*X2basis))*[px_r;py_r];
     oneline_r=1;    
   end
   beta2_r=pi/2-winkel(Xli1_r,Yli1_r,ap_r(1),ap_r(2),X2basis,0,0,2);
   alpha2_r=asin(n_r*sin(beta2_r));
   if ((Xli1_r>(ap_r(1)-ap_r(2)*2/X2basis))& oneline_r==0) |...
	 ((Yli1_r<(ap_r(2)-ap_r(1)*X2basis/2))&oneline_r==1),
     k_r=-1;
   else k_r=1; 
   end
   LA=(ap_r+([2;X2basis]+tan(alpha2_r)*[X2basis;-2]*k_r));
   rLA=LA-ap_r;
   if beta2_r>=asin(1/n_r), 
     set(licht_aus_r, 'visible','off');
   else set(licht_aus_r, 'visible','on');end
   Xla_r=6;
   Yla=ap_r(2)+((6-ap_r(1))/rLA(1))*rLA(2); 
   
   %Zeichnen:
   set(licht_in1_r,'XData',[Xbasis/2,Xli1_r], 'YData',[1,Yli1_r]);
   set(licht_in2_r, 'XData',[Xli1_r,ap_r(1)], 'YData',[Yli1_r,ap_r(2)]);
   set(licht_aus_r, 'XData',[Xla_r,ap_r(1)], 'YData',[Yla,ap_r(2)]); 
   
   %Brechung violett; 
   beta1_v=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_v );
   if beta1_v<0, beta1_v=2*pi-beta1_v;end
   Pxy_v = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_v)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_v=Pxy_v(1)-Xbasis/2;
   py_v=Pxy_v(2)-1;
   
   if py_v<0 & Xbasis/2-px_v/py_v<X2basis,   
     Xli1_v=  Xbasis/2-px_v/py_v;
     Yli1_v=0;
     oneline_v=0;
     ap_v=([Xli1_v;Yli1_v]+...
	 (  (2*X2basis-2*Xli1_v-X2basis*Yli1_v)/(2*px_v-X2basis*py_v)  )*...
	 [px_v;-py_v]);
   else Xli1_v=0;Yli1_v=1-(Xbasis*py_v)/(2*px_v);
     ap_v=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_v+py_v*X2basis))*[px_v;py_v];
     oneline_v=1;    
   end
   beta2_v=pi/2-winkel(Xli1_v,Yli1_v,ap_v(1),ap_v(2),X2basis,0,0,2);
   alpha2_v=asin(n_v*sin(beta2_v));
   if ((Xli1_v>(ap_v(1)-ap_v(2)*2/X2basis))& oneline_v==0) |...
	 ((Yli1_v<(ap_v(2)-ap_v(1)*X2basis/2))&oneline_v==1),
     k_v=-1;
   else k_v=1; 
   end
   LA=(ap_v+([2;X2basis]+tan(alpha2_v)*[X2basis;-2]*k_v));
   rLA=LA-ap_v;
   if beta2_v>=asin(1/n_v), 
     set(licht_aus_v, 'visible','off');
   else set(licht_aus_v, 'visible','on');end
   Xla_v=6;
   Yla=ap_v(2)+((6-ap_v(1))/rLA(1))*rLA(2); 
   %Zeichnen violett
   set(licht_in1_v,'XData',[Xbasis/2,Xli1_v], 'YData',[1,Yli1_v]);
   set(licht_in2_v, 'XData',[Xli1_v,ap_v(1)], 'YData',[Yli1_v,ap_v(2)]);
   set(licht_aus_v, 'XData',[Xla_v,ap_v(1)], 'YData',[Yla,ap_v(2)]); 
   
 
 %Loslassen der Linien:
 case 'end_move_k1'
   set(fig1,'WindowButtonMotionFcn','');
   set(fig1,'WindowButtonUpFcn','');
   set(kante_1,'Color','blue');
   set(licht_ein,'visible','on');
   set(licht_in1_r,'Visible','on');   
   set(licht_in2_r,'Visible','on');
   set(licht_aus_r,'Visible','on');
   set(licht_in1_v,'Visible','on');   
   set(licht_in2_v,'Visible','on');
   set(licht_aus_v,'Visible','on');
   
   %Berechnung roter Begrenzungsstrahl
   beta1_r=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_r );
   if beta1_r<0, beta1_r=2*pi-beta1_r;end
   Pxy_r = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_r)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_r=Pxy_r(1)-Xbasis/2;
   py_r=Pxy_r(2)-1;
   
   if py_r<0 & Xbasis/2-px_r/py_r<X2basis,   
     Xli1_r=  Xbasis/2-px_r/py_r;
     Yli1_r=0;
     online=0;
     ap_r=([Xli1_r;Yli1_r]+...
	 (  (2*X2basis-2*Xli1_r-X2basis*Yli1_r)/(2*px_r-X2basis*py_r)  )*...
	 [px_r;-py_r]);
   else Xli1_r=0;Yli1_r=1-(Xbasis*py_r)/(2*px_r);
     ap_r=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_r+py_r*X2basis))*[px_r;py_r];
     online=1;   
   end
   beta2_r=pi/2-winkel(Xli1_r,Yli1_r,ap_r(1),ap_r(2),X2basis,0,0,2);
   alpha2_r=asin(n_r*sin(beta2_r));
   if ((Xli1_r>(ap_r(1)-ap_r(2)*2/X2basis))& oneline_r==0) |...
	 ((Yli1_r<(ap_r(2)-ap_r(1)*X2basis/2))&oneline_r==1),
     k_r=-1;
   else k_r=1; 
   end
   LA=(ap_r+[2;X2basis]+tan(alpha2_r)*[X2basis;-2]*k_r);
   rLA=LA-ap_r;
   if beta2_r>=asin(1/n_r), 
     set(licht_aus_r, 'visible','off');
   else set(licht_aus_r, 'visible','on');end
   Xla_r=6;
   Yla=ap_r(2)+((6-ap_r(1))/rLA(1))*rLA(2); 
   %Zeichnen des Begrenzungsstrahles:
   set(licht_in1_r,'XData',[Xbasis/2,Xli1_r], 'YData',[1,Yli1_r]);
   set(licht_in2_r, 'XData',[Xli1_r,ap_r(1)], 'YData',[Yli1_r,ap_r(2)]);
   set(licht_aus_r, 'XData',[Xla_r,ap_r(1)], 'YData',[Yla,ap_r(2)]); 
   
   %violett:
   %Berechnung Begrenzungsstrahl
   beta1_v=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_v );
   if beta1_v<0, beta1_v=2*pi-beta1_v;end
   Pxy_v = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_v)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_v=Pxy_v(1)-Xbasis/2;
   py_v=Pxy_v(2)-1;
   
   if py_v<0 & Xbasis/2-px_v/py_v<X2basis,   
     Xli1_v=  Xbasis/2-px_v/py_v;
     Yli1_v=0;
     online=0;
     ap_v=([Xli1_v;Yli1_v]+...
	 (  (2*X2basis-2*Xli1_v-X2basis*Yli1_v)/(2*px_v-X2basis*py_v)  )*...
	 [px_v;-py_v]);
   else Xli1_v=0;Yli1_v=1-(Xbasis*py_v)/(2*px_v);
     ap_v=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_v+py_v*X2basis))*[px_v;py_v];
     online=1;   
   end
   beta2_v=pi/2-winkel(Xli1_v,Yli1_v,ap_v(1),ap_v(2),X2basis,0,0,2);
   alpha2_v=asin(n_v*sin(beta2_v));
   if ((Xli1_v>(ap_v(1)-ap_v(2)*2/X2basis))& oneline_v==0) |...
	 ((Yli1_v<(ap_v(2)-ap_v(1)*X2basis/2))&oneline_v==1),
     k_v=-1;
   else k_v=1; 
   end
   LA=(ap_v+[2;X2basis]+tan(alpha2_v)*[X2basis;-2]*k_v);
   rLA=LA-ap_v;
   if beta2_v>=asin(1/n_v), 
     set(licht_aus_v, 'visible','off');
   else set(licht_aus_v, 'visible','on');end
   Xla_v=6;
   Yla=ap_v(2)+((6-ap_v(1))/rLA(1))*rLA(2); 
   %Zeichnen Begrenzungsstrahl:  
   set(licht_in1_v,'XData',[Xbasis/2,Xli1_v], 'YData',[1,Yli1_v]);
   set(licht_in2_v, 'XData',[Xli1_v,ap_v(1)], 'YData',[Yli1_v,ap_v(2)]);
   set(licht_aus_v, 'XData',[Xla_v,ap_v(1)], 'YData',[Yla,ap_v(2)]); 
   
   %Berechnen und Zeichnen der Dispersion:
   [x,y,xa,ya]=spectrum(n_r,n_v,Xbasis,X2basis,Yle,aufloes);
   set (sp,'Xdata',x,'Ydata',y,'visible','on' );
   set (spa,'Xdata',xa,'Ydata',ya,'visible','on');
   
 case 'end_move_k2'
   set(fig1,'WindowButtonMotionFcn','');
   set(fig1,'WindowButtonUpFcn','');
   set(kante_2,'Color','blue');
   set(licht_ein,'visible','on');
   set(licht_in1_r,'Visible','on');   
   set(licht_in2_r,'Visible','on'); 
   set(licht_aus_r,'Visible','on');
   set(licht_in1_v,'Visible','on');   
   set(licht_in2_v,'Visible','on'); 
   set(licht_aus_v,'Visible','on');
   
   %Brechung rot
   beta1_r=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_r );
   if beta1_r<0, beta1_r=2*pi-beta1_r;end
   Pxy_r = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_r)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   
   
   px_r=Pxy_r(1)-Xbasis/2;
   py_r=Pxy_r(2)-1;
   
   if py_r<0 & Xbasis/2-px_r/py_r<X2basis,   
     Xli1_r=  Xbasis/2-px_r/py_r;
     Yli1_r=0;
     onelone=0;
     ap_r=([Xli1_r;Yli1_r]+...
	 (  (2*X2basis-2*Xli1_r-X2basis*Yli1_r)/(2*px_r-X2basis*py_r)  )*...
	 [px_r;-py_r]);
   else Xli1_r=0;Yli1_r=1-(Xbasis*py_r)/(2*px_r);
     ap_r=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_r+py_r*X2basis))*[px_r;py_r];
     oneline_r=1;
   end
   beta2_r=pi/2-winkel(Xli1_r,Yli1_r,ap_r(1),ap_r(2),X2basis,0,0,2);
   alpha2_r=asin(n_r*sin(beta2_r));
   if ((Xli1_r>(ap_r(1)-ap_r(2)*2/X2basis))& oneline_r==0) |...
	 ((Yli1_r<(ap_r(2)-ap_r(1)*X2basis/2))&oneline_r==1),
     k_r=-1;
   else k_r=1; 
   end
   LA=(ap_r+[2;X2basis]+tan(alpha2_r)*[X2basis;-2]*k_r);
   rLA=LA-ap_r;
   if beta2_r>=asin(1/n_r), 
     set(licht_aus_r, 'visible','off');
   else set(licht_aus_r, 'visible','on');end
   Xla_r=6;
   Yla=ap_r(2)+((6-ap_r(1))/rLA(1))*rLA(2); 
   set(licht_in1_r,'XData',[Xbasis/2,Xli1_r], 'YData',[1,Yli1_r]);
   set(licht_in2_r, 'XData',[Xli1_r,ap_r(1)], 'YData',[Yli1_r,ap_r(2)]);
   set(licht_aus_r, 'XData',[Xla_r,ap_r(1)], 'YData',[Yla,ap_r(2)]); 
   
   %Brechung violett
   beta1_v=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_v );
   if beta1_v<0, beta1_v=2*pi-beta1_v;end
   Pxy_v = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_v)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_v=Pxy_v(1)-Xbasis/2;
   py_v=Pxy_v(2)-1;
   
   if py_v<0 & Xbasis/2-px_v/py_v<X2basis,   
     Xli1_v=  Xbasis/2-px_v/py_v;
     Yli1_v=0;
     onelone=0;
     ap_v=([Xli1_v;Yli1_v]+...
	 (  (2*X2basis-2*Xli1_v-X2basis*Yli1_v)/(2*px_v-X2basis*py_v)  )*...
	 [px_v;-py_v]);
   else Xli1_v=0;Yli1_v=1-(Xbasis*py_v)/(2*px_v);
     ap_v=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_v+py_v*X2basis))*[px_v;py_v];
     oneline_v=1;
   end
   beta2_v=pi/2-winkel(Xli1_v,Yli1_v,ap_v(1),ap_v(2),X2basis,0,0,2);
   alpha2_v=asin(n_v*sin(beta2_v));
   if ((Xli1_v>(ap_v(1)-ap_v(2)*2/X2basis))& oneline_v==0) |...
	 ((Yli1_v<(ap_v(2)-ap_v(1)*X2basis/2))&oneline_v==1),
     k_v=-1;
   else k_v=1; 
   end
   LA=(ap_v+[2;X2basis]+tan(alpha2_v)*[X2basis;-2]*k_v);
   rLA=LA-ap_v;
   if beta2_v>=asin(1/n_v), 
     set(licht_aus_v, 'visible','off');
   else set(licht_aus_v, 'visible','on');end
   Xla_v=6;
   Yla=ap_v(2)+((6-ap_v(1))/rLA(1))*rLA(2); 
   set(licht_in1_v,'XData',[Xbasis/2,Xli1_v], 'YData',[1,Yli1_v]);
   set(licht_in2_v, 'XData',[Xli1_v,ap_v(1)], 'YData',[Yli1_v,ap_v(2)]);
   set(licht_aus_v, 'XData',[Xla_v,ap_v(1)], 'YData',[Yla,ap_v(2)]); 
   %Dispersion
   [x,y,xa,ya]=spectrum(n_r,n_v,Xbasis,X2basis,Yle,aufloes); 
   set (sp,'Xdata',x,'Ydata',y,'visible','on' );
   set (spa,'Xdata',xa,'Ydata',ya,'visible','on');
 
 case 'end_move_le'
   set(fig1,'WindowButtonMotionFcn','');
   set(fig1,'WindowButtonUpFcn','');
   set(licht_ein,'Color','black');
   
   % Brechung rot
   if py_r<0 & Xbasis/2-px_r/py_r<X2basis,   
     Xli1_r=  Xbasis/2-px_r/py_r;
     Yli1_r=0;
     oneline_r=0;
     ap_r=([Xli1_r;Yli1_r]+...
	 (  (2*X2basis-2*Xli1_r-X2basis*Yli1_r)/(2*px_r-X2basis*py_r)  )*...
	 [px_r;-py_r]);
   else Xli1_r=0;Yli1_r=1-(Xbasis*py_r)/(2*px_r);
     ap_r=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_r+py_r*X2basis))*[px_r;py_r];
     oneline_r=1;
   end
   beta2_r=pi/2-winkel(Xli1_r,Yli1_r,ap_r(1),ap_r(2),X2basis,0,0,2);
   alpha2_r=asin(n_r*sin(beta2_r));
   if ((Xli1_r>(ap_r(1)-ap_r(2)*2/X2basis))& oneline_r==0) |...
	 ((Yli1_r<(ap_r(2)-ap_r(1)*X2basis/2))&oneline_r==1),
     k_r=-1;
   else k_r=1; 
   end
   LA=(ap_r+[2;X2basis]+tan(alpha2_r)*[X2basis;-2]*k_r);
   rLA=LA-ap_r;
   if beta2_r>=asin(1/n_r), 
     set(licht_aus_r, 'visible','off');
   else set(licht_aus_r, 'visible','on');end
   Xla_r=6;
   Yla=ap_r(2)+((6-ap_r(1))/rLA(1))*rLA(2); 
   set(licht_in1_r,'XData',[Xbasis/2,Xli1_r], 'YData',[1,Yli1_r]);
   set(licht_in2_r, 'XData',[Xli1_r,ap_r(1)], 'YData',[Yli1_r,ap_r(2)]);
   set(licht_aus_r, 'XData',[Xla_r,ap_r(1)], 'YData',[Yla,ap_r(2)]); 
   
   % Brechung violett
   if py_v<0 & Xbasis/2-px_v/py_v<X2basis,   
     Xli1_v=  Xbasis/2-px_v/py_v;
     Yli1_v=0;
     oneline_v=0;
     ap_v=([Xli1_v;Yli1_v]+...
	 (  (2*X2basis-2*Xli1_v-X2basis*Yli1_v)/(2*px_v-X2basis*py_v)  )*...
	 [px_v;-py_v]);
   else Xli1_v=0;Yli1_v=1-(Xbasis*py_v)/(2*px_v);
     ap_v=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_v+py_v*X2basis))*[px_v;py_v];
     oneline_v=1;
   end
   beta2_v=pi/2-winkel(Xli1_v,Yli1_v,ap_v(1),ap_v(2),X2basis,0,0,2);
   alpha2_v=asin(n_v*sin(beta2_v));
   if ((Xli1_v>(ap_v(1)-ap_v(2)*2/X2basis))& oneline_v==0) |...
	 ((Yli1_v<(ap_v(2)-ap_v(1)*X2basis/2))&oneline_v==1),
     k_v=-1;
   else k_v=1; 
   end
   LA=(ap_v+[2;X2basis]+tan(alpha2_v)*[X2basis;-2]*k_v);
   rLA=LA-ap_v;
   if beta2_v>=asin(1/n_v), 
     set(licht_aus_v, 'visible','off');
   else set(licht_aus_v, 'visible','on');end
   Xla_v=6;
   Yla=ap_v(2)+((6-ap_v(1))/rLA(1))*rLA(2); 
   set(licht_in1_v,'XData',[Xbasis/2,Xli1_v], 'YData',[1,Yli1_v]);
   set(licht_in2_v, 'XData',[Xli1_v,ap_v(1)], 'YData',[Yli1_v,ap_v(2)]);
   set(licht_aus_v, 'XData',[Xla_v,ap_v(1)], 'YData',[Yla,ap_v(2)]); 
   %Dispersion:
   [x,y,xa,ya]=spectrum(n_r,n_v,Xbasis,X2basis,Yle,aufloes);
   set (sp,'Xdata',x,'Ydata',y,'visible','on' );
   set (spa,'Xdata',xa,'Ydata',ya,'visible','on');
   
 case 'slider_r'
   n_r = get(slider_r,'Value');
   set(slider_r_text_c,'String',sprintf('%1.2f',n_r));
   
   %Brechung rot:
   beta1_r=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_r );
   if beta1_r<0, beta1_r=2*pi-beta1_r;end
   Pxy_r = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_r)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)]; 
   px_r=Pxy_r(1)-Xbasis/2;
   py_r=Pxy_r(2)-1;
   
   if py_r<0 & Xbasis/2-px_r/py_r<X2basis,   
     Xli1_r=  Xbasis/2-px_r/py_r;
     Yli1_r=0;
     onelone=0;
     ap_r=([Xli1_r;Yli1_r]+...
	 (  (2*X2basis-2*Xli1_r-X2basis*Yli1_r)/(2*px_r-X2basis*py_r)  )*...
	 [px_r;-py_r]);
   else Xli1_r=0;Yli1_r=1-(Xbasis*py_r)/(2*px_r);
     ap_r=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_r+py_r*X2basis))*[px_r;py_r];
     oneline_r=1;
   end
   beta2_r=pi/2-winkel(Xli1_r,Yli1_r,ap_r(1),ap_r(2),X2basis,0,0,2);
   alpha2_r=asin(n_r*sin(beta2_r));
   if ((Xli1_r>(ap_r(1)-ap_r(2)*2/X2basis))& oneline_r==0) |...
	 ((Yli1_r<(ap_r(2)-ap_r(1)*X2basis/2))&oneline_r==1),
     k_r=-1;
   else k_r=1; 
   end
   LA=(ap_r+[2;X2basis]+tan(alpha2_r)*[X2basis;-2]*k_r);
   rLA=LA-ap_r;
   if beta2_r>=asin(1/n_r), 
     set(licht_aus_r, 'visible','off');
   else set(licht_aus_r, 'visible','on');end
   Xla_r=6;
   Yla=ap_r(2)+((6-ap_r(1))/rLA(1))*rLA(2); 
   set(licht_in1_r,'XData',[Xbasis/2,Xli1_r], 'YData',[1,Yli1_r]);
   set(licht_in2_r, 'XData',[Xli1_r,ap_r(1)], 'YData',[Yli1_r,ap_r(2)]);
   set(licht_aus_r, 'XData',[Xla_r,ap_r(1)], 'YData',[Yla,ap_r(2)]); 
   %Dispersion
   [x,y,xa,ya]=spectrum(n_r,n_v,Xbasis,X2basis,Yle,aufloes); 
   set (sp,'Xdata',x,'Ydata',y,'visible','on' );
   set (spa,'Xdata',xa,'Ydata',ya,'visible','on');
 
 case 'slider_v'
   n_v = get(slider_v,'Value');
   set(slider_v_text_c,'String',sprintf('%1.2f',n_v));
   
   %Brechung violett
   beta1_v=asin(sin((pi/2)-winkel((Xbasis/2),1,-6,Yle,Xbasis,0,0,2 ))/n_v );
   if beta1_v<0, beta1_v=2*pi-beta1_v;end
   Pxy_v = [2;Xbasis] / sqrt(4+(Xbasis)^2)...
       +[(Xbasis/2);1]+...
       (tan(beta1_v)/sqrt((Xbasis)^2+4))...
       *[(-Xbasis);(2)];
   px_v=Pxy_v(1)-Xbasis/2;
   py_v=Pxy_v(2)-1;
   
   if py_v<0 & Xbasis/2-px_v/py_v<X2basis,   
     Xli1_v=  Xbasis/2-px_v/py_v;
     Yli1_v=0;
     onelone=0;
     ap_v=([Xli1_v;Yli1_v]+...
	 (  (2*X2basis-2*Xli1_v-X2basis*Yli1_v)/(2*px_v-X2basis*py_v)  )*...
	 [px_v;-py_v]);
   else Xli1_v=0;Yli1_v=1-(Xbasis*py_v)/(2*px_v);
     ap_v=[Xbasis/2;1]+((X2basis-Xbasis)/(2*px_v+py_v*X2basis))*[px_v;py_v];
     oneline_v=1;
   end
   beta2_v=pi/2-winkel(Xli1_v,Yli1_v,ap_v(1),ap_v(2),X2basis,0,0,2);
   alpha2_v=asin(n_v*sin(beta2_v));
   if ((Xli1_v>(ap_v(1)-ap_v(2)*2/X2basis))& oneline_v==0) |...
	 ((Yli1_v<(ap_v(2)-ap_v(1)*X2basis/2))&oneline_v==1),
     k_v=-1;
   else k_v=1; 
   end
   LA=(ap_v+[2;X2basis]+tan(alpha2_v)*[X2basis;-2]*k_v);
   rLA=LA-ap_v;
   if beta2_v>=asin(1/n_v), 
     set(licht_aus_v, 'visible','off');
   else set(licht_aus_v, 'visible','on');end
   Xla_v=6;
   Yla=ap_v(2)+((6-ap_v(1))/rLA(1))*rLA(2); 
   set(licht_in1_v,'XData',[Xbasis/2,Xli1_v], 'YData',[1,Yli1_v]);
   set(licht_in2_v, 'XData',[Xli1_v,ap_v(1)], 'YData',[Yli1_v,ap_v(2)]);
   set(licht_aus_v, 'XData',[Xla_v,ap_v(1)], 'YData',[Yla,ap_v(2)]); 
   %Dispersion
   [x,y,xa,ya]=spectrum(n_r,n_v,Xbasis,X2basis,Yle,aufloes); 
   set (sp,'Xdata',x,'Ydata',y,'visible','on' );
   set (spa,'Xdata',xa,'Ydata',ya,'visible','on');
   
end

















