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

function BoundingBoxFig1(N,THETA,W,L)
  hold on
  axis equal
  DATA =  [0.0946    0.7696    0.7404    0.8241    0.2934    0.5230  0.8318 ...
    0.5570    0.6806    0.4564    0.5386    0.7552    0.2348    0.0514 ...
    0.6020    0.9883    0.4095    0.5409    0.2193    0.0959
    0.3232    0.2341    0.6928    0.8280    0.3094    0.3253    0.8103 ...
    0.2630    0.2337    0.3846    0.9917    0.9805    0.5286    0.7569 ...
    0.8572    0.9295    0.0003    0.2077    0.3258    0.7475]
  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
