function xphide(action)
%XPHIDE   MATLAB's version of Human Eye Sensitivity towards moving objects.
%       Demonstrates Handle Graphics.
 
%   Z. You, 2-13-96
%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.13 $

% Information regarding the play status will be held in
% the axis user data according to the following table:
play= 1;
stop=-1;

if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    oldFigNumber=watchon;

    figNumber=figure( ...
        'Name','Hide and Seek', ...
        'NumberTitle','off', ...
        'Visible','off', ...
        'BackingStore','off');
    axes( ...
        'Units','normalized', ...
        'Position',[0.05 0.05 0.65 0.90], ...
        'Visible','off', ...
        'NextPlot','add');

    figure(figNumber);
    set(gcf,'Visible','On', 'Color', 'black');
    set(gca,'Visible','On');
    n = 80;
    a = rand(n);
    b = rand(n);
    ap = [0.1 0.1 0.1]; dm = [1 1 1];
    xmax = 0.65; xmin = 0.35; ymax = 0.6; ymin = 0.4;
    thick = 0.005; shift = 0.1;

    %determine points inside the rectangular

    %rectangular
    aa = (((a > (xmin - thick)) & (a < (xmin + thick))) ...
        | ((a > (xmax - thick)) & (a < (xmax + thick))));
    bb = ((b > ymin) & (b < ymax));
    cc = (aa & bb);
    aa = ((a > xmin) & (a < xmax));
    bb = (((b > (ymin - thick)) & (b < (ymin + thick))) ...
        | ((b > (ymax - thick)) & (b < (ymax + thick))));
    dd = (aa & bb);
    cc = (cc | dd);
    ra = a(cc) + shift;
    rb = b(cc);
  
    %circle
    aa = (a-0.5).*(a-0.5)+(b-0.5).*(b-0.5);
    cc = ((aa > 0.019) & (aa < 0.021));
    ca = a(cc) + shift;
    cb = b(cc);
  
    %triangular
    cs = 1/2; ss = sqrt(3.0)/2;
    aa = ((a > xmin) & (a < xmax));
    bb = ((b > (ymin - thick)) & (b < (ymin + thick)));
    cc = (aa & bb);

    az = ((a-xmin)*cs+(b-ymin)*ss);
    bz = ((b-ymin)*cs-(a-xmin)*ss);
    aa = ((az > 0) & (az < (xmax-xmin)));
    bb = ((bz > - thick) & (bz < thick));
    dd = (aa & bb);
    cc = (cc | dd);

    az = ((a-xmax)*cs-(b-ymin)*ss);
    bz = ((b-ymin)*cs+(a-xmax)*ss);
    aa = ((az < 0) & (az > xmin-xmax));
    bb = ((bz > -thick) & (bz < thick));
    dd = (aa & bb);
    cc = (cc | dd);

    ta = a(cc)+shift;
    tb = b(cc);
  
%    sg = plot(a,b,'g.','EraseMode','none');
    ma = {ra, ta, ca};
    mb = {rb, tb, cb};
    m = 1;

    for i = 1:3
      mg(i) = plot(ma{i},mb{i},'g.','EraseMode','xor');
      if i ~= 1
        set(mg(i),'Visible','Off');
      else 
        set(mg(i),'Visible','On');
      end
    end

    axis([0 1 0 1.1]);
    axis off;
    text(0, 0.98, 'Can you see the hidden object?', ...
    'Color','y','FontSize',19, 'EraseMode','none', 'Unit', 'Normal');
    drawnow

    %===================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    yInitPos=0.90;
    xPos=0.75;
    btnWid=0.2;
    btnHt=0.10;
    % Spacing between the button and the next command's label
    spacing=0.05;
    
    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[xPos-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    h=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.5 0.5 0.5]);

    %====================================
    % The START button
    btnNumber=1;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);
    labelStr='Start';
    cmdStr='start';
    callbackStr='xphide(''start'');';

    % Generic button information
    btnPos=[xPos yPos-spacing btnWid btnHt];
    startHndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Interruptible','on', ...
        'Callback',callbackStr);

    %====================================
    % The Shape popup button
    btnNumber=2;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);
    textStr='Shape';
    popupStr={'Rectangle','Triangle','Circle'};
    
    % Generic button information
    btnPos1=[xPos yPos-spacing+btnHt/2 btnWid btnHt/2];
    btnPos2=[xPos yPos-spacing btnWid btnHt/2];
    ShapeTitle=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',btnPos1, ...
        'String',textStr);
    btnPos=[xPos yPos-spacing btnWid btnHt/2];
    ShapeHndl=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos2, ...
        'String',popupStr);

    %====================================
    % The STOP button
    btnNumber=3;
    yPos=0.90-(btnNumber-1)*(btnHt+spacing);
    labelStr='Stop';
    % Setting userdata to -1 (=stop) will stop the demo.
    callbackStr='set(gca,''Userdata'',-1)';
    
    % Generic button information
    btnPos=[xPos yPos-spacing btnWid btnHt];
    stopHndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'Enable','off', ...
        'String',labelStr, ...
        'UserData', {ma,ap,dm,mg,m}, ...
        'Callback',callbackStr);

    %====================================
    % The INFO button
    labelStr='Info';
    callbackStr='xphide(''info'')';
    infoHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[xPos 0.20 btnWid 0.10], ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % The CLOSE button
    labelStr='Close';
    callbackStr='close(gcf)';
    closeHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[xPos 0.05 btnWid 0.10], ...
        'String',labelStr, ...
        'Callback',callbackStr);

    % Uncover the figure
    hndlList=[startHndl ShapeHndl stopHndl infoHndl closeHndl];
    set(figNumber, 'Visible','on','UserData',hndlList);

    sg = plot(a,b,'g.','EraseMode','none');
    watchoff(oldFigNumber);
    figure(figNumber);
    
elseif strcmp(action,'start'),
    axHndl=gca;
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');
    startHndl=hndlList(1);
    ShapeHndl=hndlList(2);
    stopHndl=hndlList(3);
    infoHndl=hndlList(4);
    closeHndl=hndlList(5);
    set([startHndl ShapeHndl closeHndl infoHndl],'Enable','off');
    set(stopHndl,'Enable','on');
    
    % ====== Start of Demo
    set(axHndl,'Userdata',play);
%    set(gca,'Drawmode','Fast');
    so = findobj(gcf,'String','Stop');

    sa = get(so,'UserData');

    ma = sa{1};
    ap = sa{2};
    dm = sa{3};
    mg = sa{4};
    m = sa{5};    

    if m~=get(ShapeHndl,'Value'),
      set(mg(m),'Visible','Off');
      m=get(ShapeHndl,'Value');
      set(mg(m),'Visible','On');
      zp = ap(m);
      zd = dm(m);
    end;
  
    zp = ap(m);
    zd = dm(m);
    
    while get(axHndl,'Userdata')==play,
      if (((zp > 0.3) & (zd == 1)) | ((zp < -0.3) & (zd == -1)))
        zd = -zd;
      end
      nd = zd*0.03;
      zp = zp+nd;
      ma{m} = ma{m}+nd;
      set(mg(m),'Xdata',ma{m});
      drawnow;
    end;    % while get(axHndl,...

    % ====== End of Demo
    ap(m) = zp; dm(m) = zd;
    set(so,'UserData',{ma,ap,dm,mg,m});
    set([startHndl ShapeHndl closeHndl infoHndl],'Enable','on');
    set(stopHndl,'Enable','off');

elseif strcmp(action,'info');
    ttlStr='xphide';
    hlpStr= ...                                              
        {'   '  
         ' This demo shows the eye''s sensitivity '
         ' at detecting objects in motion. We can often'
         ' see moving objects that would be invisible if  '
         ' they were to stand still. This is a crucial '
         ' adaptation for all animals, both predator and'
         ' prey.'
         '  '  
         ' Press the "Start" button to see the      '
         ' effect. You have a choice of three moving      '
         ' objects: a square, a triangle, or a circle. You can  '
         ' switch between these shapes by clicking on the '
         ' "Shape" popup menu.                                '
         '                                                '  
         ' File name: xphide.m                            '};
    helpwin(hlpStr,ttlStr);                                

end;    % if strcmp(action, ...
