function pos=wrldtrv2(city1,city2);
%WRLDTRV2 Calculate and plot great circle distances.
%	WRLDTRV2 is used by the demo WRLDTRV.

%	Ned Gulley, 6-21-93
%	Copyright (c) 1984-94 by The MathWorks, Inc.

% East longitude is POSITIVE
% North latitude is POSITIVE
if nargin==0,
    %clf
    load topo
    % Sphere of 24 makes 15 degrees between lat and long lines
    [x,y,z]=sphere(24);
    h=surface(x,y,z,'FaceColor','texture','CData',topo);
    % First I'm rotating it so the axes won't look stupid
    rotate(h,[0 90],45,[0 0 0]);
    % Now view the globe so the Greenwich meridian is directly
    % facing the viewer
    view(-135,25);
    colormap(topomap1);
    set(gca,'AspectRatio',[1 1], ...
	'NextPlot','add', ...
	'Visible','off');

elseif nargin==1,
    phi1=city1(1)*pi/180;
    tht1=(city1(2)+135)*pi/180; 
    [xp1,yp1,zp1]=sph2cart(tht1,phi1,1);
    plot3(xp1,yp1,zp1,'r.', ...
	'MarkerSize',20, ...
	'EraseMode','none');
    pos=[xp1 yp1 zp1];

elseif nargin==2,
    phi1=city1(1)*pi/180;
    tht1=(city1(2)+135)*pi/180; 
    [xp1,yp1,zp1]=sph2cart(tht1,phi1,1);
    city1Hndl=plot3(xp1,yp1,zp1,'r.', ...
	'MarkerSize',20, ...
	'EraseMode','none');

    phi2=city2(1)*pi/180;
    tht2=(city2(2)+135)*pi/180; 
    [xp2,yp2,zp2]=sph2cart(tht2,phi2,1);
    city2Hndl=plot3(xp2,yp2,zp2,'r.', ...
	'MarkerSize',20, ...
	'EraseMode','none');

    out=cross([xp2 yp2 zp2],[xp1 yp1 zp1]);
    [tht3,phi3,r]=cart2sph(out(1),out(2),out(3));
    h=plot3(xp1,yp1,zp1,'y.', ...
	'MarkerSize',10, ...
	'EraseMode','none');
    
    % Following calculation uses Napier's Spherical 
    % Trigonometry Cosine Rule for Sides
    % Ref: VNR Encyclopedia of Mathematics
    angularDistCos=sin(phi1)*sin(phi2)+cos(phi1)*cos(phi2)*cos(tht1-tht2);
    angularDist=acos(angularDistCos)*180/pi;
    % Earth radius in miles: 3963
    earthRadius=3963;
    distance=(angularDist*pi/180)*earthRadius;
    format bank;
    title(['Distance = ',num2str(distance),' miles']);
    format;
    drawnow;
    for count=1:2:angularDist,
	rotate(h,[tht3*180/pi phi3*180/pi],2,[0 0 0]);
	drawnow;
    end;

    pos=[xp1 yp1 zp1];
    delete([city1Hndl city2Hndl h]);

end;
