%(x0,y0) ---initial position of the pacman
%(x1,y1) ---target position of the pacman
%count  ---the number of the drawings
%color_dim---color of the pacman
%

function drawpacman(x0,y0,x1,y1,count,color_dim)
global bigMatrixImg;
image_rows=40;   %the number of the rows of pixels in one frame; 
image_cols=40;   %the number of the colomns of pixels in one frame;

if((y1==y0)&(x1>=x0))angle=0;
else if((y1==y0)&(x1<x0))angle=pi;
    else if((y1>y0)&(x1==x0))angle=pi/2;
        else if((y1<=y0)&(x1==x0))angle=3*pi/2;
                else angle=atan2((y1-y0),(x1-x0));
        end;    
end;
end;
end;

body_row_center=image_rows./2+y0.*image_rows+image_rows./8.*count*(y1-y0);  %the y coordinates of the center of the pacman body.
body_col_center=image_cols./2+x0.*image_cols+image_cols./8.*count*(x1-x0);  %the x coordinates of the center of the pacman body.

if(sin(pi/2+angle)>sin(3*pi/2+angle))
eye_row_center=body_row_center-image_cols./4*sin(pi/2+angle); %the y coordinates of the center of the pacman eye.
eye_col_center=body_col_center-image_cols./4*cos(pi/2+angle); %the x coordinates of the center of the pacman eye.
else
eye_row_center=body_row_center-image_cols./4*sin(3*pi/2+angle); %the y coordinates of the center of the pacman eye.
eye_col_center=body_col_center-image_cols./4*cos(3*pi/2+angle); %the x coordinates of the center of the pacman eye. 
end;

    for i=ceil(body_row_center-image_rows/2+4):1:ceil(body_row_center+image_rows/2-4)
        for j=ceil(body_col_center-image_cols/2+4):1:ceil(body_col_center+image_cols/2-4)
            if(i>0 & j>0 & i<240 & j<320)
            if((j-body_col_center).^2+(i-body_row_center).^2<(image_rows.*0.40).^2 )%make sure that the coordinates are in a big circle(pacman body).
                if((j-eye_col_center).^2+(i-eye_row_center).^2>(image_rows.*0.1).^2)%make sure that the coordinates are out of the small circle(pacman eye)
                    newy=(i-body_row_center)*cos(angle)-(j-body_col_center)*sin(angle);
                    newx=(j-body_col_center)*cos(angle)+(i-body_row_center)*sin(angle);
                    newangle=(pi./4).*(4-abs(4-count))./4;
                    if(newy*cos(newangle)<=-newx*sin(newangle)|newy*cos(newangle)>=newx*sin(newangle) )
                         bigMatrixImg(i,j,1) = 0;
                         bigMatrixImg(i,j,2) = 0;
                         bigMatrixImg(i,j,3) = 0;
                         bigMatrixImg(i,j,color_dim) = 1;
                    end
                end
            end 
        end
        end
    end
