% file: comparison.m
%
% this file finds and plots the set boundaries for the untransformed and 
% transformed data.  It must be run after homework_02.m

% plot the data sets
%
fig4=figure;
hold on;
box on;
grid on;
plot(set1(:,1),set1(:,2),'*');
plot(set2(:,1),set2(:,2),'r*');
set(gca,'Xtick',[-5:1:5],'Ytick',[-6:1:6]);
title('Set Boundary Comparison');

x_values=[x1(1) x2(1) x3(1) x4(1)];
y_values=[x1(2) x2(2) x3(2) x4(2)];

% plot the test set
%
%plot(x1(1),x1(2),'black*');
%plot(x2(1),x2(2),'black*');
%plot(x3(1),x3(2),'black*');
%plot(x4(1),x4(2),'black*');
plot(x_values,y_values,'k*');


% initialize the counters
%
i=1;
j=1;

%plot the boundary line of set1 and set2
%
for x = -5.0:0.1:5.0

   diff=0;
   diff_trans=0;

   for y = -5.0:0.1:5.0

      % compute the distances for the untransformed data
      %
      d1= dist(orig_mean1, [x;y]);
      d2= dist(orig_mean2, [x;y]);
      new_diff = d2-d1;

      % compute the distances for the transformed data
      %
      d1= dist(trans_mean1, transform1*[x;y]);
      d2= dist(trans_mean2, transform2*[x;y]);
      new_diff_trans = d2-d1;
		
      % determine boundary cases and use these data points to plot line
      % the boundary cases are determined by when the data changes from
      % set 1 classification to set 2 or vice versa
      %
      if (new_diff > 0) & (diff<0)
         orig_line(i,:) = [x,y];
         i = i + 1;
      elseif (new_diff < 0) & (diff > 0)
         orig_line(i,:) = [x,y];
         i = i + 1;
      elseif isequal(new_diff,0)
         orig_line(i,:) = [x,y];
        i= i + 1;
      end
      diff = new_diff;

      % determine boundary cases and use these data points to plot line
      %
      if (new_diff_trans > 0) & (diff_trans<0)
         trans_line(j,:) = [x,y];
         j = j + 1;
      elseif (new_diff_trans < 0) & (diff_trans > 0)
         trans_line(j,:) = [x,y];
         j = j + 1;
      elseif isequal(new_diff_trans,0)
         trans_line(j,:) = [x,y];
         j= j + 1;
      end
      diff_trans = new_diff_trans;

   end


end

% plot the boundary line for untransformed data
%
hold on;
plot(orig_line(:,1), orig_line(:,2), 'k--');

% plot the boundary line for transformed data
%
hold on;
plot(trans_line(:,1), trans_line(:,2), 'g--');

% add a legend
%
leg1=legend('Set 1','Set 2', 'Test Set','before transform', 'after transform',0);
