function th=fan_angles(r,r2,th1)
%
% This solution is much more complex than you need, given that the
% initial configuration puts the links in a right triangle; Note that the
% solution below works over a full cycle of the linkage -- giving the
% angles of all links if the angle of link 1 is known.
%
% calculate angles of components in 4 bar linkage
% r  : 	lengths of components
% r2 :	r'*r, lengths of compoenents squared
%
% assumes that angle of ground link (link 4) is zero and angle of 
% first link (theta_1) is given; assumes that th3 is in range 0 to pi
%

% use dirty (and expensive) trick to get th4 in the range 0 to pi 
gam1 = acos(cos(th1));
%
[rm, rm2] = law_cos_len(r2(1,1), r2(4,4), r2(1,4), gam1);
gam2      = law_cos_ang(r2(4,4), r2(1,1), rm2, r(1)*rm);
gam3      = pi - gam1 - gam2;
%
gam4      = law_cos_ang(rm2, r2(2,2), r2(3,3), r2(2,3));
gam5      = law_cos_ang(r2(3,3), r2(2,2), rm2, r(2)*rm);
%
gam8      = pi - gam4 - gam5;
if (sin(th1) > 0.0e0) 
	gam6      = gam8 + gam3;
else
	gam6      = gam8 - gam3;
end
th3 = -gam6;
%
%[rn, rn2] = law_cos_len(r2(4,4), r2(3,3), r2(4,3), gam6);
%gam7      = law_cos_ang(rn2, r2(1,1), r2(2,2), r(1,2));
%
th2 = pi - gam4 - gam6;
%
% set values
%
th = [th1 th2 th3]';
