function [figure_handle] = draw_beast( circle , riaa, rila, liaa, lila, roaa, rola,loaa,lola)
%  draw_beast  Renders an MQL beast as explained in homework 3
%
%  Input:
%     circle  = Set to 1 if the body is a circle, 0 for a hexagon
%     riaa    = The inner angle for the right arm. This routine 
%               expects a value between 1 and 6 corresponding to the
%               bin number. The riaa was discretized in the following
%               way: [0 - 15;15 - 30;30 - 45;45 - 60;60 - 75;75 - 90].
%               The angle used for rendering is chosen as the mid 
%               value of the bin (e.g. 7.5 for the first bin).
%     rila    = The inner angle for the right leg. This routine 
%               expects a value between 1 and 6 corresponding to the
%               bin number. The riaa angle was discretized in the 
%               following way: 
%               [270 - 285;285 - 300;300 - 315;315 - 330;
%                330 - 345;345 - 360].
%     liaa    = The inner angle for the left arm. This routine expects
%               a value between 1 and 6 corresponding to the bin 
%               number. The liaa was discretized in the following way:
%               [90 - 105;105 - 120;120 - 135;135 - 150;150 - 165;
%                165 - 180].
%     lila    = The inner angle for the left leg. This routine expects
%               a value between 1 and 6 corresponding to the bin 
%               number. The lila was discretized in the following way:
%               [180 - 195;195 - 210;210 - 225;225 - 240;240 - 255;
%                255 - 270].
%     roaa    = The outer angle for the right ar. This routine expects
%               a value between 1 and 6 corresponding to the bin 
%               number. The roaa angle was discretized in the 
%               following way:[-60...-40;-40...-20;-20...0;0...20;
%                20...40;40...60]. Notice that if this angle is equal
%               to zero, the outer right arm will be aligned with the
%               inner right arm. A negative angle means a clockwise
%               rotation, and a positive one means counter clockwise.
%     rola    = Defined as roaa. This is the right leg outer angle.
%     loaa    = Defines as roaa. This is the left arm outer angle. 
%               Notice that in order to have simmetry, roaa and loaa
%               have to be different. For example, if roaa is in 
%               bin 1, in order to have simmetry, loaa must be in 
%               bin 6.
%     lola    = defines as roaa
%
%   Output:
%      figure_handle, a handle to the figure where the MQL beast was
%      rendered. Use functions close to close the figure, and saveas
%      to save it as an image.
%
%   Example:
%       handle = draw_beast(1,6,6,6,6,3,4,4,3)
%       saveas(handle,'image_name.jpg');
%       close(handle);

%if(riaa < 1 || riaa > 6)
%    display('Error, riaa must be between 1 and 6');
%    return;
%end
%
%if(rila < 1 || rila > 6)
%    display('Error, rila must be between 1 and 6');
%    return;
%end
%
%if(liaa < 1 || liaa > 6)
%    display('Error, liaa must be between 1 and 6');
%    return;
%end
%
%if(lila < 1 || lila > 6)
%    display('Error, lila must be between 1 and 6');
%    return;
%end
%
%if(roaa < 1 || roaa > 6)
%    display('Error, roaa must be between 1 and 6');
%    return;
%end
%
%if(rola < 1 || rola > 6)
%    display('Error, rola must be between 1 and 6');
%    return;
%end
%
%if(loaa < 1 || loaa > 6)
%    display('Error, loaa must be between 1 and 6');
%    return;
%end
%
%if(lola < 1 || lola > 6)
%    display('Error, lola must be between 1 and 6');
%    return;
%end
%
%if(circle < 0 || circle > 1)
%    display('Error, circle must be either 0 or 1');
%    return;
%end

figure_handle = figure(1);
clf


riaa = angle_bins(riaa);
%rila = -angle_bins(rila);
rila = 3/2.0 * pi + angle_bins(rila);

liaa = (pi/2 + angle_bins(liaa));
lila = (pi + angle_bins(lila));

roaa = outer_angle_bins(roaa)+riaa;
loaa = outer_angle_bins(loaa)+liaa;
rola = outer_angle_bins(rola)+rila;
lola = outer_angle_bins(lola)+lila;

limb_radius_x = 1.0;
limb_radius_y = 0.4;
plot(-5,-5);
hold on
axis square
plot(5,5);
plot(-5,5);
plot(5,-5);
if circle == 1
    radius_body = 1;
    myEllipse(0,0,1,0,radius_body,radius_body,[0.5,0.5,0.5]);
else
    t = (1/12:1/6:1)'*2*pi;
    x = (1.2)*sin(t);
    y = (1.2)*cos(t);
    fill(x,y,[0.5,0.5,0.5]);
    axis square
end

contact_riaa_x = 1/sqrt(2);
contact_riaa_y = 1/sqrt(2);
vertex_x = -limb_radius_x;
dx = vertex_x - vertex_x*cos(riaa);
dy = -vertex_x*sin(riaa);
myEllipse(contact_riaa_x+limb_radius_x + dx,contact_riaa_y + dy, 1,riaa,limb_radius_x,limb_radius_y,[1.0,1.0,0.0]);

vertex_x = limb_radius_x;
contact_roaa_x = vertex_x*cos(riaa) + contact_riaa_x + limb_radius_x + dx;
contact_roaa_y = vertex_x*sin(riaa) + contact_riaa_y + dy;
vertex_x = -limb_radius_x;
dx = vertex_x - vertex_x*cos(roaa);
dy = -vertex_x*sin(roaa);
myEllipse(contact_roaa_x+limb_radius_x + dx,contact_roaa_y + dy, 1,roaa,limb_radius_x,limb_radius_y,[1.0,0.0,1.0]);

contact_liaa_x = -1/sqrt(2);
contact_liaa_y = 1/sqrt(2);
vertex_x = limb_radius_x;
dx = vertex_x + vertex_x*cos(liaa);
dy = vertex_x*sin(liaa);
myEllipse(contact_liaa_x - limb_radius_x + dx,contact_liaa_y  + dy,1,liaa,limb_radius_x,limb_radius_y,[1.0,1.0,0.0]);
vertex_x = -limb_radius_x;
contact_loaa_x = -vertex_x*cos(liaa) + contact_liaa_x - limb_radius_x + dx;
contact_loaa_y = -vertex_x*sin(liaa) + contact_liaa_y + dy;
vertex_x = limb_radius_x;
dx = vertex_x +vertex_x*cos(loaa);
dy = vertex_x*sin(loaa);
myEllipse(contact_loaa_x-limb_radius_x + dx,contact_loaa_y + dy, 1,loaa,limb_radius_x,limb_radius_y,[1.0,0.0,1.0]);

contact_rila_x = 1/sqrt(2);
contact_rila_y = -1/sqrt(2);
vertex_x = -limb_radius_x;
vertex_y = 0;
dx = vertex_x - vertex_x*cos(rila);
dy = -vertex_x*sin(rila);
myEllipse(contact_rila_x+limb_radius_x + dx,contact_rila_y + dy, 1,rila,limb_radius_x,limb_radius_y,[1.0,0.0,0.0]);
vertex_x = limb_radius_x;
contact_rola_x = vertex_x*cos(rila) + contact_rila_x + limb_radius_x + dx;
contact_rola_y = vertex_x*sin(rila) + contact_rila_y + dy;
vertex_x = -limb_radius_x;
dx = vertex_x - vertex_x*cos(rola);
dy = -vertex_x*sin(rola);
myEllipse(contact_rola_x+limb_radius_x + dx,contact_rola_y + dy, 1,rola,limb_radius_x,limb_radius_y,[0.0,1.0,0.0]);


contact_lila_x = -1/sqrt(2);
contact_lila_y = -1/sqrt(2);
vertex_x = limb_radius_x;
dx = vertex_x + vertex_x*cos(lila);
dy = vertex_x*sin(lila);
myEllipse(contact_lila_x - limb_radius_x + dx,contact_lila_y  + dy,1,lila,limb_radius_x,limb_radius_y,[1.0,0.0,0.0]);
vertex_x = -limb_radius_x;
contact_lola_x = -vertex_x*cos(lila) + contact_lila_x - limb_radius_x + dx;
contact_lola_y = -vertex_x*sin(lila) + contact_lila_y + dy;
vertex_x = limb_radius_x;
dx = vertex_x +vertex_x*cos(lola);
dy = vertex_x*sin(lola);
myEllipse(contact_lola_x-limb_radius_x + dx,contact_lola_y + dy, 1,lola,limb_radius_x,limb_radius_y,[0.0,1.0,0.0]);

imresize(figure_handle, 0.5);


end


function [figure_handle] = draw_sym_beast( circle , riaa, rila, roaa, rola)
%  draw_beast  Renders a symmetric MQL beast
    liaa = 7 - riaa;
    loaa = 7 - roaa;
    lila = 7 - rila;
    lola = 7 - rola;

    figure_handle = draw_beast( circle , riaa, rila, liaa, lila, roaa, rola,loaa,lola);
end




function myEllipse(x0,y0,dilation,rotation,rx,ry, thecolor) % x0, y0: coordinates of center point % rx, ry: radii in x and y directions
% Part I: Compute ellipse with axes parallel to % x and y axes and center at (0, 0)
delt = 2*pi/30;	
t = 0:delt:2*pi; x=rx*cos(t);	
y = ry*sin(t);
% Translate, dilate and rotate the ellipse
 xxxx= x0 + dilation*(cos(rotation)*x-sin(rotation)*y); 
 yyyy=y0 + dilation*(sin(rotation)*x+cos(rotation)*y); 
 thandle = plot(xxxx,yyyy,'k-');
 thandle = fill(xxxx,yyyy,thecolor);
 
end

function angle = angle_bins(i)
    test = 1;

    BIN_SIZE = 15 * pi/180;
    angle = BIN_SIZE * i - BIN_SIZE/2;


    if test && i >= 1 && i <= 6
        angle_increment = pi/24;
        angle_bins_ = [ angle_increment angle_increment*3 angle_increment*5 angle_increment*7 angle_increment*9 angle_increment*11];

        if abs(angle_bins_(i) - angle) > 1e-7
            angle_bins_(i)
            angle
            error('Angles do not match')
        end
    end
end

function angle= outer_angle_bins(i)
    test = 1;

    BIN_SIZE = 15 * pi/180;
    angle = BIN_SIZE * (i-3) - BIN_SIZE/2;


    if test && i >= 1 && i <= 6
        outer_angle_increment = pi/24;
        outer_angle_bins_ = [ -outer_angle_increment*5 -outer_angle_increment*3 -outer_angle_increment outer_angle_increment outer_angle_increment*3 outer_angle_increment*5];

        if abs(outer_angle_bins_(i) - angle) > 1e-7
            error('Angles do not match')
        end
    end
end
