% parallel coordinate 2D circle
% with random theta
clf
clear all

npoints=100;
nframe=120;

% fname='c:\my documents\huh.avi';
% mov=avifile(fname);
% mov.compression='cinepak';
% mov.quality=100;

toggle=1;
while(toggle)
%     for i = 1:npoints/2; 
%         pos0(1,i)= ((rand-1/2)>0)-0.5;  %x1
%         pos0(2,i)= rand-1/2;  %x2 
%         s(:,i) = [pos0(1,i)==0.5 pos0(2,i)==0.5  pos0(1,i)==0.5]';
%     end
%     for i = npoints/2+1:npoints; 
%         pos0(1,i)= rand-1/2;  %x1
%         pos0(2,i)= ((rand-1/2)>0)-0.5;  %x2 
%         s(:,i) = [1 pos0(2,i)==0.5  pos0(1,i)==0.5]';
%     end

    for i = 1:npoints
        pos0(1,i)= rand-0.5;  %x1
        pos0(2,i)= 0;  %x2 
        s(:,i) = [1 pos0(2,i)+.5 pos0(1,i)+.5]';
    end

    toggle=0
end

while(0)
    for i = 1:npoints; 
        pos0(1,i)= rand-0.5;  %x1
        pos0(2,i)= rand-0.5;  %x2 
        s(:,i) = [1 pos0(2,i)+0.5 pos0(1,i)+0.5]';
    end
    toggle=0
end


framenum=1;

for angle=linspace(0,180,nframe)
    angle=angle/57.3;
    
    pos(1,:) = cos(angle).*pos0(1,:) - sin(angle).*pos0(2,:);
    pos(2,:) = sin(angle).*pos0(1,:) + cos(angle).*pos0(2,:);
    
    clf
    %==========================
    subplot(3,1,1)
    
    hold on
    for i = 1:npoints
        plot(pos(:,i), 'color', s(:,i), 'linewidth',2 );    
    end
    
    axisx = [1,3; 1,1; 2,2; 3,3]';
    axisy = [0,0; 0,1; 0,1; 0,1]';
    line(axisx, axisy, 'color', 'k');
    set(gca,'ylim',[-1 1])
    set(gca,'xlim',[1 2])
    
    %==========================
    
    subplot(3,1,2)
    axis([-1 1 -1 1])
    set(gca,'color','white')
    set(gca,'pos',[.05 .4 .9 .25]);
    hold on
    for i = 1:npoints
        plot(pos(1,i),pos(2,i),...
            'color', s(:,i),...
            'markersize',1.5,'linewidth',3,'marker','o')
    end
    
    %======================
    %density histogram
    
    subplot(3,1,3)
    
    xstep=20; ystep=20;
    xmin=0; xmax=1; ymin=-1; ymax=1;
    
    density=zeros(xstep+1,ystep);
    
    for i = 1:npoints
        for x = xmin: 1/xstep: xmax
            
            y =pos(1,i)+...
                x*(pos(2,i)-pos(1,i));
            
            xindex= fix(x*xstep)+1;
            yindex= fix((y-ymin)/(ymax-ymin)*ystep)+1;
            
            density(xindex,yindex)= density(xindex,yindex)+1;
            
        end    
    end
    
    mask=[1/4 1/2 1/4; ...
            1/2 1   1/2; ...
            1/4 1/2 1/4;];
    
    density=filter2(mask,density);
    contourf(density',10);
    set(gca,'pos',[0 0 .9 .33]);
  
    drawnow
%     F=getframe(gcf);
%     mov=addframe(mov,F);
    %framenum=framenum+1;
end

%mov = close(mov);