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, 6 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 6, order n = 11, dim = 12, blocks = 6
nnz(A) = 11 + 0, nnz(ADA) = 24, nnz(L) = 15
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            6.13E+00 0.000
  1 :  -5.77E-01 2.08E+00 0.000 0.3392 0.9000 0.9000   2.26  1  1  1.8E+00
  2 :  -1.83E+00 5.31E-01 0.000 0.2555 0.9000 0.9000   1.21  1  1  3.8E-01
  3 :  -2.11E+00 2.87E-02 0.000 0.0539 0.9900 0.9900   1.21  1  1  1.7E-02
  4 :  -2.12E+00 5.78E-03 0.000 0.2017 0.9000 0.9000   1.02  1  1  3.4E-03
  5 :  -2.12E+00 1.77E-04 0.000 0.0306 0.9900 0.9900   1.00  1  1  1.3E-04
  6 :  -2.12E+00 1.18E-05 0.446 0.0664 0.9900 0.9900   1.00  1  1  8.3E-06
  7 :  -2.12E+00 8.56E-08 0.000 0.0073 0.9902 0.9900   1.00  1  1  1.2E-07
  8 :  -2.12E+00 1.02E-10 0.016 0.0012 0.9990 0.9874   1.00  1  1  1.0E-09

iter seconds digits       c*x               b*y
  8      0.0   Inf -2.1213203424e+00 -2.1213203412e+00
|Ax-b| =   6.2e-11, [Ay-c]_+ =   1.2E-09, |x|=  2.4e+00, |y|=  3.6e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    4.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 4,
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