clc, clear all, close all 

% input values
theta1 = input('Angle of incidence in degree in region 1, theta1=');
n1 = input('Index of refraction in region 1, n1=');
n2 = input('Index of refraction in region 2, n2=');

% separation
h=line([0 1],[.5 .5]);
set(h,'color','k','LineWidth',[4.5])
axis([0 1 0 1])
hold on

% vertical line
h=line([.5 .5],[0 1]);
set(h,'linestyle','-.')


x1 = .5;
y1 = .5;

% light ray 1
m1 = tand(theta1);
text(.47,.6,'\theta_1')
x=0:.001:.5;
y=-m1*(x-x1)+y1;
plot(x,y)

% find the solution
if(n1>n2 && abs((n1*sind(theta1)/n2) > 1))
  x=.5:.001:1;
  y=m1*(x-x1)+y1;
  plot(x,y)
  title('No light passes into region 2')
  text(.8,.9,['\theta_1=',num2str(theta1),'^o'])
  error('No light passes into region 2');
else
  theta2 = asind((n1/n2)*sind(theta1));
  
  % light ray 2
  m2 = tand(theta2);
  x=.5:.001:1;
  y=-m2*(x-x1)+y1;
  plot(x,y)
  text(.51,.4,'\theta_2')

  % text the result
  text(.8,.9,['\theta_1=',num2str(theta1),'^o'])
  text(.8,.8,['\theta_2=',num2str(theta2),'^o'])
end
axis off
