MATLAB: How to determine maximum length in the x and y direction respectively


How do you determine the determine the maximum length in x and y direction of an irregular rectangular shape and then also show this by a line to mark this length.. If it was a regular shape i was just going to subtract the max y value from the y min value and so forth. How can can I calculate it from the ccordinates. Below is the image and attached is the text file. Any codes wil be greatly appreciated.

Best Answer

  • Try this:
    xy = dlmread('test2-coordinates.txt');
    x = xy(:, 1);
    y = xy(:, 2);
    plot(x, y, 'g*');
    grid on;
    % Find the two points farthest apart
    distances = pdist2(xy, xy);
    maxDistance = max(distances(:))
    [rows, columns] = find(distances == maxDistance)
    index1 = rows(1);
    index2 = rows(2);
    % Draw a line between index1 and index2
    hold on;
    plot([x(index1), x(index2)], [y(index1), y(index2)], 'bo-', 'LineWidth', 2);
    0000 Screenshot.png