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

2 views (last 30 days)
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.
Capture.PNG

Accepted Answer

Image Analyst
Image Analyst on 25 May 2019
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
  6 Comments
Conrade Muyambo
Conrade Muyambo on 28 May 2019
Thank you. how could i fit all the data to a line? Any codes are greatly appreciated!
Image Analyst
Image Analyst on 28 May 2019
Edited: Image Analyst on 28 May 2019
coefficients = polyfit(x, y, 1);
yFitted = polyval(coefficients, x);
plot(x, yFitted, 'b-', 'LineWidth', 2)

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!