Euclidean distance between polyhedra in 2D

% Section 8.2.1, Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/09/05
% (a figure is generated)
%
% Given two polyhedra C = {x | A1*x <= b1} and D = {x | A2*x <= b2}, the
% distance between them is the optimal value of the problem:
%           minimize    || x - y ||_2
%               s.t.    A1*x <= b1
%                       A2*y <= b2
% Note: here x is in R^2

% Input data
randn('seed',0);
n = 2;
m = 2*n;
A1 = randn(m,n);
b1 = randn(m,1);
A2 = randn(m,n);
b2 = randn(m,1);

fprintf(1,'Computing the distance between the 2 polyhedra...');
% Solution via CVX
cvx_begin
    variables x(n) y(n)
    minimize (norm(x - y))
    norm(x,1) <= 2;
    norm(y-[4;3],inf) <=1;
cvx_end

fprintf(1,'Done! \n');

% Displaying results
disp('------------------------------------------------------------------');
disp('The distance between the 2 polyhedra C and D is: ' );
disp(['dist(C,D) = ' num2str(cvx_optval)]);
disp('The optimal points are: ')
disp('x = '); disp(x);
disp('y = '); disp(y);

%Plotting
figure;
fill([-2; 0; 2; 0],[0;2;0;-2],'b', [3;5;5;3],[2;2;4;4],'r')
axis([-3 6 -3 6])
axis square
hold on;
plot(x(1),x(2),'k.')
plot(y(1),y(2),'k.')
plot([x(1) y(1)],[x(2) y(2)])
title('Euclidean distance between 2 polyhedron in R^2');
xlabel('x_1');
ylabel('x_2');
Computing the distance between the 2 polyhedra... 
Calling SeDuMi: 11 variables (0 free), 5 equality constraints
------------------------------------------------------------------------
SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 5, order n = 11, dim = 12, blocks = 2
nnz(A) = 20 + 0, nnz(ADA) = 19, nnz(L) = 12
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            6.27E+000 0.000
  1 :  1.24E-001 1.96E+000 0.000 0.3117 0.9000 0.9000   2.28  1  1  2.2E+000
  2 :  1.41E+000 5.50E-001 0.000 0.2812 0.9000 0.9000   1.00  1  1  6.1E-001
  3 :  2.06E+000 4.86E-002 0.000 0.0884 0.9900 0.9900   1.24  1  1  5.3E-002
  4 :  2.12E+000 1.96E-003 0.000 0.0403 0.9900 0.9900   1.01  1  1  2.1E-003
  5 :  2.12E+000 1.32E-004 0.431 0.0674 0.9900 0.9900   1.00  1  1  1.4E-004
  6 :  2.12E+000 2.36E-006 0.000 0.0179 0.9900 0.9905   1.00  1  1  5.5E-006
  7 :  2.12E+000 1.54E-010 0.294 0.0001 0.9999 1.0000   1.00  1  1  6.6E-010

iter seconds digits       c*x               b*y
  7      0.0   Inf  2.1213203432e+000  2.1213203433e+000
|Ax-b| =  1.8e-009, [Ay-c]_+ =  6.7E-012, |x|= 3.7e+000, |y|= 2.0e+000

Detailed timing (sec)
   Pre          IPM          Post
0.000E+000    2.003E-002    0.000E+000    
Max-norms: ||b||=4, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.12132
Done! 
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is: 
dist(C,D) = 2.1213
The optimal points are: 
x = 
    1.5000
    0.5000

y = 
    3.0000
    2.0000