%  Create an instance of figure 14.2 with N points, angle THETA, width W,
% and length L

function BoundingBoxFig(N,THETA,W,L)
  DATA = rand(2,N);
  DX = L*cos(THETA)*DATA(1,:) - W*sin(THETA)*DATA(2,:);
  DY = L*sin(THETA)*DATA(1,:) + W*cos(THETA)*DATA(2,:);
  plot(DX,DY,'*r') 
  XMIN = min(DX);
  XMAX = max(DX);
  YMIN = min(DY);
  YMAX = max(DY);
  plot([XMIN,XMAX,XMAX,XMIN,XMIN],[YMIN,YMIN,YMAX,YMAX,YMIN],'--b')

  Q(1,:) = DX;
  Q(2,:) = DY;
  MU = mean(Q,2);
  for I=1:N
      Q(:,I) = Q(:,I) - MU;
  end
  [L,S,R] = svd(Q);
  P = L'*Q;
  BOX(:,1) = MU + min(P(1,:))*L(:,1) + min(P(2,:))*L(:,2);
  BOX(:,2) = MU + max(P(1,:))*L(:,1) + min(P(2,:))*L(:,2);
  BOX(:,3) = MU + max(P(1,:))*L(:,1) + max(P(2,:))*L(:,2);
  BOX(:,4) = MU + min(P(1,:))*L(:,1) + max(P(2,:))*L(:,2);
  BOX(:,5) = BOX(:,1);
  plot(BOX(1,:), BOX(2,:), 'k')
end
