% Generate a random stickman and output in three versions:
% (1) with colorful background
% (2) with white backgroud
% (3) save to the disk as an image

function GenStickman(imagename)
figure(1);

axisFactor = myrand(4,10);
background = gallery('chebvand',ceil(11*axisFactor)) ;
imagesc(background);
shift = 0.5*axisFactor;
hold on;

%head
a=myrand(0.5*axisFactor,0.9*axisFactor);
b=myrand(1.2*axisFactor,1.7*axisFactor);
sita=0:pi/20:2*pi;
x1=myrand(4*axisFactor,6*axisFactor)+ shift;
y1=myrand(7*axisFactor,9*axisFactor)+ shift;
plot(x1+a*cos(sita),y1+b*sin(sita),'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]); 
hold on

%point2  body bottom
x2 = myrand(4*axisFactor,6*axisFactor)+ shift;
y2 = myrand(4*axisFactor,y1-5*axisFactor)+ shift;
plot([x2 x1], [y2 y1-b],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);

%point3 intersect
ratio = myrand(1/2,2/3);
if x2<5
    x3 = x2+(x1-x2)*ratio;
elseif x2 > 5
    x3 = x2-(x2-x1)*ratio;
end
y3 = y2+(y1-b-y2)*ratio;

%point4
x4 = myrand(7*axisFactor,8*axisFactor)+ shift;
y4 = myrand(4*axisFactor,8*axisFactor)+ shift;
midX34 = (x3+x4)/2;
midY34 = (y3+y4)/2;
plot([x3 midX34], [y3 midY34],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX34 x4], [midY34 y4],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);

%point5
x5 = myrand(x4+1*axisFactor,x4+2*axisFactor)+ shift;
y5 = myrand(y4-1.5*axisFactor,y4+1.5*axisFactor)+ shift;
midX45 = (x4+x5)/2;
midY45 = (y4+y5)/2;
plot([x4 midX45], [y4 midY45],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX45 x5], [midY45 y5],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);

%point6
x6 = myrand(2*axisFactor,3*axisFactor)+ shift;
y6 = myrand(4*axisFactor,8*axisFactor)+ shift;
midX36 = (x3+x6)/2;
midY36 = (y3+y6)/2;
plot([x3 midX36], [y3 midY36],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX36 x6], [midY36 y6],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point7
x7 = myrand(x6-2*axisFactor,x6-1*axisFactor)+ shift;
y7 = myrand(y6-1.5*axisFactor,y6+1.5*axisFactor)+ shift;
midX67 = (x6+x7)/2;
midY67 = (y6+y7)/2;
plot([x6 midX67], [y6 midY67],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX67 x7], [midY67 y7],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point8
x8 = myrand(2*axisFactor,4.5*axisFactor)+ shift;
y8 = myrand(y2-2*axisFactor,y2-1*axisFactor)+ shift;
midX28 = (x2+x8)/2;
midY28 = (y2+y8)/2;
plot([x2 midX28], [y2 midY28],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX28 x8], [midY28 y8],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point9
x9 = myrand(5.5*axisFactor,8*axisFactor)+ shift;
y9 = myrand(y2-2*axisFactor,y2-1*axisFactor)+ shift;
midX29 = (x2+x9)/2;
midY29 = (y2+y9)/2;
plot([x2 midX29], [y2 midY29],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX29 x9], [midY29 y9],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point10
x10 = myrand(x8-0.5*axisFactor,x8)+ shift;
y10 = myrand(0,y8-0.5*axisFactor)+ shift;
midX108 = (x10+x8)/2;
midY108 = (y10+y8)/2;
plot([x10 midX108], [y10 midY108],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX108 x8], [midY108 y8],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point11
x11 = myrand(x9,8*axisFactor)+ shift;
y11 = myrand(0,y9-0.5*axisFactor)+ shift;
midX119 = (x11+x9)/2;
midY119 = (y11+y9)/2;
plot([x11 midX119], [y11 midY119],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX119 x9], [midY119 y9],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point12
x12 = myrand(x10-1.5*axisFactor,x10-0.5*axisFactor)+ shift;
y12 = myrand(0.5*y10,y10)+ shift;
midX1012 = (x10+x12)/2;
midY1012 = (y10+y12)/2;
plot([x10 midX1012], [y10 midY1012],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX1012 x12], [midY1012 y12],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);


%point13
x13 = myrand(x11+0.5*axisFactor,x11+1.5*axisFactor)+ shift;
y13 = myrand(0.5*y11,y11)+ shift;
midX1113 = (x11+x13)/2;
midY1113 = (y11+y13)/2;
plot([x11 midX1113], [y11 midY1113],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);
plot([midX1113 x13], [midY1113 y13],'LineWidth',myrand(2,4), 'Color',[rand(1), rand(1), rand(1)]);

% random lines
for k=1:8
    RX1 = myrand(0,11*axisFactor)+ shift;
    RY1 = myrand(0,11*axisFactor)+ shift;
    RX2 = myrand(0,11*axisFactor)+ shift;
    RYy = myrand(0,11*axisFactor)+ shift;
    plot([RX1 RX2], [RY1 RYy],'LineWidth',0.5, 'Color',[rand(1), rand(1), rand(1)]);
end

axis([0 11*axisFactor 0 11*axisFactor]);
axis xy
axis off;




figure(2);
set(gcf,'Color',[1,1,1]); %set background to be white
plot(x1+a*cos(sita),y1+b*sin(sita),'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x2 x1], [y2 y1-b],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x3 midX34], [y3 midY34],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX34 x4], [midY34 y4],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x4 midX45], [y4 midY45],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX45 x5], [midY45 y5],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x11 midX1113], [y11 midY1113],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX1113 x13], [midY1113 y13],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x3 midX36], [y3 midY36],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX36 x6], [midY36 y6],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x6 midX67], [y6 midY67],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX67 x7], [midY67 y7],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x2 midX28], [y2 midY28],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX28 x8], [midY28 y8],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x2 midX29], [y2 midY29],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX29 x9], [midY29 y9],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x10 midX108], [y10 midY108],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX108 x8], [midY108 y8],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x11 midX119], [y11 midY119],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX119 x9], [midY119 y9],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([x10 midX1012], [y10 midY1012],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;
plot([midX1012 x12], [midY1012 y12],'LineWidth',myrand(2,4), 'Color',[0,0,0]);
hold on;

axis([0 11*axisFactor 0 11*axisFactor]);
axis xy
axis off;


saveas(gcf,imagename);
end



function z = myrand( x,y )

z = x+(y-x)*rand(1);
end