function [result,newLocation] = animation(x,y,z,minx,maxx,miny,maxy,...
    minz,maxz,pointer, surf,location, plane,centroid,radii )
% Draw a new frame of the object to show a moving spherical pointer

result=0;
    
sphere=translate(pointer,-x,-y,-z);   % move sphere from origin by -x,-y,-z
cla
newLocation=[location(1)-x,location(2)-y,location(3)-z];
renderpatch(surf);
renderpatch(sphere);
  
grid on
 
l=light('position',[4,-4,0]);
   
drawnow  
   
rotate3d on

surf.EdgeLighting = 'phong';
surf.DiffuseStrength = 0;

% check if crossing a boundary (check ALL created objects)

% Check cubic objects for boundary crossing
index=size(minx);
for i=1:index(2)
    if((newLocation(1)>=minx(i))&&(newLocation(1)<=maxx(i))&&...
            (newLocation(2)>=miny(i))&&(newLocation(2)<=maxy(i))&&...
            (newLocation(3)<=maxz(i))&&(newLocation(3)>=minz(i)))
        result=1;
        break; 
    end
end

% Check spherical objects for boundary crossing
if (result == 0 && ~isempty(centroid))
    index = length(radii);
    for i = 1:index
        distance = norm(centroid(i+1,:) - newLocation-[0 0 .05]) % error margin
        radii(index)
        i
        if distance <= radii(index)
            result = 1;
            break;
        end
    end
end