% Create the sample data n=20; d1=[rand(1,n)/4+2; 3*rand(1,n)+2]; d2=[4*rand(1,n)-2; rand(1,n)/4+2]; d3=[4*rand(1,n)-2; rand(1,n)/4+5]; d4=[rand(1,n)/4-2; 3*rand(1,n)+2]; data=[d1, d2, d3, d4]; data=round(data*50); % Do the fitting theta=rectangleFit(data); fprintf('data=%s\n', mat2str(data)); fprintf('theta=%s\n', mat2str(theta)); % Plot the result plot(data(1,:), data(2,:), '.'); x1=theta(1)-theta(3); x2=theta(1)+theta(3); y1=theta(2)-theta(4); y2=theta(2)+theta(4); line([x1 x1 x2 x2 x1], [y1 y2 y2 y1 y1], 'color', 'r'); axis image legend({'Sample data', 'Final fitting rectangle'}, 'location', 'northOutside', 'orientation', 'horizontal');