% --- start of displayed preamble in the book ---
vardef refractray(expr s,a,p,na,nb)=
 save tI,tn,ia,ib,I,J,sib;pair I,J;
 tI=xpart(p intersectiontimes (s--(s+30cm*dir(a))));
 if tI>=0:
   I=point tI of p;draw s--I;tn=angle(direction tI of p)+90;
   %drawarrow I--(I+dir(tn)*3cm); % normal
   %drawarrow I--(I+dir(180+tn)*3cm);
   ia=tn-angle(s-I);
   % we have na*sind(ia)=nb*sind(ib)
   sib=na*sind(ia)/nb;ib=-angle(1+-+sib,sib);
   J=I+dir(tn+180+ib)*2cm;draw I--J withcolor blue;
 fi;
enddef;
% --- end of displayed preamble in the book ---
defaultfont:="ptmr8r";
warningcheck:=0;
beginfig(1)
numeric u;u=3mm;path p;
p=((5u,-6u)..(4u,-2u)..(4.5u,0)..(5u,5u)) rotated 90;
z0=origin;
for a=-40 step 5 until 140:refractray(z0,a,p,1,1.5);endfor;
draw p withcolor red;
endfig;
end;