%
% 3D parametric plots of a star shape sphere eversion from 
% A Topological Picturebook (pg.116) by George Francis.  
% Thanks to Heidi Burgiel for the parametrization code.
%
clear
c = 10;
o = pi*[0:.01:2]';
n = 4;
r = [-0.5:1/(n-1):0.5];
t = 0.3;
m = length(o);

gammax = zeros(m,1);
gammay = zeros(m,1);
h      = zeros(m,n);
X      = zeros(m,n);
Y      = zeros(m,n);
Z      = zeros(m,n);

gammax = cos(o*(1-n)) + ((n-1)/(n+1)) * cos(o*(n+1));
gammay = sin(o*(1-n)) + ((n-1)/(n+1)) * sin(o*(n+1));

for i = 1:n
  h(:,i) = ((2*n-1)/(2*n)) * sin(2*n*o) + r(i)*sin(n*o);
end

B = sin(pi*t);
A = cos(pi*t);

for i = 1:n
  X(:,i) = (1-n)*A*sin(o) + B*gammax + r(i)*cos(o);
  Y(:,i) = (n-1)*A*cos(o) + B*gammay + r(i)*sin(o);
  Z(:,i) = (r(i) + (3*(n-1)/n) *cos(o*n)) * A + h(:,i)*B; 
end

figure
mobj = plot3( X(:,1), Y(:,1), Z(:,1) );
writeoogl( mobj, 'starline.vect');
xlabel('x');
ylabel('y');
zlabel('z');

figure
[cmat, mobj] = contour3(X,Y,Z);
xlabel('x');
ylabel('y');
zlabel('z');

writeoogl( mobj, 'star.vect', 1, 1, 1);


figure
mobj = surf(X,Y,Z);
xlabel('x');
ylabel('y');
zlabel('z');

writeoogl( mobj, 'star.mesh');
