function gcdemo

% GCDEMO  Demo for GCLINE
%

  load('topo.mat','topo','topolegend'); sz = size(topo);
 
  lon = ( 1 : sz(2) ) - 0.5; lat = ( 1 : sz(1) ) - 0.5 - sz(1);
 
  lon =  lon * topolegend(1) + topolegend(3);
  lat =  lat * topolegend(1) + topolegend(2);
 
  ii   = find( lon > 360-30-1 );  % [ 30W ..  00W ] prepend West
  jj   = find( lon < 200+1 );     % [ 00E .. 200E ]  append East
 
  lon  = cat( 2 , lon(ii)-360 , lon  , lon(jj)+360 );
  topo = cat( 2 , topo(:,ii)  , topo , topo(:,jj) );
 
  xlm = fix(lon([1 end]));
 
  figure('position',[50 50 960 320],...
         'name' , upper(mfilename), ...
         'menubar','none','toolbar','none');
  hold on, grid on, box on, axis equal
  axis([ xlm -90 90 ]);
  set(gca,'position',[0.06 0.08 0.92 0.85])
  [cs,h] = contour(lon,lat,topo,[0 0],'k-');
 
  line('xdata',NaN,'ydata',NaN,'color','r',...
       'erasemode','xor','tag','GCLINE');
 
  set(get(gca,'title'),'erasemode','xor');

  CBmove = [ 'p = get(gca,''currentpoint''); p = p(1,[1 2]);' ...
             'xlm = get(gca,''xlim''); ' ...
             'h = findobj(gca,''tag'',''GCLINE''); n = 100;' ...
             '[x,y,w] = gcline(get(h,''userdata''),p,n);' ...
             'w = 100*round(w*(n-1)*pi/180*6371/100);' ...
             'x = x + 360 * ( x < xlm(1) );' ...
             'if abs(p(1)-x(n))>180;' ...
             ' x = x + 360 * sign(p(1)-x(n));' ...
             'end;' , ...
             'while any(diff(x)<-180);' ...
             ' ii = find(diff(x)<-180)+1;' ... 
             ' x(ii:n) = x(ii:n) + 360;' ...
             'end;' , ...
             'while any(diff(x)>180);' ...
             ' ii = find(diff(x)>180);' ... 
             ' x(1:ii) = x(1:ii) + 360;' ...
             ' end;' , ...
             'x = x - 360 * ( x > xlm(2) );' ...
             'set(h,''xdata'',x,''ydata'',y);' ...
             'set(get(gca,''title''),''string'',sprintf(''%.0f km'',w))' ];

  CBdown = [ 'p = get(gca,''currentpoint''); p = p(1,[1 2]);' ...
             'set(findobj(gca,''tag'',''GCLINE''),''userdata'',p);' ...
             'set(gcf,''windowbuttonmotionfcn'',get(gca,''userdata''));' ];
 
 
  set([gca;get(gca,'children')],'buttondownfcn',CBdown,'userdata',CBmove);
  set(gcf,'windowbuttonupfcn','set(gcf,''windowbuttonmotionfcn'','''');');
 
  try, geoaxis(gca); end
