%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function 'Pacman' creates simple .avi file which contains motion of a small pacman, 
% This program is revised from Nilesh Ghubade's makemovie program (nileshg@temple.edu)
% Programed by xiangdong wen
% Date : Oct, 2002
% Advisor : Dr.Longin Jan Latecki (latecki@temple.edu )
% Project : a small movie
% 
% Input arguments: No need for input
% Output : 'pacman.avi'
% Example : Pacman()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%function Pacman()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%The parameter needed for the drawing.
clear
image_rows = 100;   %the number of the rows of pixels in one frame; 
image_cols = 100;   %the number of the colomns of pixels in one frame;
face_radius = image_rows*0.5;
body_row_center = image_rows/2;  %the y coordinates of the center of the face.
body_col_center = image_cols/2;  %the x coordinates of the center of the face.
left_eye_row_center = image_rows/4;     %the y coordinates of the center of the left eye.
left_eye_col_center = image_cols/4;     %the x coordinates of the center of the left eye.
right_eye_row_center = image_rows/4;    %the y coordinates of the center of the right eye.
right_eye_col_center = image_cols*.75;  %the x coordinates of the center of the right eye.
eye_radius = image_rows*0.1;            %radius of the eyes.
eyeball_radius = eye_radius/2;          %radius of the eyeballs.
left_eyeball_row_center = left_eye_row_center + eyeball_radius;     %the y coordiantes of the center of the left eyeball.
left_eyeball_col_center = left_eye_col_center;                      %the x coordiantes of the center of the left eyeball.
right_eyeball_row_center = right_eye_row_center + eyeball_radius;   %the y coordiantes of the center of the right eyeball.
right_eyeball_col_center = right_eye_col_center;                    %the x coordiantes of the center of the right eyeball.
mouth_row_center = image_rows*.75;       %the y coordinates of the center of the mouth.
mouth_col_center = image_cols/2;         %the x coordinates of the center of the mouth.
nose_radius = 3;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
framesPerSec = 4; % 'frames per second' parameter for the .avi file.
aviobj = avifile('face.avi','fps',framesPerSec); % initialize .avi file object.
framenumber = 64;
% for loop for creating each frame of the .avi file.
bigMatrixImg = zeros(image_rows,image_cols,3);

a = -pi/2;
b = -pi/2;
step = pi/16;

for count = 1 : framenumber
    for i = 1:image_rows
        for j = 1:image_cols
            if ((j - body_col_center)^2 + (i - body_row_center)^2 < (face_radius)^2)%make sure that the coordinates are in a big circle(the face).
                %this forms the yellow face
                bigMatrixImg(i,j,1) = 255;
                bigMatrixImg(i,j,2) = 255; 
                bigMatrixImg(i,j,3) = 0;
               %if (((eye_radius)^2 < ((j - left_eye_col_center).^2 + (i-left_eye_row_center).^2) < ((eye_radius + 1)^2)) | ((j - right_eye_col_center).^2 + (i - right_eye_row_center).^2 == (eye_radius)^2)) 
                if (((j - left_eye_col_center).^2 + (i - left_eye_row_center).^2 == (eye_radius)^2) | ((j - right_eye_col_center).^2 + (i - right_eye_row_center).^2 == (eye_radius)^2)) 
                    %this forms the black eye boundery      
                     bigMatrixImg(i,j,1) = 0;
                     bigMatrixImg(i,j,2) = 0;
                     bigMatrixImg(i,j,3) = 0;
                end    
                %this forms two black eye balls
                if ((j - left_eyeball_col_center).^2 + (i-left_eyeball_row_center).^2 < (eyeball_radius)^2) | ((j - right_eyeball_col_center).^2 + (i - right_eyeball_row_center).^2 < (eyeball_radius)^2)
                    bigMatrixImg(i,j,1) = 0;
                    bigMatrixImg(i,j,2) = 0;
                    bigMatrixImg(i,j,3) = 0;    
                end
                %paint the nose
                if ((j - body_col_center).^2 + (i-body_row_center).^2 < (nose_radius)^2) 
                    bigMatrixImg(i,j,1) = 0;
                    bigMatrixImg(i,j,2) = 0;
                    bigMatrixImg(i,j,3) = 0;    
                end
                %if count > 32
                % paint a smiling mouth
                mouth_col = 25:75;
                mouth_row1 = 50:75;
                mouth_row2 = 74:-1:50;
                mouth_row = [mouth_row1,mouth_row2];
                mouth_size = size(mouth_col);
                for k = 1 : mouth_size;
                    bigMatrixImg(mouth_row(1,k),mouth_col(1,k),1) = 0;
                    bigMatrixImg(mouth_row(1,k),mouth_col(1,k),2) = 0;
                    bigMatrixImg(mouth_row(1,k),mouth_col(1,k),3) = 0;    
                end

            end
        end
    end
    %reposition center of eyeball 
    if count <= 32
        a = a + step;
        left_eyeball_row_center = left_eye_row_center + eyeball_radius*sin(a);  %updated y coordinate of the center of left eyeball.
        left_eyeball_col_center = left_eye_col_center + eyeball_radius*cos(a);  %updated x coordinate of the center of left eyeball.
        right_eyeball_row_center = right_eye_row_center + eyeball_radius*sin(-a);%updated y coordinate of the center of right eyeball.
        right_eyeball_col_center = right_eye_col_center + eyeball_radius*cos(-a);%updated x coordinate of the center of right eyeball.
        % paint a confused mouth
        %
        %
    elseif count >32
        b = b + step;
        left_eyeball_row_center = left_eye_row_center + eyeball_radius*sin(b);  %updated y coordinate of the center of left eyeball.
        left_eyeball_col_center = left_eye_col_center + eyeball_radius*cos(b);  %updated x coordinate of the center of left eyeball.
        right_eyeball_row_center = right_eye_row_center + eyeball_radius*sin(b);%updated y coordinate of the center of right eyeball.
        right_eyeball_col_center = right_eye_col_center + eyeball_radius*cos(b);%updated x coordinate of the center of right eyeball.
%         % paint a smiling mouth
%         mouth_col = 25:75;
%         mouth_row1 = 50:75;
%         mouth_row2 = 74:-1:50;
%         mouth_row = [mouth_row1,mouth_row2];
%         mouth_size = size(mouth_col);
%         for k = 1 : mouth_size;
%             bigMatrixImg(mouth_row(1,k),mouth_col(1,k),1) = 0;
%             bigMatrixImg(mouth_row(1,k),mouth_col(1,k),2) = 0;
%             bigMatrixImg(mouth_row(1,k),mouth_col(1,k),3) = 0;    
%         end

    end

    fileName = 'temp.bmp';
    imwrite(bigMatrixImg,fileName,'bmp');
    imgObj = imread(fileName,'bmp');
    frameObj = im2frame(imgObj);  
    aviobj = addframe(aviobj,frameObj);
    delete(fileName);
end
aviobj = close(aviobj);